Docgen/add base context

This commit is contained in:
Julien Fastré 2021-12-15 22:43:06 +00:00
parent 6501a0148e
commit d4a5735e15
11 changed files with 151 additions and 18 deletions

View File

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

View File

@ -0,0 +1,53 @@
<?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\Service\Context;
use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class BaseContextData
{
private NormalizerInterface $normalizer;
private Security $security;
public function __construct(Security $security, NormalizerInterface $normalizer)
{
$this->security = $security;
$this->normalizer = $normalizer;
}
public function getData(): array
{
$data = [];
$user = $this->security->getUser();
$data['creator'] = $this->normalizer->normalize(
$user instanceof User ? $user : null,
'docgen',
['docgen:expects' => User::class, 'groups' => ['docgen:read']]
);
$data['createdAt'] = $this->normalizer->normalize(new DateTimeImmutable(), 'docgen', [
'docgen:expects' => DateTimeImmutable::class, 'groups' => ['docgen:read'],
]);
$data['location'] = $this->normalizer->normalize(
$user instanceof User ? $user->getCurrentLocation() : null,
'docgen',
['docgen:expects' => Location::class, 'groups' => ['docgen:read']]
);
return $data;
}
}

View File

@ -34,6 +34,11 @@ services:
autowire: true
autoconfigure: true
Chill\DocGeneratorBundle\Service\Context\:
resource: "../Service/Context/"
autowire: true
autoconfigure: true
Chill\DocGeneratorBundle\GeneratorDriver\:
resource: "../GeneratorDriver/"
autowire: true

View File

@ -0,0 +1,68 @@
<?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\tests\Service\Context;
use Chill\DocGeneratorBundle\Service\Context\BaseContextData;
use Chill\MainBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* @internal
* @coversNothing
*/
final class BaseContextDataTest extends KernelTestCase
{
protected function setUp()
{
parent::setUp();
self::bootKernel();
}
public function testGenerateNoContext()
{
$context = $this->buildBaseContext();
$actual = $context->getData();
$this->assertIsArray($actual);
$this->assertArrayHasKey('creator', $actual);
$this->assertArrayHasKey('createdAt', $actual);
$this->assertArrayHasKey('location', $actual);
}
public function testGenerateWithUser()
{
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn(new User());
$context = $this->buildBaseContext($security->reveal());
$actual = $context->getData();
$this->assertIsArray($actual);
$this->assertArrayHasKey('creator', $actual);
$this->assertArrayHasKey('createdAt', $actual);
$this->assertArrayHasKey('location', $actual);
}
private function buildBaseContext(
?Security $security = null,
?NormalizerInterface $normalizer = null
): BaseContextData {
return new BaseContextData(
$security ?? self::$container->get(Security::class),
$normalizer ?? self::$container->get(NormalizerInterface::class)
);
}
}

View File

@ -25,10 +25,10 @@ class LocationApiController extends ApiController
->leftJoin('e.locationType', 'lt')
->andWhere(
$query->expr()->andX(
$query->expr()->eq('e.availableForUsers', "'TRUE'"),
$query->expr()->eq('lt.availableForUsers', "'TRUE'"),
$query->expr()->eq('e.active', "'TRUE'"),
)
$query->expr()->eq('e.availableForUsers', "'TRUE'"),
$query->expr()->eq('lt.availableForUsers', "'TRUE'"),
$query->expr()->eq('e.active', "'TRUE'"),
)
);
}
}

View File

