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,20 +1,10 @@
<?php
/*
/**
* Chill is a software for social workers
*
* Copyright (C) 2014-2021, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Serializer\Normalizer;
@@ -23,23 +13,22 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod\Origin;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
*
* @internal we keep this normalizer, because the property 'text' may be replace by a rendering in the future
*/
class AccompanyingPeriodOriginNormalizer implements NormalizerInterface
{
public function normalize($origin, string $format = null, array $context = array())
public function normalize($origin, ?string $format = null, array $context = [])
{
/** @var Origin $origin */
return [
'type' => 'origin',
'id' => $origin->getId(),
'label' => $origin->getLabel(),
'text' => $origin->getLabel()
'text' => $origin->getLabel(),
];
}
public function supportsNormalization($data, string $format = null): bool
public function supportsNormalization($data, ?string $format = null): bool
{
return $data instanceof Origin;
}

View File

@@ -1,52 +1,42 @@
<?php
/*
/**
* Chill is a software for social workers
*
* Copyright (C) 2014-2021, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
class AccompanyingPeriodParticipationNormalizer implements NormalizerInterface, NormalizerAwareInterface {
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class AccompanyingPeriodParticipationNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
protected ?NormalizerInterface $normalizer = null;
public function normalize($participation, string $format = null, array $context = array())
public function normalize($participation, ?string $format = null, array $context = [])
{
/** @var AccompanyingPeriodParticipation $participation */
return [
'id' => $participation->getId(),
'startDate' => $this->normalizer->normalize($participation->getStartDate(), $format),
'endDate' => $this->normalizer->normalize($participation->getEndDate(), $format),
'person' => $this->normalizer->normalize($participation->getPerson(), $format)
'person' => $this->normalizer->normalize($participation->getPerson(), $format),
];
}
public function supportsNormalization($data, string $format = null): bool
{
return false;
return $data instanceof AccompanyingPeriodParticipation;
}
public function setNormalizer(NormalizerInterface $normalizer)
{
$this->normalizer = $normalizer;
}
public function supportsNormalization($data, ?string $format = null): bool
{
return false;
return $data instanceof AccompanyingPeriodParticipation;
}
}

View File

