improve docgen wip

This commit is contained in:
Julien Fastré 2021-12-10 01:10:55 +01:00
parent e266fa0e5d
commit 9004686a13
14 changed files with 123 additions and 77 deletions

View File

@ -294,8 +294,6 @@ final class DocGeneratorTemplateController extends AbstractController
static function () use ($generatedResource) {
fpassthru($generatedResource);
fclose($generatedResource);
exit();
},
Response::HTTP_OK,
[

View File

@ -46,7 +46,7 @@ class RelatorioDriver implements DriverInterface
'template' => new DataPart($template, $templateName ?? uniqid('template_'), $resourceType),
];
$form = new FormDataPart($formFields);
dump(json_encode($data));
try {
$response = $this->relatorioClient->request('POST', $this->url, [
'headers' => $form->getPreparedHeaders()->toArray(),

View File

@ -38,8 +38,9 @@ class Center implements HasCenterInterface
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @Serializer\Groups({"docgen:read"})
*/
private $id;
private ?int $id = null;
/**
* @ORM\Column(type="string", length=255)

View File

@ -22,7 +22,7 @@ class Civility
{
/**
* @ORM\Column(type="json")
* @Serializer\Groups({"read"})
* @Serializer\Groups({"read", "docgen:read"})
*/
private array $abbreviation = [];
@ -35,13 +35,13 @@ class Civility
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Serializer\Groups({"read"})
* @Serializer\Groups({"read", "docgen:read"})
*/
private $id;
private ?int $id = null;
/**
* @ORM\Column(type="json")
* @Serializer\Groups({"read"})
* @Serializer\Groups({"read", "docgen:read"})
*/
private array $name = [];

View File

@ -28,24 +28,20 @@ use Symfony\Component\Serializer\Annotation\Groups;
class Scope
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @Groups({"read", "docgen:read"})
*/
private $id;
private ?int $id = null;
/**
* translatable names.
*
* @var array
*
* @ORM\Column(type="json")
* @Groups({"read", "docgen:read"})
*/
private $name = [];
private array $name = [];
/**
* @var Collection

View File

@ -55,7 +55,6 @@ class User implements AdvancedUserInterface
* @var string
*
* @ORM\Column(type="string", length=150, nullable=true)
* @Serializer\Groups({"docgen:read"})
*/
private ?string $email = null;
@ -83,7 +82,6 @@ class User implements AdvancedUserInterface
/**
* @ORM\Column(type="string", length=200)
* @Serializer\Groups({"docgen:read"})
*/
private string $label = '';
@ -95,7 +93,6 @@ class User implements AdvancedUserInterface
/**
* @ORM\ManyToOne(targetEntity=Center::class)
* @Serializer\Groups({"docgen:read"})
*/
private ?Center $mainCenter = null;

View File

@ -32,14 +32,14 @@ class UserJob
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @Serializer\Groups({"read"})
* @Serializer\Groups({"read", "docgen:read"})
*/
protected ?int $id = null;
/**
* @var array|string[]A
* @ORM\Column(name="label", type="json")
* @Serializer\Groups({"read"})
* @Serializer\Groups({"read", "docgen:read"})
*/
protected array $label = [];

View File

@ -11,18 +11,31 @@ declare(strict_types=1);
namespace Chill\MainBundle\Serializer\Normalizer;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserJob;
use Chill\MainBundle\Templating\Entity\UserRender;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class UserNormalizer implements NormalizerAwareInterface, NormalizerInterface
class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
private UserRender $userRender;
const NULL_USER = [
'type' => 'user',
'id' => '',
'username' => '',
'text' => '',
'label' => '',
'email' => '',
];
public function __construct(UserRender $userRender)
{
$this->userRender = $userRender;
@ -31,20 +44,42 @@ class UserNormalizer implements NormalizerAwareInterface, NormalizerInterface
public function normalize($user, ?string $format = null, array $context = [])
{
/** @var User $user */
$userJobContext = array_merge(
$context, ['docgen:expects' => UserJob::class, 'groups' => 'docgen:read']);
$scopeContext = array_merge(
$context, ['docgen:expects' => Scope::class, 'groups' => 'docgen:read']);
$centerContext = array_merge(
$context, ['docgen:expects' => Center::class, 'groups' => 'docgen:read']);
if (null === $user && 'docgen' === $format) {
return array_merge(self::NULL_USER, [
'user_job' => $this->normalizer->normalize(null, $format, $userJobContext),
'main_center' => $this->normalizer->normalize(null, $format, $centerContext),
'main_scope' => $this->normalizer->normalize(null, $format, $scopeContext),
]);
}
return [
'type' => 'user',
'id' => $user->getId(),
'username' => $user->getUsername(),
'text' => $this->userRender->renderString($user, []),
'label' => $user->getLabel(),
'user_job' => $this->normalizer->normalize($user->getUserJob(), $format, $context),
'main_center' => $this->normalizer->normalize($user->getMainCenter(), $format, $context),
'main_scope' => $this->normalizer->normalize($user->getMainScope(), $format, $context),
'email' => $user->getEmail(),
'user_job' => $this->normalizer->normalize($user->getUserJob(), $format, $userJobContext),
'main_center' => $this->normalizer->normalize($user->getMainCenter(), $format, $centerContext),
'main_scope' => $this->normalizer->normalize($user->getMainScope(), $format, $scopeContext),
];
}
public function supportsNormalization($data, ?string $format = null): bool
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
return $data instanceof User && ('json' === $format || 'docgen' === $format);
if ($data instanceof User && ('json' === $format || 'docgen' === $format)) {
return true;
}
if (null === $data && 'docgen' === $format && User::class === ($context['docgen:expects'] ?? null)) {
return true;
}
return false;
}
}

View File

@ -16,6 +16,8 @@ use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Security\Resolver\ScopeResolverDispatcher;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\PersonBundle\Templating\Entity\ClosingMotiveRender;
use Chill\PersonBundle\Templating\Entity\SocialIssueRender;
@ -89,17 +91,6 @@ class AccompanyingPeriodDocGenNormalizer implements ContextAwareNormalizerInterf
public function normalize($period, ?string $format = null, array $context = [])
{
if ($period instanceof AccompanyingPeriod) {
$ignored = $context[self::IGNORE_FIRST_PASS_KEY] ?? [];
$ignored[] = spl_object_hash($period);
$initial =
$this->normalizer->normalize($period, $format, array_merge(
$context,
[self::IGNORE_FIRST_PASS_KEY => $ignored, AbstractNormalizer::GROUPS => 'docgen:read']
));
// some transformation
$user = $initial['user'];
unset($initial['user']);
$scopes = $this->scopeResolverDispatcher->isConcerned($period) ? $this->scopeResolverDispatcher->resolveScope($period) : [];
@ -107,28 +98,45 @@ class AccompanyingPeriodDocGenNormalizer implements ContextAwareNormalizerInterf
$scopes = [$scopes];
}
return array_merge(
// get a first default data
$initial,
// and add data custom
[
'intensity' => $this->translator->trans($period->getIntensity()),
'step' => $this->translator->trans('accompanying_period.' . $period->getStep()),
'emergencyText' => $period->isEmergency() ? $this->translator->trans('accompanying_period.emergency') : '',
'confidentialText' => $period->isConfidential() ? $this->translator->trans('confidential') : '',
//'originText' => null !== $period->getOrigin() ? $this->translatableStringHelper->localize($period->getOrigin()->getLabel()) : '',
'closingMotiveText' => null !== $period->getClosingMotive() ?
$this->closingMotiveRender->renderString($period->getClosingMotive(), []) : '',
'ref' => $user,
'socialIssuesText' => implode(', ', array_map(function (SocialIssue $s) {
return $this->socialIssueRender->renderString($s, []);
}, $period->getSocialIssues()->toArray())),
'scopesText' => implode(', ', array_map(function (Scope $s) {
return $this->translatableStringHelper->localize($s->getName());
}, $scopes)),
'scopes' => $scopes,
]
);
$dateContext = array_merge($context, ['docgen:expects' => DateTime::class, 'groups' => 'docgen:read']);
$userContext = array_merge($context, ['docgen:expects' => User::class, 'groups' => 'docgen:read']);
$participationContext = array_merge($context, ['docgen:expects' => AccompanyingPeriodParticipation::class, 'groups' => 'docgen:read']);
return [
'id' => $period->getId(),
'closingDate' => $this->normalizer->normalize($period->getClosingDate(), $format, $dateContext),
'confidential' => $period->isConfidential(),
'createdAt' => $this->normalizer->normalize($period->getCreatedAt(), $format, $dateContext),
'createdBy' => $this->normalizer->normalize($period->getCreatedBy(), $format, $userContext),
'emergency' => $period->isEmergency(),
'openingDate' => $this->normalizer->normalize($period->getOpeningDate(), $format, $dateContext),
'origin' => $this->normalizer->normalize($period->getOrigin(), $format, array_merge($context, ['docgen:expects' => AccompanyingPeriod\Origin::class])),
'participations' => $this->normalizer->normalize($period->getParticipations(), $format, $participationContext),
'currentParticipations' => $this->normalizer->normalize($period->getCurrentParticipations(), $format, $participationContext),
'requestorAnonymous' => $period->isRequestorAnonymous(),
'requestor' => $this->normalizer->normalize($period->getRequestor(), $format, array_merge($context, ['docgen:expects' => Person::class])),
'resources' => $this->normalizer->normalize($period->getResources(), $format, $context),
'scopes' => $this->normalizer->normalize($period->getScopes(), $format, array_merge($context, ['docgen:expects' => Scope::class, 'groups' => 'docgen:read'])),
'socialIssues' => $this->normalizer->normalize($period->getSocialIssues(), $format, $context),
'intensity' => $this->translator->trans($period->getIntensity()),
'step' => $this->translator->trans('accompanying_period.' . $period->getStep()),
'emergencyText' => $period->isEmergency() ? $this->translator->trans('accompanying_period.emergency') : '',
'confidentialText' => $period->isConfidential() ? $this->translator->trans('confidential') : '',
//'originText' => null !== $period->getOrigin() ? $this->translatableStringHelper->localize($period->getOrigin()->getLabel()) : '',
'isClosed' => $period->getClosingDate() !== null,
'closingMotiveText' => null !== $period->getClosingMotive() ?
$this->closingMotiveRender->renderString($period->getClosingMotive(), []) : '',
'ref' => $this->normalizer->normalize($period->getUser(), $format, $userContext),
'hasRef' => $period->getUser() !== null,
'socialIssuesText' => implode(', ', array_map(function (SocialIssue $s) {
return $this->socialIssueRender->renderString($s, []);
}, $period->getSocialIssues()->toArray())),
'scopesText' => implode(', ', array_map(function (Scope $s) {
return $this->translatableStringHelper->localize($s->getName());
}, $scopes)),
//'scopes' => $scopes,
'hasRequestor' => $period->getRequestor() !== null,
];
} elseif (null === $period) {
return self::PERIOD_NULL;
}
@ -143,10 +151,10 @@ class AccompanyingPeriodDocGenNormalizer implements ContextAwareNormalizerInterf
}
if ($data instanceof AccompanyingPeriod) {
if (array_key_exists(self::IGNORE_FIRST_PASS_KEY, $context)
/*if (array_key_exists(self::IGNORE_FIRST_PASS_KEY, $context)
&& in_array(spl_object_hash($data), $context[self::IGNORE_FIRST_PASS_KEY], true)) {
return false;
}
}*/
return true;
}

View File

@ -64,6 +64,7 @@ class PersonDocGenNormalizer implements
}
$data = [
'kind' => 'person',
'firstname' => $person->getFirstName(),
'lastname' => $person->getLastName(),
'altNames' => implode(

View File

@ -171,11 +171,11 @@ class AccompanyingPeriodContext implements
$options = $template->getOptions();
$data = [];
$data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]);
$data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']);
foreach (['mainPerson', 'person1', 'person2'] as $k) {
if ($options[$k]) {
$data[$k] = $this->normalizer->normalize($contextGenerationData[$k], 'docgen', ['docgen:expects' => Person::class]);
$data[$k] = $this->normalizer->normalize($contextGenerationData[$k], 'docgen', ['docgen:expects' => Person::class, 'groups' => 'docgen:read']);
}
}

View File

@ -63,7 +63,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
* @var string
* @ORM\Column(name="acronym", type="string", length=64, nullable=true)
* @Assert\Length(min="2")
* @Groups({"read", "write"})
* @Groups({"read", "write", "docgen:read"})
*/
private ?string $acronym = '';
@ -78,7 +78,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
* @ORM\ManyToOne(targetEntity="\Chill\MainBundle\Entity\Address",
* cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
* @Groups({"read", "write"})
* @Groups({"read", "write", "docgen:read"})
*/
private ?Address $address = null;
@ -97,6 +97,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
* @ORM\JoinTable(name="chill_3party.thirdparty_category",
* joinColumns={@ORM\JoinColumn(name="thirdparty_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")})
* @Groups({"docgen:read"})
*/
private Collection $categories;
@ -121,6 +122,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
* @var Civility
* @ORM\ManyToOne(targetEntity=Civility::class)
* ORM\JoinColumn(name="civility", referencedColumnName="id", nullable=true)
* @Groups({"docgen:read", "read"})
*/
private ?Civility $civility = null;
@ -131,7 +133,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
/**
* @ORM\Column(name="contact_data_anonymous", type="boolean", options={"default": false})
* @Groups({"read"})
* @Groups({"read", "docgen:read"})
*/
private bool $contactDataAnonymous = false;
@ -149,7 +151,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
/**
* @ORM\Column(name="email", type="string", length=255, nullable=true)
* @Assert\Email(checkMX=false)
* @Groups({"read", "write"})
* @Groups({"read", "write", "docgen:read"})
*/
private ?string $email = null;
@ -158,6 +160,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Groups({"read", "write", "docgen:read"})
*/
private ?int $id = null;
@ -171,7 +174,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
* @var string
* @ORM\Column(name="name", type="string", length=255)
* @Assert\Length(min="2")
* @Groups({"read", "write"})
* @Groups({"read", "write", "docgen:read"})
*/
private ?string $name = '';
@ -181,7 +184,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
* @var string
* @ORM\Column(name="name_company", type="string", length=255, nullable=true)
* @Assert\Length(min="3")
* @Groups({"read", "write"})
* @Groups({"read", "write", "docgen:read"})
*/
private ?string $nameCompany = '';
@ -190,7 +193,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
*
* @ORM\ManyToOne(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
* @Groups({"read"})
* @Groups({"read", "docgen:read"})
*/
private ?ThirdParty $parent = null;
@ -200,6 +203,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
* @var ThirdPartyProfession
* @ORM\ManyToOne(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdPartyProfession")
* ORM\JoinColumn(name="profession", referencedColumnName="id", nullable=true)
* @Groups({"docgen:read"})
*/
private ?ThirdPartyProfession $profession = null;
@ -209,7 +213,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
* message="Invalid phone number: it should begin with the international prefix starting with ""+"", hold only digits and be smaller than 20 characters. Ex: +33123456789"
* )
* @PhonenumberConstraint(type="any")
* @Groups({"read", "write"})
* @Groups({"read", "write", "dogen:read"})
*/
private ?string $telephone = null;
@ -495,7 +499,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
}
/**
* @Groups({"read"})
* @Groups({"read", "docgen:read"})
*/
public function isChild(): bool
{

View File

@ -13,6 +13,7 @@ namespace Chill\ThirdPartyBundle\Entity;
use Chill\ThirdPartyBundle\Repository\ThirdPartyCategoryRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
/**
* @ORM\Table(name="chill_3party.party_category")
@ -23,19 +24,21 @@ class ThirdPartyCategory
/**
* @ORM\Column(type="boolean")
*/
private $active = true;
private bool $active = true;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Serializer\Groups({"docgen:read"})
*/
private $id;
private ?int $id = null;
/**
* @ORM\Column(type="json")
* @Serializer\Groups({"docgen:read"})
*/
private $name = [];
private array $name = [];
public function getActive(): ?bool
{

View File

@ -13,6 +13,7 @@ namespace Chill\ThirdPartyBundle\Entity;
use Chill\ThirdPartyBundle\Repository\ThirdPartyProfessionRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
/**
* @ORM\Table(name="chill_3party.party_profession")
@ -23,19 +24,21 @@ class ThirdPartyProfession
/**
* @ORM\Column(type="boolean")
*/
private $active = true;
private bool $active = true;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Serializer\Groups({"docgen:read"})
*/
private $id;
private ?int $id = null;
/**
* @ORM\Column(type="json")
* @Serializer\Groups({"docgen:read"})
*/
private $name = [];
private array $name = [];
public function getActive(): ?bool
{