cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -1,5 +1,12 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;

View File

@@ -1,29 +1,40 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\Context;
/**
* Interface for context for for document generation
* Interface for context for for document generation.
*/
interface DocGeneratorContextInterface
{
/**
* has form
* Get the data that will be injected to the generated document.
*
* @param mixed $entity
*/
public function hasForm(): bool;
public function getData($entity): array;
/**
* Generate the form that display
* Generate the form that display.
*
* @param mixed $entity
*/
public function getForm($entity);
/**
* True of false which entity supports
* has form.
*/
public function supports(string $entityClass): bool;
public function hasForm(): bool;
/**
* Get the data that will be injected to the generated document
* True of false which entity supports.
*/
public function getData($entity): array;
public function supports(string $entityClass): bool;
}

View File

@@ -1,31 +1,76 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\Context;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Exception;
/**
* Context that display a form to select a member of a houseHold
* Context that display a form to select a member of a houseHold.
*/
class HouseholdMemberSelectionContext implements DocGeneratorContextInterface
{
/**
* has form
* Get the data that will be injected to the generated document.
*
* @param mixed $entity
*/
public function hasForm(): bool {
return true;
public function getData($entity): array
{
$datas = [
'setValues' => [],
'cloneRowAndSetValues' => [],
];
$persons = $entity->getAccompanyingPeriodWork()->getPersons();
if (sizeof($persons) > 0) {
$firstPerson = $persons[0];
$datas['setValues'][] = [
'firstPersonFirstName' => $firstPerson->getFirstName(),
'firstPersonLastName' => $firstPerson->getLastName(), ];
}
if (get_class($entity) == AccompanyingPeriodWorkEvaluation::class) {
$values = [];
foreach ($entity->getAccompanyingPeriodWork()->getPersons() as $person) {
$i = 1;
$values[] = [
'personRowId' => $i,
'personFirstName' => $person->getFirstName(),
'personLastName' => $person->getLastName(),
];
}
$datas['cloneRowAndSetValues'][] = [
'personRowId', $values, ];
}
return $datas;
}
/**
* Generate the form that display
* Generate the form that display.
*
* @param mixed $entity
*/
public function getForm($entity) {
throw new \Exception("No implemented yet", 1);
public function getForm($entity)
{
throw new Exception('No implemented yet', 1);
$choices = [];
if(get_class($entity) == AccompanyingPeriodWorkEvaluation::class) {
if (get_class($entity) == AccompanyingPeriodWorkEvaluation::class) {
foreach ($entity->getAccompanyingPeriodWork()->getPersons() as $person) {
$choices[$person->getId()] = $person->getName();
}
@@ -34,56 +79,27 @@ class HouseholdMemberSelectionContext implements DocGeneratorContextInterface
$builder->add('members', ChoiceType::class, [
'choices' => $choices,
'placeholder' => 'Choose a person',
'label' => 'Person to add'
'label' => 'Person to add',
]);
return $builder;
}
/**
* True of false which entity supports
* has form.
*/
public function supports(string $entityClass): bool {
return
($entityClass == AccompanyingPeriod::class) ||
($entityClass == SocialAction::class);
public function hasForm(): bool
{
return true;
}
/**
* Get the data that will be injected to the generated document
* True of false which entity supports.
*/
public function getData($entity): array {
$datas = array(
'setValues' => array(),
'cloneRowAndSetValues' => array()
);
$persons = $entity->getAccompanyingPeriodWork()->getPersons();
if(sizeof($persons) > 0) {
$firstPerson = $persons[0];
$datas['setValues'][] = array(
'firstPersonFirstName' => $firstPerson->getFirstName(),
'firstPersonLastName' => $firstPerson->getLastName(),);
}
if(get_class($entity) == AccompanyingPeriodWorkEvaluation::class) {
$values = array();
foreach($entity->getAccompanyingPeriodWork()->getPersons() as $person) {
$i = 1;
$values[] = array(
'personRowId' => $i,
'personFirstName' => $person->getFirstName(),
'personLastName' => $person->getLastName(),
);
}
$datas['cloneRowAndSetValues'][] = array(
'personRowId', $values);
}
return $datas;
public function supports(string $entityClass): bool
{
return
(AccompanyingPeriod::class == $entityClass)
|| (SocialAction::class == $entityClass);
}
}

View File

@@ -1,5 +1,12 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\Controller;
use Chill\MainBundle\CRUD\Controller\CRUDController;

View File

@@ -1,46 +1,40 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\Routing\Annotation\Route;
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlOpenstackGenerator;
use PhpOffice\PhpWord\TemplateProcessor;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class DocGeneratorController
*
* @package Chill\DocGenerator\Controller
* Class DocGeneratorController.
*/
class DocGeneratorController extends AbstractController
{
/**
* @Route(
* "{_locale}/doc/gen/test",
* name="chill_docgenerator_test"
* )
*/
public function testAction(): Response
{
return (new Response())->setContent('Test');
}
/**
* @Route(
* "{_locale}/doc/gen/test",
* name="chill_docgenerator_test"
* "{_locale}/doc/gen/test",
* name="chill_docgenerator_test"
* )
*/
public function getDoc(Request $request, TempUrlOpenstackGenerator $tempUrlGenerator): Response
{
$p = $tempUrlGenerator->generate(
'GET',
'FORMULAIRE_AEB.docx',
$request->query->has('expires_delay') ? $request->query->getInt('expires_delay', 0) : null
);
'GET',
'FORMULAIRE_AEB.docx',
$request->query->has('expires_delay') ? $request->query->getInt('expires_delay', 0) : null
);
$tmpfname = tempnam(sys_get_temp_dir(), 'DOC_TEMPLATE');
file_put_contents($tmpfname, file_get_contents($p->url));
@@ -66,4 +60,15 @@ class DocGeneratorController extends AbstractController
return $response;
}
/**
* @Route(
* "{_locale}/doc/gen/test",
* name="chill_docgenerator_test"
* )
*/
public function testAction(): Response
{
return (new Response())->setContent('Test');
}
}

View File

@@ -1,54 +1,36 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\Controller;
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface;
use Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument;
use Chill\PersonBundle\Entity\SocialWork\Evaluation;
use GuzzleHttp\Exception\TransferException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\Routing\Annotation\Route;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlOpenstackGenerator;
use PhpOffice\PhpWord\TemplateProcessor;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException;
use PhpOffice\PhpWord\TemplateProcessor;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
// TODO à mettre dans services
use Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DocGeneratorTemplateController extends AbstractController
{
/**
* @Route(
* "{_locale}/doc/gen/templates/for/{entityClassName}",
* name="chill_docgenerator_templates_for_entity_api"
* )
*/
public function listTemplateApiAction(
string $entityClassName, DocGeneratorTemplateRepository $templateRepository): Response
{
$entities = $templateRepository->findByEntity($entityClassName);
$ret = [];
foreach ($entities as $entity) {
$ret[] = [
'id' => $entity->getId(),
'name' => $entity->getName(),
'description' => $entity->getDescription()
];
}
return new JsonResponse(['results' => $ret]);
}
/**
* @Route(
* "{_locale}/doc/gen/generate/from/{template}/for/{entityClassName}/{entityId}",
@@ -64,7 +46,8 @@ class DocGeneratorTemplateController extends AbstractController
): Response {
$getUrlGen = $tempUrlGenerator->generate(
'GET',
$template->getFile());
$template->getFile()
);
$tmpfname = tempnam(sys_get_temp_dir(), 'DOC_TEMPLATE');
file_put_contents($tmpfname, file_get_contents($getUrlGen->url));
@@ -75,7 +58,7 @@ class DocGeneratorTemplateController extends AbstractController
$context = new HouseholdMemberSelectionContext();
$datas = $context->getData($entity);
} else {
throw new \Exception('Not implemented', 1);
throw new Exception('Not implemented', 1);
}
$templateProcessor = new TemplateProcessor($tmpfname);
@@ -99,7 +82,8 @@ class DocGeneratorTemplateController extends AbstractController
$getUrlGen = $tempUrlGenerator->generate(
'PUT',
$genDocName);
$genDocName
);
unlink($tmpfname2);
@@ -107,7 +91,7 @@ class DocGeneratorTemplateController extends AbstractController
try {
$putResponse = $client->request('PUT', $getUrlGen->url, [
'body' => $fileContent
'body' => $fileContent,
]);
if ($putResponse->getStatusCode() == 201) {
@@ -125,8 +109,7 @@ class DocGeneratorTemplateController extends AbstractController
$doc = new AccompanyingPeriodWorkEvaluationDocument();
$doc
->setStoredObject($storedObject)
->setTemplate($template)
;
->setTemplate($template);
$entity->addDocument($doc);
$em->persist($doc);
}
@@ -135,13 +118,39 @@ class DocGeneratorTemplateController extends AbstractController
return $this->redirectToRoute('chill_wopi_file_edit', [
'fileId' => $storedObject->getUuid(),
'returnPath' => $request->query->get('returnPath', "/")
'returnPath' => $request->query->get('returnPath', '/'),
]);
}
} catch (TransferException $e) {
throw $e;
}
throw new \Exception('Unable to generate document.');
throw new Exception('Unable to generate document.');
}
/**
* @Route(
* "{_locale}/doc/gen/templates/for/{entityClassName}",
* name="chill_docgenerator_templates_for_entity_api"
* )
*/
public function listTemplateApiAction(
string $entityClassName,
DocGeneratorTemplateRepository $templateRepository
): Response
{
$entities = $templateRepository->findByEntity($entityClassName);
$ret = [];
foreach ($entities as $entity) {
$ret[] = [
'id' => $entity->getId(),
'name' => $entity->getName(),
'description' => $entity->getDescription(),
];
}
return new JsonResponse(['results' => $ret]);
}
}

View File

@@ -1,23 +1,25 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\DataFixtures\ORM;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
/**
* Load DocGeneratorTemplate
*
* @author Champs-Libres Coop
* Load DocGeneratorTemplate.
*/
class LoadDocGeneratorTemplate extends AbstractFixture
class LoadDocGeneratorTemplate extends AbstractFixture
{
public function load(ObjectManager $manager)
{
$templates = [
@@ -37,16 +39,14 @@ class LoadDocGeneratorTemplate extends AbstractFixture
];
foreach ($templates as $template) {
print('Adding doc generator templates '.$template['file'].")\n");
echo 'Adding doc generator templates ' . $template['file'] . ")\n";
$newTemplate = (new DocGeneratorTemplate())
->setName($template['name'])
->setDescription($template['desc'])
->setFile($template['file'])
->setContext($template['context'])
->setEntities($template['entities'])
;
->setEntities($template['entities']);
$manager->persist($newTemplate);

View File

@@ -1,26 +1,30 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
/**
* This is the class that loads and manages your bundle configuration.
*
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
* @see http://symfony.com/doc/current/cookbook/bundles/extension.html
*/
class ChillDocGeneratorExtension extends Extension implements PrependExtensionInterface
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../config'));
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../config'));
$loader->load('services.yaml');
$loader->load('services/controller.yaml');
$loader->load('services/fixtures.yaml');
@@ -33,20 +37,6 @@ class ChillDocGeneratorExtension extends Extension implements PrependExtensionIn
$this->prependCruds($container);
}
protected function preprendRoutes(ContainerBuilder $container)
{
$container->prependExtensionConfig('chill_main', array(
'routing' => array(
'resources' => array(
'@ChillDocGeneratorBundle/config/routes.yaml'
)
)
));
}
/**
* @param ContainerBuilder $container
*/
protected function prependCruds(ContainerBuilder $container)
{
$container->prependExtensionConfig('chill_main', [
@@ -60,19 +50,30 @@ class ChillDocGeneratorExtension extends Extension implements PrependExtensionIn
'actions' => [
'index' => [
'template' => '@ChillDocGenerator/Admin/DocGeneratorTemplate/index.html.twig',
'role' => 'ROLE_ADMIN'
'role' => 'ROLE_ADMIN',
],
'new' => [
'new' => [
'role' => 'ROLE_ADMIN',
'template' => '@ChillDocGenerator/Admin/DocGeneratorTemplate/new.html.twig',
],
'edit' => [
'edit' => [
'role' => 'ROLE_ADMIN',
'template' => '@ChillDocGenerator/Admin/DocGeneratorTemplate/edit.html.twig',
]
]
]
]
],
],
],
],
]);
}
protected function preprendRoutes(ContainerBuilder $container)
{
$container->prependExtensionConfig('chill_main', [
'routing' => [
'resources' => [
'@ChillDocGeneratorBundle/config/routes.yaml',
],
],
]);
}
}

View File

@@ -1,5 +1,12 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
@@ -12,9 +19,6 @@ use Symfony\Component\Config\Definition\ConfigurationInterface;
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('chill_calendar');

View File

@@ -1,8 +1,14 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\Entity;
use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
@@ -12,6 +18,36 @@ use Symfony\Component\Serializer\Annotation as Serializer;
*/
class DocGeneratorTemplate
{
/**
* @ORM\Column(type="string", length=255)
*
* Class name of the context to use
*
* so if $context = ''
* this template will use '' as context
*/
private string $context;
/**
* @ORM\Column(type="text", nullable=true)
*/
private string $description;
/**
* @ORM\Column(type="simple_array")
*
* Class name of the entities for which this template can be used
*
* so if $entities = ['Chill\PersonBundle\Entity\AccompanyingPeriod', 'Chill\PersonBundle\Entity\SocialWork\SocialAction']
* this template can be selected for an AccompanyingPeriod or a SocialAction
*/
private array $entities = [];
/**
* @ORM\Column(type="string", length=255)
*/
private string $file;
/**
* @ORM\Id
* @ORM\GeneratedValue
@@ -26,37 +62,25 @@ class DocGeneratorTemplate
*/
private array $name = [];
/**
* @ORM\Column(type="text", nullable=true)
*/
private string $description;
public function getContext(): ?string
{
return $this->context;
}
public function getDescription(): ?string
{
return $this->description;
}
/**
* @ORM\Column(type="simple_array")
*
* Class name of the entities for which this template can be used
*
* so if $entities = ['Chill\PersonBundle\Entity\AccompanyingPeriod', 'Chill\PersonBundle\Entity\SocialWork\SocialAction']
* this template can be selected for an AccompanyingPeriod or a SocialAction
*/
private array $entities = [];
public function getEntities(): ?array
{
return $this->entities;
}
/**
* @ORM\Column(type="string", length=255)
*
* Class name of the context to use
*
* so if $context = ''
* this template will use '' as context
*/
private string $context;
/**
* @ORM\Column(type="string", length=255)
*/
private string $file;
public function getFile(): ?string
{
return $this->file;
}
public function getId(): ?int
{
@@ -68,18 +92,13 @@ class DocGeneratorTemplate
return $this->name;
}
public function setName(array $name): self
public function setContext(string $context): self
{
$this->name = $name;
$this->context = $context;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
@@ -87,9 +106,11 @@ class DocGeneratorTemplate
return $this;
}
public function getFile(): ?string
public function setEntities(array $entities): self
{
return $this->file;
$this->entities = $entities;
return $this;
}
public function setFile(string $file): self
@@ -99,26 +120,9 @@ class DocGeneratorTemplate
return $this;
}
public function getEntities(): ?array
public function setName(array $name): self
{
return $this->entities;
}
public function setEntities(array $entities): self
{
$this->entities = $entities;
return $this;
}
public function getContext(): ?string
{
return $this->context;
}
public function setContext(string $context): self
{
$this->context = $context;
$this->name = $name;
return $this;
}

View File

@@ -1,38 +1,35 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\Form;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
class DocGeneratorTemplateType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TranslatableStringFormType::class, [
'label' => 'Nom'
'label' => 'Nom',
])
->add('description')
->add('file')
;
->add('file');
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefault('class', DocGeneratorTemplate::class)
;
->setDefault('class', DocGeneratorTemplate::class);
}
}

