diff --git a/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php b/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php index 1d00a2a1a..1c1341aa9 100644 --- a/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php +++ b/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php @@ -138,7 +138,12 @@ class ApiController extends AbstractCRUDController return $response; } - return $this->json($entity); + return $this->json( + $entity, + Response::HTTP_OK, + [], + $this->getContextForSerializationPostAlter($action, $request, $_format, $entity) + ); } protected function onAfterValidation(string $action, Request $request, string $_format, $entity, ConstraintViolationListInterface $errors): ?Response @@ -276,7 +281,26 @@ class ApiController extends AbstractCRUDController protected function getContextForSerialization(string $action, Request $request, string $_format, $entity): array { - return []; + switch ($request->getMethod()) { + case Request::METHOD_GET: + return [ 'groups' => [ 'read' ]]; + case Request::METHOD_PUT: + case Request::METHOD_PATCH: + return [ 'groups' => [ 'write' ]]; + default: + throw new \LogicException("get context for serialization is not implemented for this method"); + } + } + + /** + * Get the context for serialization post alter query (in case of + * PATCH, PUT, or POST method) + * + * This is called **after** the entity was altered. + */ + protected function getContextForSerializationPostAlter(string $action, Request $request, string $_format, $entity): array + { + return [ 'groups' => [ 'read' ]]; } /** diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index 1a6a3faae..2ff24cfb7 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -33,6 +33,7 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Context\ExecutionContextInterface; use Chill\MainBundle\Entity\User; +use Symfony\Component\Serializer\Annotation\Groups; /** * AccompanyingPeriod Class @@ -80,6 +81,7 @@ class AccompanyingPeriod * @ORM\Id * @ORM\Column(name="id", type="integer") * @ORM\GeneratedValue(strategy="AUTO") + * @Groups({"read"}) */ private $id; @@ -87,6 +89,7 @@ class AccompanyingPeriod * @var \DateTime * * @ORM\Column(type="date") + * @Groups({"read", "write"}) */ private $openingDate; @@ -94,6 +97,7 @@ class AccompanyingPeriod * @var \DateTime * * @ORM\Column(type="date", nullable=true) + * @Groups({"read", "write"}) */ private $closingDate = null; @@ -101,6 +105,7 @@ class AccompanyingPeriod * @var string * * @ORM\Column(type="text") + * @Groups({"read", "write"}) */ private $remark = ''; @@ -110,6 +115,7 @@ class AccompanyingPeriod * @ORM\OneToMany(targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\Comment", * mappedBy="accompanyingPeriod" * ) + * @Groups({"read"}) */ private $comments; @@ -119,6 +125,7 @@ class AccompanyingPeriod * @ORM\OneToMany(targetEntity=AccompanyingPeriodParticipation::class, * mappedBy="accompanyingPeriod", * cascade={"persist", "refresh", "remove", "merge", "detach"}) + * @Groups({"read"}) */ private $participations; @@ -128,18 +135,21 @@ class AccompanyingPeriod * @ORM\ManyToOne( * targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive") * @ORM\JoinColumn(nullable=true) + * @Groups({"read", "write"}) */ private $closingMotive = null; /** * @ORM\ManyToOne(targetEntity=User::class) * @ORM\JoinColumn(nullable=true) + * @Groups({"read", "write"}) */ private $user; /** * @ORM\ManyToOne(targetEntity=User::class) * @ORM\JoinColumn(nullable=true) + * @Groups({"read"}) */ private $createdBy; @@ -152,12 +162,14 @@ class AccompanyingPeriod /** * @ORM\ManyToOne(targetEntity=Origin::class) * @ORM\JoinColumn(nullable=true) + * @Groups({"read", "write"}) */ private $origin; /** * @var string * @ORM\Column(type="string", nullable=true) + * @Groups({"read", "write"}) */ private $intensity; @@ -172,6 +184,7 @@ class AccompanyingPeriod * joinColumns={@ORM\JoinColumn(name="accompanying_period_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="scope_id", referencedColumnName="id")} * ) + * @Groups({"read"}) */ private $scopes; @@ -190,18 +203,21 @@ class AccompanyingPeriod /** * @var bool * @ORM\Column(type="boolean") + * @Groups({"read", "write"}) */ private $requestorAnonymous = false; /** * @var bool * @ORM\Column(type="boolean") + * @Groups({"read", "write"}) */ private $emergency = false; /** * @var bool * @ORM\Column(type="boolean") + * @Groups({"read", "write"}) */ private $confidential = false; @@ -212,6 +228,7 @@ class AccompanyingPeriod * targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\Resource", * mappedBy="accompanyingPeriod" * ) + * @Groups({"read"}) */ private $resources; @@ -536,6 +553,7 @@ class AccompanyingPeriod /** * @return Person|ThirdParty + * @Groups({"read"}) */ public function getRequestor() { diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php index 55857de4c..442c129eb 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php @@ -24,6 +24,7 @@ namespace Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Repository\AccompanyingPeriod\OriginRepository; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Serializer\Annotation\Groups; /** * @ORM\Entity(repositoryClass=OriginRepository::class) @@ -35,16 +36,19 @@ class Origin * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") + * @Groups({"read"}) */ private $id; /** * @ORM\Column(type="json") + * @Groups({"read"}) */ private $label; /** * @ORM\Column(type="date_immutable", nullable=true) + * @Groups({"read"}) */ private $noActiveAfter; diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodNormalizer.php deleted file mode 100644 index b76cd1b4e..000000000 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodNormalizer.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * 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 . - */ -namespace Chill\PersonBundle\Serializer\Normalizer; - -use Chill\PersonBundle\Entity\AccompanyingPeriod; -use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; -use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; - - -class AccompanyingPeriodNormalizer implements NormalizerInterface, NormalizerAwareInterface { - - protected ?NormalizerInterface $normalizer = null; - - public function normalize($period, string $format = null, array $context = array()) - { - /** @var AccompanyingPeriod $period */ - return [ - 'id' => $period->getId(), - 'openingDate' => $this->normalizer->normalize($period->getOpeningDate(), $format), - 'closingDate' => $this->normalizer->normalize($period->getClosingDate(), $format), - 'remark' => $period->getRemark(), - 'participations' => $this->normalizer->normalize($period->getParticipations(), $format), - 'closingMotive' => $this->normalizer->normalize($period->getClosingMotive(), $format), - 'requestor' => $this->normalizer->normalize($period->getRequestor(), $format), - 'requestorAnonymous' => $this->normalizer->normalize($period->isRequestorAnonymous(), $format), - 'user' => $this->normalizer->normalize($period->getUser(), $format), - 'step' => $period->getStep(), - 'origin' => $this->normalizer->normalize($period->getOrigin(), $format), - 'intensity' => $period->getIntensity(), - 'emergency' => $period->isEmergency(), - 'confidential' => $period->isConfidential() - ]; - } - - public function supportsNormalization($data, string $format = null): bool - { - return $data instanceof AccompanyingPeriod; - } - - public function setNormalizer(NormalizerInterface $normalizer) - { - $this->normalizer = $normalizer; - } -} diff --git a/src/Bundle/ChillPersonBundle/config/services/serializer.yaml b/src/Bundle/ChillPersonBundle/config/services/serializer.yaml index 843c984f8..0925cc2ed 100644 --- a/src/Bundle/ChillPersonBundle/config/services/serializer.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/serializer.yaml @@ -7,10 +7,6 @@ services: tags: - { name: 'serializer.normalizer', priority: 64 } - Chill\PersonBundle\Serializer\Normalizer\AccompanyingPeriodNormalizer: - tags: - - { name: 'serializer.normalizer', priority: 64 } - Chill\PersonBundle\Serializer\Normalizer\AccompanyingPeriodParticipationNormalizer: tags: - { name: 'serializer.normalizer', priority: 64 }