Use annoations for serialization

This commit is contained in:
Julien Fastré 2021-05-11 19:49:41 +02:00
parent 3f64db3b3a
commit a4989f99d6
5 changed files with 48 additions and 67 deletions

View File

@ -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' ]];
}
/**

View File

@ -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()
{

View File

@ -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;

View File

@ -1,61 +0,0 @@
<?php
/*
*
* 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/>.
*/
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;
}
}

View File

@ -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 }