View File

@@ -1,13 +1,20 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Repository;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Doctrine\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository;
final class DocGeneratorTemplateRepository implements ObjectRepository
{
@@ -23,11 +30,6 @@ final class DocGeneratorTemplateRepository implements ObjectRepository
return $this->repository->find($id, $lockMode, $lockVersion);
}
public function findOneBy(array $criteria, array $orderBy = null): ?DocGeneratorTemplate
{
return $this->repository->findOneBy($criteria, $orderBy);
}
/**
* @return DocGeneratorTemplate[]
*/
@@ -37,26 +39,34 @@ final class DocGeneratorTemplateRepository implements ObjectRepository
}
/**
* @param mixed|null $limit
* @param mixed|null $offset
*
* @return DocGeneratorTemplate[]
*/
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null): array
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
public function findByEntity($entity) {
public function findByEntity($entity)
{
$builder = $this->repository->createQueryBuilder('t');
$builder
->where('t.entities LIKE :entity')
->setParameter('entity', '%'.addslashes($entity).'%')
;
->setParameter('entity', '%' . addslashes($entity) . '%');
return $builder->getQuery()->execute();
}
public function getClassName() {
public function findOneBy(array $criteria, ?array $orderBy = null): ?DocGeneratorTemplate
{
return $this->repository->findOneBy($criteria, $orderBy);
}
public function getClassName()
{
return DocGeneratorTemplate::class;
}
}

View File

@@ -1,19 +1,24 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\Serializer\Encoder;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use function array_keys;
use function is_array;
class DocGenEncoder implements \Symfony\Component\Serializer\Encoder\EncoderInterface
{
/**
* @inheritDoc
*/
public function encode($data, string $format, array $context = [])
{
if (!$this->isAssociative($data)) {
throw new UnexpectedValueException("Only associative arrays are allowed; lists are not allowed");
throw new UnexpectedValueException('Only associative arrays are allowed; lists are not allowed');
}
$result = [];
@@ -22,11 +27,28 @@ class DocGenEncoder implements \Symfony\Component\Serializer\Encoder\EncoderInte
return $result;
}
public function supportsEncoding(string $format)
{
return 'docgen' === $format;
}
private function canonicalizeKey(string $path, string $key): string
{
return '' === $path ? $key : $path . '_' . $key;
}
private function isAssociative(array $data)
{
$keys = array_keys($data);
return array_keys($keys) !== $keys;
}
private function recusiveEncoding(array $data, array &$result, $path)
{
if ($this->isAssociative($data)) {
foreach ($data as $key => $value) {
if (\is_array($value)) {
if (is_array($value)) {
$this->recusiveEncoding($value, $result, $this->canonicalizeKey($path, $key));
} else {
$result[$this->canonicalizeKey($path, $key)] = $value;
@@ -34,9 +56,8 @@ class DocGenEncoder implements \Symfony\Component\Serializer\Encoder\EncoderInte
}
} else {
foreach ($data as $elem) {
if (!$this->isAssociative($elem)) {
throw new UnexpectedValueException(sprintf("Embedded loops are not allowed. See data under %s path", $path));
throw new UnexpectedValueException(sprintf('Embedded loops are not allowed. See data under %s path', $path));
}
$sub = [];
@@ -45,25 +66,4 @@ class DocGenEncoder implements \Symfony\Component\Serializer\Encoder\EncoderInte
}
}
}
private function canonicalizeKey(string $path, string $key): string
{
return $path === '' ? $key : $path.'_'.$key;
}
private function isAssociative(array $data)
{
$keys = \array_keys($data);
return $keys !== \array_keys($keys);
}
/**
* @inheritDoc
*/
public function supportsEncoding(string $format)
{
return $format === 'docgen';
}
}

View File

@@ -1,8 +1,16 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\Serializer\Helper;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use function array_merge;
class NormalizeNullValueHelper
{
@@ -31,12 +39,15 @@ class NormalizeNullValueHelper
case 'string':
case 'null':
$data[$key] = '';
break;
default:
$data[$key] = $this->normalizer->normalize(null, $format, \array_merge(
$data[$key] = $this->normalizer->normalize(null, $format, array_merge(
$context,
['docgen:expects' => $class]
));
break;
}
}

View File

@@ -1,8 +1,16 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\Serializer\Normalizer;
use Chill\DocGeneratorBundle\Serializer\Helper\NormalizeNullValueHelper;
use ReflectionClass;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
@@ -14,12 +22,19 @@ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use function array_filter;
use function array_key_exists;
use function array_merge;
use function implode;
use function in_array;
use function is_array;
class DocGenObjectNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
private ClassMetadataFactoryInterface $classMetadataFactory;
private PropertyAccessor $propertyAccess;
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory)
@@ -28,32 +43,30 @@ class DocGenObjectNormalizer implements NormalizerInterface, NormalizerAwareInte
$this->propertyAccess = PropertyAccess::createPropertyAccessor();
}
/**
* @inheritDoc
*/
public function normalize($object, string $format = null, array $context = [])
public function normalize($object, ?string $format = null, array $context = [])
{
$classMetadataKey = $object ?? $context['docgen:expects'];
if (!$this->classMetadataFactory->hasMetadataFor($classMetadataKey)) {
throw new LogicException(sprintf("This object does not have metadata: %s. Add groups on this entity to allow to serialize with the format %s and groups %s", is_object($object) ? get_class($object) : $context['docgen:expects'], $format, \implode(', ', $context['groups'])));
throw new LogicException(sprintf('This object does not have metadata: %s. Add groups on this entity to allow to serialize with the format %s and groups %s', is_object($object) ? get_class($object) : $context['docgen:expects'], $format, implode(', ', $context['groups'])));
}
$metadata = $this->classMetadataFactory->getMetadataFor($classMetadataKey);
$expectedGroups = \array_key_exists(AbstractNormalizer::GROUPS, $context) ?
\is_array($context[AbstractNormalizer::GROUPS]) ? $context[AbstractNormalizer::GROUPS] : [$context[AbstractNormalizer::GROUPS]]
$expectedGroups = array_key_exists(AbstractNormalizer::GROUPS, $context) ?
is_array($context[AbstractNormalizer::GROUPS]) ? $context[AbstractNormalizer::GROUPS] : [$context[AbstractNormalizer::GROUPS]]
: [];
$attributes = \array_filter(
$attributes = array_filter(
$metadata->getAttributesMetadata(),
function (AttributeMetadata $a) use ($expectedGroups) {
foreach ($a->getGroups() as $g) {
if (\in_array($g, $expectedGroups, true)) {
if (in_array($g, $expectedGroups, true)) {
return true;
}
}
return false;
});
}
);
if (null === $object) {
return $this->normalizeNullData($format, $context, $metadata, $attributes);
@@ -62,11 +75,36 @@ class DocGenObjectNormalizer implements NormalizerInterface, NormalizerAwareInte
return $this->normalizeObject($object, $format, $context, $expectedGroups, $metadata, $attributes);
}
public function supportsNormalization($data, ?string $format = null): bool
{
return 'docgen' === $format && (is_object($data) || null === $data);
}
private function getExpectedType(AttributeMetadata $attribute, ReflectionClass $reflection): string
{
// we have to get the expected content
if ($reflection->hasProperty($attribute->getName())) {
$type = $reflection->getProperty($attribute->getName())->getType();
} elseif ($reflection->hasMethod($attribute->getName())) {
$type = $reflection->getMethod($attribute->getName())->getReturnType();
} else {
throw new \LogicException(sprintf(
'Could not determine how the content is determined for the attribute %s. Add attribute property only on property or method',
$attribute->getName()
));
}
if (null === $type) {
throw new \LogicException(sprintf(
'Could not determine the type for this attribute: %s. Add a return type to the method or property declaration',
$attribute->getName()
));
}
return $type->getName();
}
/**
* @param string $format
* @param array $context
* @param array $expectedGroups
* @param ClassMetadata $metadata
* @param array|AttributeMetadata[] $attributes
*/
private function normalizeNullData(string $format, array $context, ClassMetadata $metadata, array $attributes): array
@@ -83,15 +121,43 @@ class DocGenObjectNormalizer implements NormalizerInterface, NormalizerAwareInte
return $normalizer->normalize($keys, $format, $context);
}
/**
* @param mixed $format
*/
private function normalizeNullOutputValue($format, array $context, AttributeMetadata $attribute, ReflectionClass $reflection)
{
$type = $this->getExpectedType($attribute, $reflection);
switch ($type) {
case 'array':
case 'bool':
case 'double':
case 'float':
case 'int':
case 'resource':
case 'string':
return '';
default:
return $this->normalizer->normalize(
null,
$format,
array_merge(
$context,
['docgen:expects' => $type]
)
);
}
}
/**
* @param $object
* @param $format
* @param array $context
* @param array $expectedGroups
* @param ClassMetadata $metadata
* @param array|AttributeMetadata[] $attributes
* @return array
*
* @throws ExceptionInterface
*
* @return array
*/
private function normalizeObject($object, $format, array $context, array $expectedGroups, ClassMetadata $metadata, array $attributes)
{
@@ -105,8 +171,9 @@ class DocGenObjectNormalizer implements NormalizerInterface, NormalizerAwareInte
if (is_object($value)) {
$data[$key] =
$this->normalizer->normalize($value, $format, \array_merge(
$context, $attribute->getNormalizationContextForGroups($expectedGroups)
$this->normalizer->normalize($value, $format, array_merge(
$context,
$attribute->getNormalizationContextForGroups($expectedGroups)
));
} elseif (null === $value) {
$data[$key] = $this->normalizeNullOutputValue($format, $context, $attribute, $reflection);
@@ -117,61 +184,4 @@ class DocGenObjectNormalizer implements NormalizerInterface, NormalizerAwareInte
return $data;
}
private function getExpectedType(AttributeMetadata $attribute, \ReflectionClass $reflection): string
{
// we have to get the expected content
if ($reflection->hasProperty($attribute->getName())) {
$type = $reflection->getProperty($attribute->getName())->getType();
} elseif ($reflection->hasMethod($attribute->getName())) {
$type = $reflection->getMethod($attribute->getName())->getReturnType();
} else {
throw new \LogicException(sprintf(
"Could not determine how the content is determined for the attribute %s. Add attribute property only on property or method", $attribute->getName()
));
}
if (null === $type) {
throw new \LogicException(sprintf(
"Could not determine the type for this attribute: %s. Add a return type to the method or property declaration", $attribute->getName()
));
}
return $type->getName();
}
/**
*/
private function normalizeNullOutputValue($format, array $context, AttributeMetadata $attribute, \ReflectionClass $reflection)
{
$type = $this->getExpectedType($attribute, $reflection);
switch ($type) {
case 'array':
case 'bool':
case 'double':
case 'float':
case 'int':
case 'resource':
case 'string':
return '';
default:
return $this->normalizer->normalize(
null,
$format,
\array_merge(
$context,
['docgen:expects' => $type]
)
);
}
}
/**
* @inheritDoc
*/
public function supportsNormalization($data, string $format = null): bool
{
return $format === 'docgen' && (is_object($data) || null === $data);
}
}

View File

@@ -1,5 +1,12 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\Migrations\DocGenerator;
@@ -8,10 +15,16 @@ use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Creation of table for storing DocGenTemplate
* Creation of table for storing DocGenTemplate.
*/
final class Version20210805162522 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('DROP SEQUENCE chill_docgen_template_id_seq CASCADE');
$this->addSql('DROP TABLE chill_docgen_template');
}
public function getDescription(): string
{
return 'Creation of table for storing DocGenTemplate';
@@ -22,10 +35,4 @@ final class Version20210805162522 extends AbstractMigration
$this->addSql('CREATE SEQUENCE chill_docgen_template_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE chill_docgen_template (id INT NOT NULL, name JSON NOT NULL, description TEXT DEFAULT NULL, file VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
}
public function down(Schema $schema): void
{
$this->addSql('DROP SEQUENCE chill_docgen_template_id_seq CASCADE');
$this->addSql('DROP TABLE chill_docgen_template');
}
}

View File

@@ -1,5 +1,12 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\Migrations\DocGenerator;
@@ -8,10 +15,17 @@ use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Add entities and context fields to DocGenTemplate
* Add entities and context fields to DocGenTemplate.
*/
final class Version20210812214310 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_docgen_template DROP entities');
$this->addSql('ALTER TABLE chill_docgen_template DROP context');
$this->addSql('COMMENT ON COLUMN chill_docgen_template.name IS NULL');
}
public function getDescription(): string
{
return 'Add entities and context fields to DocGenTemplate';
@@ -23,11 +37,4 @@ final class Version20210812214310 extends AbstractMigration
$this->addSql('ALTER TABLE chill_docgen_template ADD context VARCHAR(255)');
$this->addSql('COMMENT ON COLUMN chill_docgen_template.entities IS \'(DC2Type:simple_array)\'');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_docgen_template DROP entities');
$this->addSql('ALTER TABLE chill_docgen_template DROP context');
$this->addSql('COMMENT ON COLUMN chill_docgen_template.name IS NULL');
}
}

View File

@@ -1,5 +1,12 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\Migrations\DocGenerator;
@@ -9,6 +16,11 @@ use Doctrine\Migrations\AbstractMigration;
final class Version20211119173556 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->throwIrreversibleMigrationException();
}
public function getDescription(): string
{
return 'remove comment on deprecated json_array type';
@@ -17,16 +29,11 @@ final class Version20211119173556 extends AbstractMigration
public function up(Schema $schema): void
{
$columns = [
'chill_docgen_template.name'
'chill_docgen_template.name',
];
foreach ($columns as $col) {
$this->addSql("COMMENT ON COLUMN $col IS NULL");
$this->addSql("COMMENT ON COLUMN {$col} IS NULL");
}
}
public function down(Schema $schema): void
{
$this->throwIrreversibleMigrationException();
}
}