@ -40,7 +40,7 @@ class Location implements TrackCreationInterface, TrackUpdateInterface
/**
* @ORM\ManyToOne(targetEntity=Address::class, cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
* @Serializer\Groups({"read", "write"})
* @Serializer\Groups({"read", "write", "docgen:read"})
*/
private ?Address $address = null;
@ -72,26 +72,26 @@ class Location implements TrackCreationInterface, TrackUpdateInterface
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Serializer\Groups({"read"})
* @Serializer\Groups({"read", "docgen:read"})
*/
private ?int $id = null;
/**
* @ORM\ManyToOne(targetEntity=LocationType::class)
* @ORM\JoinColumn(nullable=false)
* @Serializer\Groups({"read", "write"})
* @Serializer\Groups({"read", "write", "docgen:read"})
*/
private ?LocationType $locationType = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Serializer\Groups({"read", "write"})
* @Serializer\Groups({"read", "write", "docgen:read"})
*/
private ?string $name = null;
/**
* @ORM\Column(type="string", length=64, nullable=true)
* @Serializer\Groups({"read", "write"})
* @Serializer\Groups({"read", "write", "docgen:read"})
* @Assert\Regex(pattern="/^([\+{1}])([0-9\s*]{4,20})$/")
* @PhonenumberConstraint(type="any")
*/
@ -99,7 +99,7 @@ class Location implements TrackCreationInterface, TrackUpdateInterface
/**
* @ORM\Column(type="string", length=64, nullable=true)
* @Serializer\Groups({"read", "write"})
* @Serializer\Groups({"read", "write", "docgen:read"})
* @Assert\Regex(pattern="/^([\+{1}])([0-9\s*]{4,20})$/")
* @PhonenumberConstraint(type="any")
*/

View File

@ -71,13 +71,14 @@ class LocationType
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Serializer\Groups({"read"})
* @Serializer\Groups({"read", "docgen:read"})
*/
private ?int $id = null;
/**
* @ORM\Column(type="json")
* @Serializer\Groups({"read"})
* @Serializer\Groups({"read", "docgen:read"})
* @Serializer\Context({"is-translatable": true}, groups={"docgen:read"})
*/
private array $title = [];

View File

@ -74,7 +74,7 @@ class PersonDocGenNormalizer implements
$data = [
'type' => 'person',
'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(),
'lastname' => $person->getLastName(),
'altNames' => implode(
@ -87,6 +87,7 @@ class PersonDocGenNormalizer implements
)
),
'text' => $this->personRender->renderString($person, []),
'age' => $person->getAge(),
'birthdate' => $this->normalizer->normalize($person->getBirthdate(), $format, $dateContext),
'deathdate' => $this->normalizer->normalize($person->getDeathdate(), $format, $dateContext),
'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\Exception\UnexpectedTypeException;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocGeneratorBundle\Service\Context\BaseContextData;
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Repository\DocumentCategoryRepository;
@ -38,6 +39,8 @@ class AccompanyingPeriodContext implements
DocGeneratorContextWithAdminFormInterface,
DocGeneratorContextWithPublicFormInterface
{
private BaseContextData $baseContextData;
private DocumentCategoryRepository $documentCategoryRepository;
private EntityManagerInterface $em;
@ -56,7 +59,8 @@ class AccompanyingPeriodContext implements
TranslatableStringHelperInterface $translatableStringHelper,
EntityManagerInterface $em,
PersonRender $personRender,
TranslatorInterface $translator
TranslatorInterface $translator,
BaseContextData $baseContextData
) {
$this->documentCategoryRepository = $documentCategoryRepository;
$this->normalizer = $normalizer;
@ -64,12 +68,11 @@ class AccompanyingPeriodContext implements
$this->em = $em;
$this->personRender = $personRender;
$this->translator = $translator;
$this->baseContextData = $baseContextData;
}
public function adminFormReverseTransform(array $data): array
{
dump($data);
if (array_key_exists('category', $data)) {
$data['category'] = [
'idInsideBundle' => $data['category']->getIdInsideBundle(),
@ -171,6 +174,7 @@ class AccompanyingPeriodContext implements
$options = $template->getOptions();
$data = [];
$data = array_merge($data, $this->baseContextData->getData());
$data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']);
foreach (['mainPerson', 'person1', 'person2'] as $k) {

View File

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

View File

@ -406,8 +406,6 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
/**
* Get email.
*
* @return string|null
*/
public function getEmail(): ?string
{