docgen: add age and base context

This commit is contained in:
Julien Fastré 2021-12-15 23:42:37 +01:00
parent 689f612382
commit 9453fc2dd5
6 changed files with 15 additions and 5 deletions

View File

@ -13,6 +13,8 @@ and this project adheres to
<!-- write down unreleased development here --> <!-- write down unreleased development here -->
* AddAddress: optimize loading: wait for the user finish typing; * AddAddress: optimize loading: wait for the user finish typing;
* UserPicker: fix bug with deprecated role * UserPicker: fix bug with deprecated role
* docgen: add base context + tests
* docgen: add age for person
## Test releases ## Test releases

View File

@ -45,7 +45,7 @@ class BaseContextData
$data['location'] = $this->normalizer->normalize( $data['location'] = $this->normalizer->normalize(
$user instanceof User ? $user->getCurrentLocation() : null, $user instanceof User ? $user->getCurrentLocation() : null,
'docgen', 'docgen',
['docgen:expects' => Location::class, 'groups' => ['docgen:expects']] ['docgen:expects' => Location::class, 'groups' => ['docgen:read']]
); );
return $data; return $data;

View File

@ -38,6 +38,7 @@ final class BaseContextDataTest extends KernelTestCase
$this->assertIsArray($actual); $this->assertIsArray($actual);
$this->assertArrayHasKey('creator', $actual); $this->assertArrayHasKey('creator', $actual);
$this->assertArrayHasKey('createdAt', $actual); $this->assertArrayHasKey('createdAt', $actual);
$this->assertArrayHasKey('location', $actual);
} }
public function testGenerateWithUser() public function testGenerateWithUser()
@ -52,6 +53,7 @@ final class BaseContextDataTest extends KernelTestCase
$this->assertIsArray($actual); $this->assertIsArray($actual);
$this->assertArrayHasKey('creator', $actual); $this->assertArrayHasKey('creator', $actual);
$this->assertArrayHasKey('createdAt', $actual); $this->assertArrayHasKey('createdAt', $actual);
$this->assertArrayHasKey('location', $actual);
} }
private function buildBaseContext( private function buildBaseContext(

View File

@ -74,7 +74,7 @@ class PersonDocGenNormalizer implements
$data = [ $data = [
'type' => 'person', 'type' => 'person',
'isNull' => false, 'isNull' => false,
'civility' => $this->normalizer->normalize($person->getCivility(), $format, array_merge($context, ['docgen:expect' => Civility::class])), 'civility' => $this->normalizer->normalize($person->getCivility(), $format, array_merge($context, ['docgen:expects' => Civility::class])),
'firstname' => $person->getFirstName(), 'firstname' => $person->getFirstName(),
'lastname' => $person->getLastName(), 'lastname' => $person->getLastName(),
'altNames' => implode( 'altNames' => implode(
@ -87,6 +87,7 @@ class PersonDocGenNormalizer implements
) )
), ),
'text' => $this->personRender->renderString($person, []), 'text' => $this->personRender->renderString($person, []),
'age' => $person->getAge(),
'birthdate' => $this->normalizer->normalize($person->getBirthdate(), $format, $dateContext), 'birthdate' => $this->normalizer->normalize($person->getBirthdate(), $format, $dateContext),
'deathdate' => $this->normalizer->normalize($person->getDeathdate(), $format, $dateContext), 'deathdate' => $this->normalizer->normalize($person->getDeathdate(), $format, $dateContext),
'gender' => $this->translator->trans($person->getGender()), 'gender' => $this->translator->trans($person->getGender()),

View File

@ -15,6 +15,7 @@ use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface;
use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocGeneratorBundle\Service\Context\BaseContextData;
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Repository\DocumentCategoryRepository; use Chill\DocStoreBundle\Repository\DocumentCategoryRepository;
@ -38,6 +39,8 @@ class AccompanyingPeriodContext implements
DocGeneratorContextWithAdminFormInterface, DocGeneratorContextWithAdminFormInterface,
DocGeneratorContextWithPublicFormInterface DocGeneratorContextWithPublicFormInterface
{ {
private BaseContextData $baseContextData;
private DocumentCategoryRepository $documentCategoryRepository; private DocumentCategoryRepository $documentCategoryRepository;
private EntityManagerInterface $em; private EntityManagerInterface $em;
@ -56,7 +59,8 @@ class AccompanyingPeriodContext implements
TranslatableStringHelperInterface $translatableStringHelper, TranslatableStringHelperInterface $translatableStringHelper,
EntityManagerInterface $em, EntityManagerInterface $em,
PersonRender $personRender, PersonRender $personRender,
TranslatorInterface $translator TranslatorInterface $translator,
BaseContextData $baseContextData
) { ) {
$this->documentCategoryRepository = $documentCategoryRepository; $this->documentCategoryRepository = $documentCategoryRepository;
$this->normalizer = $normalizer; $this->normalizer = $normalizer;
@ -64,12 +68,11 @@ class AccompanyingPeriodContext implements
$this->em = $em; $this->em = $em;
$this->personRender = $personRender; $this->personRender = $personRender;
$this->translator = $translator; $this->translator = $translator;
$this->baseContextData = $baseContextData;
} }
public function adminFormReverseTransform(array $data): array public function adminFormReverseTransform(array $data): array
{ {
dump($data);
if (array_key_exists('category', $data)) { if (array_key_exists('category', $data)) {
$data['category'] = [ $data['category'] = [
'idInsideBundle' => $data['category']->getIdInsideBundle(), 'idInsideBundle' => $data['category']->getIdInsideBundle(),
@ -171,6 +174,7 @@ class AccompanyingPeriodContext implements
$options = $template->getOptions(); $options = $template->getOptions();
$data = []; $data = [];
$data = array_merge($data, $this->baseContextData->getData());
$data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']); $data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']);
foreach (['mainPerson', 'person1', 'person2'] as $k) { foreach (['mainPerson', 'person1', 'person2'] as $k) {

View File

@ -56,6 +56,7 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
'placeOfBirth' => '', 'placeOfBirth' => '',
'memo' => '', 'memo' => '',
'numberOfChildren' => '', 'numberOfChildren' => '',
'age' => '@ignored',
]; ];
private NormalizerInterface $normalizer; private NormalizerInterface $normalizer;