View File

@@ -1,12 +1,22 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\Tests\Serializer\Encoder;
use Chill\DocGeneratorBundle\Serializer\Encoder\DocGenEncoder;
use Doctrine\ORM\EntityRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
/**
* @internal
* @coversNothing
*/
class DocGenEncoderTest extends TestCase
{
private DocGenEncoder $encoder;
@@ -18,62 +28,32 @@ class DocGenEncoderTest extends TestCase
$this->encoder = new DocGenEncoder();
}
/**
* @dataProvider generateEncodeData
*/
public function testEncode($expected, $data, string $msg)
{
$generated = $this->encoder->encode($data, 'docgen');
$this->assertEquals($expected, $generated, $msg);
}
public function testEmbeddedLoopsThrowsException()
{
$this->expectException(UnexpectedValueException::class);
$data = [
'data' => [
['item' => 'one'],
[
'embedded' => [
[
['subitem' => 'two'],
['subitem' => 'three']
]
]
],
]
];
$this->encoder->encode($data, 'docgen');
}
public function generateEncodeData()
{
yield [ ['tests' => 'ok'], ['tests' => 'ok'], "A simple test with a simple array"];
yield [
// expected:
['item_subitem' => 'value'],
// data:
['item' => ['subitem' => 'value']],
"A test with multidimensional array"
];
yield [
// expected:
[ 'data' => [['item' => 'one'], ['item' => 'two']] ],
// data:
[ 'data' => [['item' => 'one'], ['item' => 'two']] ],
"a list of items"
];
yield [['tests' => 'ok'], ['tests' => 'ok'], 'A simple test with a simple array'];
yield [
// expected:
[ 'data' => [['item_subitem' => 'alpha'], ['item' => 'two']] ],
['item_subitem' => 'value'],
// data:
[ 'data' => [['item' => ['subitem' => 'alpha']], ['item' => 'two'] ] ],
"a list of items with multidimensional array inside item"
['item' => ['subitem' => 'value']],
'A test with multidimensional array',
];
yield [
// expected:
['data' => [['item' => 'one'], ['item' => 'two']]],
// data:
['data' => [['item' => 'one'], ['item' => 'two']]],
'a list of items',
];
yield [
// expected:
['data' => [['item_subitem' => 'alpha'], ['item' => 'two']]],
// data:
['data' => [['item' => ['subitem' => 'alpha']], ['item' => 'two']]],
'a list of items with multidimensional array inside item',
];
yield [
@@ -88,9 +68,9 @@ class DocGenEncoderTest extends TestCase
'father_firstname' => 'Marcel',
'father_lastname' => 'Dupont',
'father_dateOfBirth_long' => '10 novembre 1953',
'father_dateOfBirth_short' => '10/11/1953'
'father_dateOfBirth_short' => '10/11/1953',
],
]
],
],
// data:
[
@@ -98,16 +78,49 @@ class DocGenEncoderTest extends TestCase
[
'firstname' => 'Jonathan',
'lastname' => 'Dupont',
'dateOfBirth' => [ 'long' => '16 juin 1981', 'short' => '16/06/1981'],
'dateOfBirth' => ['long' => '16 juin 1981', 'short' => '16/06/1981'],
'father' => [
'firstname' => 'Marcel',
'lastname' => 'Dupont',
'dateOfBirth' => ['long' => '10 novembre 1953', 'short' => '10/11/1953']
]
'dateOfBirth' => ['long' => '10 novembre 1953', 'short' => '10/11/1953'],
],
],
]
],
],
"a longer list, with near real data inside and embedded associative arrays"
'a longer list, with near real data inside and embedded associative arrays',
];
}
public function testEmbeddedLoopsThrowsException()
{
$this->expectException(UnexpectedValueException::class);
$data = [
'data' => [
['item' => 'one'],
[
'embedded' => [
[
['subitem' => 'two'],
['subitem' => 'three'],
],
],
],
],
];
$this->encoder->encode($data, 'docgen');
}
/**
* @dataProvider generateEncodeData
*
* @param mixed $expected
* @param mixed $data
*/
public function testEncode($expected, $data, string $msg)
{
$generated = $this->encoder->encode($data, 'docgen');
$this->assertEquals($expected, $generated, $msg);
}
}