@@ -1,23 +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\PersonBundle\Serializer\Normalizer;
use Chill\PersonBundle\Entity\Person;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\MainBundle\Serializer\Normalizer\DiscriminatedObjectDenormalizer;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\AccompanyingPeriod\ResourceRepository;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Symfony\Component\Serializer\Exception;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
use Symfony\Component\Serializer\Exception;
use Chill\MainBundle\Serializer\Normalizer\DiscriminatedObjectDenormalizer;
use function array_key_exists;
use function array_merge;
class AccompanyingPeriodResourceNormalizer implements DenormalizerInterface, DenormalizerAwareInterface
{
use DenormalizerAwareTrait;
use ObjectToPopulateTrait;
private ResourceRepository $repository;
@@ -27,59 +34,57 @@ class AccompanyingPeriodResourceNormalizer implements DenormalizerInterface, Den
$this->repository = $repository;
}
public function denormalize($data, string $type, string $format = null, array $context = [])
public function denormalize($data, string $type, ?string $format = null, array $context = [])
{
$resource = $this->extractObjectToPopulate($type, $context);
if ('accompanying_period_resource' !== ($data['type'] ?? NULL)) {
if ('accompanying_period_resource' !== ($data['type'] ?? null)) {
throw new Exception\InvalidArgumentException("the key type must be present in data and set to 'accompanying_period_resource'");
}
if ($resource === NULL && \array_key_exists('id', $data)) {
if (null === $resource && array_key_exists('id', $data)) {
$resource = $this->repository->find($data['id']);
if (NULL === $resource) {
throw new Exception\UnexpectedValueException(sprintf("the resource with".
"id %d is not found", $data['id']));
if (null === $resource) {
throw new Exception\UnexpectedValueException(sprintf('the resource with' .
'id %d is not found', $data['id']));
}
// if resource found, available only for read-only
if (count($data) > 2) {
unset($data['id']);
unset($data['type']);
unset($data['id'], $data['type']);
throw new Exception\ExtraAttributesException($data);
}
}
if ($resource === NULL) {
if (null === $resource) {
$resource = new Resource();
}
if (\array_key_exists('resource', $data)) {
if (array_key_exists('resource', $data)) {
$res = $this->denormalizer->denormalize(
$data['resource'],
DiscriminatedObjectDenormalizer::TYPE,
$format,
\array_merge(
array_merge(
$context,
[
DiscriminatedObjectDenormalizer::ALLOWED_TYPES =>
[
Person::class, ThirdParty::class
]
[
DiscriminatedObjectDenormalizer::ALLOWED_TYPES => [
Person::class, ThirdParty::class,
],
]
)
);
$resource->setResource($res);
}
}
return $resource;
}
public function supportsDenormalization($data, string $type, string $format = null)
public function supportsDenormalization($data, string $type, ?string $format = null)
{
return $type === Resource::class;
}
return Resource::class === $type;
}
}

View File

@@ -1,29 +1,27 @@
<?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\PersonBundle\Serializer\Normalizer;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\SocialWork\Goal;
use Chill\PersonBundle\Entity\SocialWork\Result;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Serializer\Exception\BadMethodCallException;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
use function array_key_exists;
use function array_merge;
use function in_array;
use function is_array;
/**
* This denormalizer rely on AbstractNormalizer for most of the job, and
@@ -34,15 +32,14 @@ class AccompanyingPeriodWorkDenormalizer implements DenormalizerAwareInterface,
use DenormalizerAwareTrait;
use ObjectToPopulateTrait;
private AccompanyingPeriodWorkRepository $workRepository;
public const GROUP_CREATE = 'accompanying_period_work:create';
public const GROUP_EDIT = 'accompanying_period_work:edit';
private EntityManagerInterface $em;
public const GROUP_CREATE = 'accompanying_period_work:create';
public const GROUP_EDIT = 'accompanying_period_work:edit';
private AccompanyingPeriodWorkRepository $workRepository;
/**
* @param AccompanyingPeriodWorkRepository $workRepository
*/
public function __construct(
AccompanyingPeriodWorkRepository $workRepository,
EntityManagerInterface $em
@@ -51,37 +48,46 @@ class AccompanyingPeriodWorkDenormalizer implements DenormalizerAwareInterface,
$this->em = $em;
}
/**
* @inheritDoc
*/
public function denormalize($data, string $type, string $format = null, array $context = [])
public function denormalize($data, string $type, ?string $format = null, array $context = [])
{
$work = $this->denormalizer->denormalize($data, $type, $format, \array_merge($context,
['skip' => self::class]));
$work = $this->denormalizer->denormalize($data, $type, $format, array_merge(
$context,
['skip' => self::class]
));
if (\in_array('accompanying_period_work:edit', $context['groups'] ?? [])) {
if (in_array('accompanying_period_work:edit', $context['groups'] ?? [])) {
$this->handleEvaluationCollection($data, $work, $format, $context);
}
return $work;
}
public function supportsDenormalization($data, string $type, ?string $format = null, array $context = []): bool
{
return AccompanyingPeriodWork::class === $type
&& self::class !== ($context['skip'] ?? null)
&& is_array($data)
&& array_key_exists('type', $data)
&& 'accompanying_period_work' === $data['type'];
}
private function handleEvaluationCollection(array $data, AccompanyingPeriodWork $work, string $format, array $context)
{
$dataById = [];
$dataWithoutId = [];
foreach ($data['accompanyingPeriodWorkEvaluations'] as $e) {
if (\array_key_exists('id', $e)) {
$dataById[$e['id']] = $e;
} else {
$dataWithoutId[] = $e;
}
if (array_key_exists('id', $e)) {
$dataById[$e['id']] = $e;
} else {
$dataWithoutId[] = $e;
}
}
// partition the separate kept evaluations and removed one
list($kept, $removed) = $work->getAccompanyingPeriodWorkEvaluations()
[$kept, $removed] = $work->getAccompanyingPeriodWorkEvaluations()
->partition(
fn(int $key, AccompanyingPeriodWorkEvaluation $a) => \array_key_exists($a->getId(), $dataById)
fn (int $key, AccompanyingPeriodWorkEvaluation $a) => array_key_exists($a->getId(), $dataById)
);
// remove the evaluations from work
@@ -92,13 +98,13 @@ class AccompanyingPeriodWorkDenormalizer implements DenormalizerAwareInterface,
foreach ($kept as $k) {
$this->denormalizer->denormalize(
$dataById[$k->getId()],
AccompanyingPeriodWorkEvaluation::class,
AccompanyingPeriodWorkEvaluation::class,
$format,
\array_merge(
array_merge(
$context,
[
'groups' => [ 'write' ],
AbstractNormalizer::OBJECT_TO_POPULATE => $k
'groups' => ['write'],
AbstractNormalizer::OBJECT_TO_POPULATE => $k,
]
)
);
@@ -109,24 +115,12 @@ class AccompanyingPeriodWorkDenormalizer implements DenormalizerAwareInterface,
$newData,
AccompanyingPeriodWorkEvaluation::class,
$format,
\array_merge($context, ['groups' => [ 'accompanying_period_work_evaluation:create']]
array_merge(
$context,
['groups' => ['accompanying_period_work_evaluation:create']]
)
);
$work->addAccompanyingPeriodWorkEvaluation($evaluation);
}
}
/**
* @inheritDoc
*/
public function supportsDenormalization($data, string $type, string $format = null, array $context = []): bool
{
return $type === AccompanyingPeriodWork::class
&& ($context['skip'] ?? null) !== self::class
&& \is_array($data)
&& \array_key_exists("type", $data)
&& $data["type"] === 'accompanying_period_work';
}
}

View File

@@ -1,31 +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.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\PersonBundle\Entity\Household\Position;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Household\Position;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Household\MembersEditor;
use Chill\PersonBundle\Household\MembersEditorFactory;
use DateTimeImmutable;
use Symfony\Component\Serializer\Exception;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Exception;
use Chill\PersonBundle\Household\MembersEditorFactory;
use Chill\PersonBundle\Household\MembersEditor;
use function array_key_exists;
class MembersEditorNormalizer implements DenormalizerInterface, DenormalizerAwareInterface
{
private MembersEditorFactory $factory;
use DenormalizerAwareTrait;
private MembersEditorFactory $factory;
public function __construct(MembersEditorFactory $factory)
{
$this->factory = $factory;
}
public function denormalize($data, string $type, string $format = null, array $context = [])
public function denormalize($data, string $type, ?string $format = null, array $context = [])
{
// some test about schema first...
$this->performChecks($data);
@@ -33,23 +42,16 @@ class MembersEditorNormalizer implements DenormalizerInterface, DenormalizerAwar
// route to "leave movement" (all concerned leave household)
// or "move to another household" (all concerned go to another
// household)
if (NULL === $data['destination']) {
if (null === $data['destination']) {
return $this->denormalizeLeave($data, $type, $format, $context);
}
return $this->denormalizeMove($data, $type, $format, $context);
}
private function performChecks($data): void
public function supportsDenormalization($data, string $type, ?string $format = null)
{
if (NULL == $data['concerned'] ?? NULL
&& FALSE === ·\is_array('concerned')) {
throw new Exception\UnexpectedValueException("The schema does not have any key 'concerned'");
}
if (FALSE === \array_key_exists('destination', $data)) {
throw new Exception\UnexpectedValueException("The schema does not have any key 'destination'");
}
return MembersEditor::class === $type;
}
protected function denormalizeLeave($data, string $type, string $format, array $context = [])
@@ -57,18 +59,26 @@ class MembersEditorNormalizer implements DenormalizerInterface, DenormalizerAwar
$editor = $this->factory->createEditor(null);
foreach ($data['concerned'] as $key => $concerned) {
$person = $this->denormalizer->denormalize($concerned['person'] ?? null, Person::class,
$format, $context);
$startDate = $this->denormalizer->denormalize($concerned['start_date'] ?? null, \DateTimeImmutable::class,
$format, $context);
$person = $this->denormalizer->denormalize(
$concerned['person'] ?? null,
Person::class,
$format,
$context
);
$startDate = $this->denormalizer->denormalize(
$concerned['start_date'] ?? null,
DateTimeImmutable::class,
$format,
$context
);
if (
NULL === $person
&& NULL === $startDate
null === $person
&& null === $startDate
) {
throw new Exception\InvalidArgumentException("position with ".
"key $key could not be denormalized: missing ".
"person or start_date.");
throw new Exception\InvalidArgumentException('position with ' .
"key {$key} could not be denormalized: missing " .
'person or start_date.');
}
$editor->leaveMovement($startDate, $person);
@@ -82,46 +92,73 @@ class MembersEditorNormalizer implements DenormalizerInterface, DenormalizerAwar
$householdContext = $context;
$householdContext['groups'][] = 'create';
$household = $this->denormalizer->denormalize($data['destination'], Household::class,
$format, $householdContext);
$household = $this->denormalizer->denormalize(
$data['destination'],
Household::class,
$format,
$householdContext
);
if (NULL === $household) {
throw new Exception\InvalidArgumentException("household could not be denormalized. Impossible to process");
if (null === $household) {
throw new Exception\InvalidArgumentException('household could not be denormalized. Impossible to process');
}
$editor = $this->factory->createEditor($household);
foreach ($data['concerned'] as $key => $concerned) {
$person = $this->denormalizer->denormalize($concerned['person'] ?? null, Person::class,
$format, $context);
$position = $this->denormalizer->denormalize($concerned['position'] ?? null, Position::class,
$format, $context);
$startDate = $this->denormalizer->denormalize($concerned['start_date'] ?? null, \DateTimeImmutable::class,
$format, $context);
$person = $this->denormalizer->denormalize(
$concerned['person'] ?? null,
Person::class,
$format,
$context
);
$position = $this->denormalizer->denormalize(
$concerned['position'] ?? null,
Position::class,
$format,
$context
);
$startDate = $this->denormalizer->denormalize(
$concerned['start_date'] ?? null,
DateTimeImmutable::class,
$format,
$context
);
$holder = (bool) $concerned['holder'] ?? false;
$comment = (string) $concerned['comment'] ?? false;
if (
NULL === $person
&& NULL === $position
&& NULL === $startDate
null === $person
&& null === $position
&& null === $startDate
) {
throw new Exception\InvalidArgumentException("position with ".
"key $key could not be denormalized: missing ".
"person, position or start_date.");
throw new Exception\InvalidArgumentException('position with ' .
"key {$key} could not be denormalized: missing " .
'person, position or start_date.');
}
$editor->addMovement($startDate, $person, $position, $holder,
$comment);
$editor->addMovement(
$startDate,
$person,
$position,
$holder,
$comment
);
}
return $editor;
}
public function supportsDenormalization($data, string $type, string $format = null)
private function performChecks($data): void
{
return $type === MembersEditor::class;
}
if (null == $data['concerned'] ?? null
&& false === ·\is_array('concerned')) {
throw new Exception\UnexpectedValueException("The schema does not have any key 'concerned'");
}
if (false === array_key_exists('destination', $data)) {
throw new Exception\UnexpectedValueException("The schema does not have any key 'destination'");
}
}
}

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\PersonBundle\Serializer\Normalizer;
use Chill\DocGeneratorBundle\Serializer\Helper\NormalizeNullValueHelper;
@@ -7,32 +14,27 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\PersonAltName;
use Chill\PersonBundle\Templating\Entity\PersonRender;
use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use DateTimeInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use function array_key_exists;
use function array_map;
use function implode;
class PersonDocGenNormalizer implements
ContextAwareNormalizerInterface,
NormalizerAwareInterface
{
use NormalizerAwareTrait;
private PersonRender $personRender;
private TranslatorInterface $translator;
private TranslatableStringHelper $translatableStringHelper;
/**
* @param PersonRender $personRender
* @param TranslatorInterface $translator
* @param TranslatableStringHelper $translatableStringHelper
*/
private TranslatorInterface $translator;
public function __construct(
PersonRender $personRender,
TranslatorInterface $translator,
@@ -43,12 +45,11 @@ class PersonDocGenNormalizer implements
$this->translatableStringHelper = $translatableStringHelper;
}
public function normalize($person, string $format = null, array $context = [])
public function normalize($person, ?string $format = null, array $context = [])
{
/** @var Person $person */
$dateContext = $context;
$dateContext['docgen:expects'] = \DateTimeInterface::class;
$dateContext['docgen:expects'] = DateTimeInterface::class;
if (null === $person) {
return $this->normalizeNullValue($format, $context);
@@ -57,9 +58,9 @@ class PersonDocGenNormalizer implements
return [
'firstname' => $person->getFirstName(),
'lastname' => $person->getLastName(),
'altNames' => \implode(
'altNames' => implode(
', ',
\array_map(
array_map(
function (PersonAltName $altName) {
return $altName->getLabel();
},
@@ -83,34 +84,34 @@ class PersonDocGenNormalizer implements
];
}
private function normalizeNullValue(string $format, array $context)
public function supportsNormalization($data, ?string $format = null, array $context = [])
{
$normalizer = new NormalizeNullValueHelper($this->normalizer);
$attributes = [
'firstname', 'lastname', 'altNames', 'text',
'birthdate' => \DateTimeInterface::class,
'deathdate' => \DateTimeInterface::class,
'gender', 'maritalStatus',
'maritalStatusDate' => \DateTimeInterface::class,
'email', 'firstPhoneNumber', 'fixPhoneNumber', 'mobilePhoneNumber', 'nationality',
'placeOfBirth', 'memo', 'numberOfChildren'
];
return $normalizer->normalize($attributes, $format, $context);
}
public function supportsNormalization($data, string $format = null, array $context = [])
{
if ($format !== 'docgen') {
if ('docgen' !== $format) {
return false;
}
return
$data instanceof Person
|| (
\array_key_exists('docgen:expects', $context)
&& $context['docgen:expects'] === Person::class
array_key_exists('docgen:expects', $context)
&& Person::class === $context['docgen:expects']
);
}
private function normalizeNullValue(string $format, array $context)
{
$normalizer = new NormalizeNullValueHelper($this->normalizer);
$attributes = [
'firstname', 'lastname', 'altNames', 'text',
'birthdate' => DateTimeInterface::class,
'deathdate' => DateTimeInterface::class,
'gender', 'maritalStatus',
'maritalStatusDate' => DateTimeInterface::class,
'email', 'firstPhoneNumber', 'fixPhoneNumber', 'mobilePhoneNumber', 'nationality',
'placeOfBirth', 'memo', 'numberOfChildren',
];
return $normalizer->normalize($attributes, $format, $context);
}
}

View File

@@ -1,44 +1,34 @@
<?php
/*
/**
* Chill is a software for social workers
*
* Copyright (C) 2014-2021, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher;
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
use Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\PersonRepository;
use DateTime;
use LogicException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Chill\PersonBundle\Repository\PersonRepository;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
use function array_key_exists;
/**
* Serialize a Person entity
*
* Serialize a Person entity.
*/
class PersonJsonNormalizer implements
NormalizerInterface,
@@ -46,17 +36,15 @@ class PersonJsonNormalizer implements
DenormalizerInterface,
DenormalizerAwareInterface
{
private ChillEntityRenderExtension $render;
private PersonRepository $repository;
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
use ObjectToPopulateTrait;
private CenterResolverManagerInterface $centerResolverManager;
use NormalizerAwareTrait;
private ChillEntityRenderExtension $render;
use ObjectToPopulateTrait;
use DenormalizerAwareTrait;
private PersonRepository $repository;
public function __construct(
ChillEntityRenderExtension $render,
@@ -68,7 +56,92 @@ class PersonJsonNormalizer implements
$this->centerResolverManager = $centerResolverManager;
}
public function normalize($person, string $format = null, array $context = [])
public function denormalize($data, string $type, ?string $format = null, array $context = [])
{
$person = $this->extractObjectToPopulate($type, $context);
if (array_key_exists('id', $data)) {
$person = $this->repository->find($data['id']);
if (null === $person) {
throw new UnexpectedValueException("The person with id \"{$data['id']}\" does " .
'not exists');
}
// currently, not allowed to update a person through api
// if instantiated with id
return $person;
}
if (null === $person) {
$person = new Person();
}
foreach (['firstName', 'lastName', 'phonenumber', 'mobilenumber', 'gender',
'birthdate', 'deathdate', 'center', ]
as $item) {
if (!array_key_exists($item, $data)) {
continue;
}
switch ($item) {
case 'firstName':
$person->setFirstName($data[$item]);
break;
case 'lastName':
$person->setLastName($data[$item]);
break;
case 'phonenumber':
$person->setPhonenumber($data[$item]);
break;
case 'mobilenumber':
$person->setMobilenumber($data[$item]);
break;
case 'gender':
$person->setGender($data[$item]);
break;
case 'birthdate':
$object = $this->denormalizer->denormalize($data[$item], DateTime::class, $format, $context);
if ($object instanceof DateTime) {
$person->setBirthdate($object);
}
break;
case 'deathdate':
$object = $this->denormalizer->denormalize($data[$item], DateTime::class, $format, $context);
if ($object instanceof DateTime) {
$person->setDeathdate($object);
}
break;
case 'center':
$object = $this->denormalizer->denormalize($data[$item], Center::class, $format, $context);
$person->setCenter($object);
break;
default:
throw new LogicException("item not defined: {$item}");
}
}
return $person;
}
public function normalize($person, ?string $format = null, array $context = [])
{
/** @var Household $household */
$household = $person->getCurrentHousehold();
@@ -92,93 +165,24 @@ class PersonJsonNormalizer implements
];
}
public function supportsDenormalization($data, string $type, ?string $format = null)
{
return Person::class === $type && 'person' === ($data['type'] ?? null);
}
public function supportsNormalization($data, ?string $format = null): bool
{
return $data instanceof Person && 'json' === $format;
}
protected function normalizeAltNames($altNames): array
{
$r = [];
foreach ($altNames as $n) {
$r[] = [ 'key' => $n->getKey(), 'label' => $n->getLabel() ];
$r[] = ['key' => $n->getKey(), 'label' => $n->getLabel()];
}
return $r;
}
public function supportsNormalization($data, string $format = null): bool
{
return $data instanceof Person && $format === 'json';
}
public function denormalize($data, string $type, string $format = null, array $context = [])
{
$person = $this->extractObjectToPopulate($type, $context);
if (\array_key_exists('id', $data)) {
$person = $this->repository->find($data['id']);
if (null === $person) {
throw new UnexpectedValueException("The person with id \"{$data['id']}\" does ".
"not exists");
}
// currently, not allowed to update a person through api
// if instantiated with id
return $person;
}
if (null === $person) {
$person = new Person();
}
foreach (['firstName', 'lastName', 'phonenumber', 'mobilenumber', 'gender',
'birthdate', 'deathdate', 'center']
as $item) {
if (!\array_key_exists($item, $data)) {
continue;
}
switch ($item) {
case 'firstName':
$person->setFirstName($data[$item]);
break;
case 'lastName':
$person->setLastName($data[$item]);
break;
case 'phonenumber':
$person->setPhonenumber($data[$item]);
break;
case 'mobilenumber':
$person->setMobilenumber($data[$item]);
break;
case 'gender':
$person->setGender($data[$item]);
break;
case 'birthdate':
$object = $this->denormalizer->denormalize($data[$item], \DateTime::class, $format, $context);
if ($object instanceof \DateTime) {
$person->setBirthdate($object);
}
break;
case 'deathdate':
$object = $this->denormalizer->denormalize($data[$item], \DateTime::class, $format, $context);
if ($object instanceof \DateTime) {
$person->setDeathdate($object);
}
break;
case 'center':
$object = $this->denormalizer->denormalize($data[$item], Center::class, $format, $context);
$person->setCenter($object);
break;
default:
throw new \LogicException("item not defined: $item");
}
}
return $person;
}
public function supportsDenormalization($data, string $type, string $format = null)
{
return $type === Person::class && ($data['type'] ?? NULL) === 'person';
}
}

View File

@@ -1,13 +1,19 @@
<?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\PersonBundle\Serializer\Normalizer;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Chill\PersonBundle\Templating\Entity\SocialActionRender;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class SocialActionNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
@@ -20,10 +26,7 @@ class SocialActionNormalizer implements NormalizerInterface, NormalizerAwareInte
$this->render = $render;
}
/**
* {@inheritDoc}
*/
public function normalize($socialAction, string $format = null, array $context = [])
public function normalize($socialAction, ?string $format = null, array $context = [])
{
return [
'id' => $socialAction->getId(),
@@ -31,14 +34,11 @@ class SocialActionNormalizer implements NormalizerInterface, NormalizerAwareInte
'text' => $this->render->renderString($socialAction, []),
'parent' => $this->normalizer->normalize($socialAction->getParent()),
'desactivationDate' => $this->normalizer->normalize($socialAction->getDesactivationDate()),
'title' => $socialAction->getTitle()
'title' => $socialAction->getTitle(),
];
}
/**
* {@inheritDoc}
*/
public function supportsNormalization($data, string $format = null)
public function supportsNormalization($data, ?string $format = null)
{
return $data instanceof SocialAction;
}

View File

@@ -1,25 +1,32 @@
<?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\PersonBundle\Serializer\Normalizer;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Chill\PersonBundle\Templating\Entity\SocialIssueRender;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Chill\PersonBundle\Templating\Entity\SocialIssueRender;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class SocialIssueNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
private SocialIssueRender $render;
use NormalizerAwareTrait;
private SocialIssueRender $render;
public function __construct(SocialIssueRender $render)
{
$this->render = $render;
}
public function normalize($socialIssue, string $format = null, array $context = [])
public function normalize($socialIssue, ?string $format = null, array $context = [])
{
/** @var SocialIssue $socialIssue */
return [
@@ -28,11 +35,11 @@ class SocialIssueNormalizer implements NormalizerInterface, NormalizerAwareInter
'parent_id' => $socialIssue->hasParent() ? $socialIssue->getParent()->getId() : null,
'children_ids' => $socialIssue->getChildren()->map(function (SocialIssue $si) { return $si->getId(); }),
'title' => $socialIssue->getTitle(),
'text' => $this->render->renderString($socialIssue, [])
'text' => $this->render->renderString($socialIssue, []),
];
}
public function supportsNormalization($data, string $format = null): bool
public function supportsNormalization($data, ?string $format = null): bool
{
return $data instanceof SocialIssue;
}