View File

@@ -1,5 +1,12 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocGeneratorBundle\tests\Serializer\Normalizer;
use Chill\MainBundle\Entity\Center;
@@ -8,11 +15,14 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* @internal
* @coversNothing
*/
class DocGenObjectNormalizerTest extends KernelTestCase
{
private NormalizerInterface $normalizer;
protected function setUp()
{
parent::setUp();
@@ -28,33 +38,16 @@ class DocGenObjectNormalizerTest extends KernelTestCase
$user->setMainCenter($center = new Center());
$center->setName('test');
$normalized = $this->normalizer->normalize($user, 'docgen', [ AbstractNormalizer::GROUPS => ['docgen:read']]);
$normalized = $this->normalizer->normalize($user, 'docgen', [AbstractNormalizer::GROUPS => ['docgen:read']]);
$expected = [
'label' => 'User Test',
'email' => '',
'mainCenter' => [
'name' => 'test'
]
'name' => 'test',
],
];
$this->assertEquals($expected, $normalized, "test normalization fo an user");
}
public function testNormalizeWithNullValueEmbedded()
{
$user = new User();
$user->setUsername('User Test');
$normalized = $this->normalizer->normalize($user, 'docgen', [ AbstractNormalizer::GROUPS => ['docgen:read']]);
$expected = [
'label' => 'User Test',
'email' => '',
'mainCenter' => [
'name' => ''
]
];
$this->assertEquals($expected, $normalized, "test normalization fo an user with null center");
$this->assertEquals($expected, $normalized, 'test normalization fo an user');
}
public function testNormalizeNullObjectWithObjectEmbedded()
@@ -68,13 +61,27 @@ class DocGenObjectNormalizerTest extends KernelTestCase
'label' => '',
'email' => '',
'mainCenter' => [
'name' => ''
]
'name' => '',
],
];
$this->assertEquals($expected, $normalized, "test normalization for a null user");
$this->assertEquals($expected, $normalized, 'test normalization for a null user');
}
public function testNormalizeWithNullValueEmbedded()
{
$user = new User();
$user->setUsername('User Test');
$normalized = $this->normalizer->normalize($user, 'docgen', [AbstractNormalizer::GROUPS => ['docgen:read']]);
$expected = [
'label' => 'User Test',
'email' => '',
'mainCenter' => [
'name' => '',
],
];
$this->assertEquals($expected, $normalized, 'test normalization fo an user with null center');
}
}