mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Use annoations for serialization
This commit is contained in:
parent
3f64db3b3a
commit
a4989f99d6
@ -138,7 +138,12 @@ class ApiController extends AbstractCRUDController
|
|||||||
return $response;
|
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
|
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
|
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' ]];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,6 +33,7 @@ use Doctrine\Common\Collections\Collection;
|
|||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||||
use Chill\MainBundle\Entity\User;
|
use Chill\MainBundle\Entity\User;
|
||||||
|
use Symfony\Component\Serializer\Annotation\Groups;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AccompanyingPeriod Class
|
* AccompanyingPeriod Class
|
||||||
@ -80,6 +81,7 @@ class AccompanyingPeriod
|
|||||||
* @ORM\Id
|
* @ORM\Id
|
||||||
* @ORM\Column(name="id", type="integer")
|
* @ORM\Column(name="id", type="integer")
|
||||||
* @ORM\GeneratedValue(strategy="AUTO")
|
* @ORM\GeneratedValue(strategy="AUTO")
|
||||||
|
* @Groups({"read"})
|
||||||
*/
|
*/
|
||||||
private $id;
|
private $id;
|
||||||
|
|
||||||
@ -87,6 +89,7 @@ class AccompanyingPeriod
|
|||||||
* @var \DateTime
|
* @var \DateTime
|
||||||
*
|
*
|
||||||
* @ORM\Column(type="date")
|
* @ORM\Column(type="date")
|
||||||
|
* @Groups({"read", "write"})
|
||||||
*/
|
*/
|
||||||
private $openingDate;
|
private $openingDate;
|
||||||
|
|
||||||
@ -94,6 +97,7 @@ class AccompanyingPeriod
|
|||||||
* @var \DateTime
|
* @var \DateTime
|
||||||
*
|
*
|
||||||
* @ORM\Column(type="date", nullable=true)
|
* @ORM\Column(type="date", nullable=true)
|
||||||
|
* @Groups({"read", "write"})
|
||||||
*/
|
*/
|
||||||
private $closingDate = null;
|
private $closingDate = null;
|
||||||
|
|
||||||
@ -101,6 +105,7 @@ class AccompanyingPeriod
|
|||||||
* @var string
|
* @var string
|
||||||
*
|
*
|
||||||
* @ORM\Column(type="text")
|
* @ORM\Column(type="text")
|
||||||
|
* @Groups({"read", "write"})
|
||||||
*/
|
*/
|
||||||
private $remark = '';
|
private $remark = '';
|
||||||
|
|
||||||
@ -110,6 +115,7 @@ class AccompanyingPeriod
|
|||||||
* @ORM\OneToMany(targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\Comment",
|
* @ORM\OneToMany(targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\Comment",
|
||||||
* mappedBy="accompanyingPeriod"
|
* mappedBy="accompanyingPeriod"
|
||||||
* )
|
* )
|
||||||
|
* @Groups({"read"})
|
||||||
*/
|
*/
|
||||||
private $comments;
|
private $comments;
|
||||||
|
|
||||||
@ -119,6 +125,7 @@ class AccompanyingPeriod
|
|||||||
* @ORM\OneToMany(targetEntity=AccompanyingPeriodParticipation::class,
|
* @ORM\OneToMany(targetEntity=AccompanyingPeriodParticipation::class,
|
||||||
* mappedBy="accompanyingPeriod",
|
* mappedBy="accompanyingPeriod",
|
||||||
* cascade={"persist", "refresh", "remove", "merge", "detach"})
|
* cascade={"persist", "refresh", "remove", "merge", "detach"})
|
||||||
|
* @Groups({"read"})
|
||||||
*/
|
*/
|
||||||
private $participations;
|
private $participations;
|
||||||
|
|
||||||
@ -128,18 +135,21 @@ class AccompanyingPeriod
|
|||||||
* @ORM\ManyToOne(
|
* @ORM\ManyToOne(
|
||||||
* targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive")
|
* targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive")
|
||||||
* @ORM\JoinColumn(nullable=true)
|
* @ORM\JoinColumn(nullable=true)
|
||||||
|
* @Groups({"read", "write"})
|
||||||
*/
|
*/
|
||||||
private $closingMotive = null;
|
private $closingMotive = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\ManyToOne(targetEntity=User::class)
|
* @ORM\ManyToOne(targetEntity=User::class)
|
||||||
* @ORM\JoinColumn(nullable=true)
|
* @ORM\JoinColumn(nullable=true)
|
||||||
|
* @Groups({"read", "write"})
|
||||||
*/
|
*/
|
||||||
private $user;
|
private $user;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\ManyToOne(targetEntity=User::class)
|
* @ORM\ManyToOne(targetEntity=User::class)
|
||||||
* @ORM\JoinColumn(nullable=true)
|
* @ORM\JoinColumn(nullable=true)
|
||||||
|
* @Groups({"read"})
|
||||||
*/
|
*/
|
||||||
private $createdBy;
|
private $createdBy;
|
||||||
|
|
||||||
@ -152,12 +162,14 @@ class AccompanyingPeriod
|
|||||||
/**
|
/**
|
||||||
* @ORM\ManyToOne(targetEntity=Origin::class)
|
* @ORM\ManyToOne(targetEntity=Origin::class)
|
||||||
* @ORM\JoinColumn(nullable=true)
|
* @ORM\JoinColumn(nullable=true)
|
||||||
|
* @Groups({"read", "write"})
|
||||||
*/
|
*/
|
||||||
private $origin;
|
private $origin;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
* @ORM\Column(type="string", nullable=true)
|
* @ORM\Column(type="string", nullable=true)
|
||||||
|
* @Groups({"read", "write"})
|
||||||
*/
|
*/
|
||||||
private $intensity;
|
private $intensity;
|
||||||
|
|
||||||
@ -172,6 +184,7 @@ class AccompanyingPeriod
|
|||||||
* joinColumns={@ORM\JoinColumn(name="accompanying_period_id", referencedColumnName="id")},
|
* joinColumns={@ORM\JoinColumn(name="accompanying_period_id", referencedColumnName="id")},
|
||||||
* inverseJoinColumns={@ORM\JoinColumn(name="scope_id", referencedColumnName="id")}
|
* inverseJoinColumns={@ORM\JoinColumn(name="scope_id", referencedColumnName="id")}
|
||||||
* )
|
* )
|
||||||
|
* @Groups({"read"})
|
||||||
*/
|
*/
|
||||||
private $scopes;
|
private $scopes;
|
||||||
|
|
||||||
@ -190,18 +203,21 @@ class AccompanyingPeriod
|
|||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
* @ORM\Column(type="boolean")
|
* @ORM\Column(type="boolean")
|
||||||
|
* @Groups({"read", "write"})
|
||||||
*/
|
*/
|
||||||
private $requestorAnonymous = false;
|
private $requestorAnonymous = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
* @ORM\Column(type="boolean")
|
* @ORM\Column(type="boolean")
|
||||||
|
* @Groups({"read", "write"})
|
||||||
*/
|
*/
|
||||||
private $emergency = false;
|
private $emergency = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
* @ORM\Column(type="boolean")
|
* @ORM\Column(type="boolean")
|
||||||
|
* @Groups({"read", "write"})
|
||||||
*/
|
*/
|
||||||
private $confidential = false;
|
private $confidential = false;
|
||||||
|
|
||||||
@ -212,6 +228,7 @@ class AccompanyingPeriod
|
|||||||
* targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\Resource",
|
* targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\Resource",
|
||||||
* mappedBy="accompanyingPeriod"
|
* mappedBy="accompanyingPeriod"
|
||||||
* )
|
* )
|
||||||
|
* @Groups({"read"})
|
||||||
*/
|
*/
|
||||||
private $resources;
|
private $resources;
|
||||||
|
|
||||||
@ -536,6 +553,7 @@ class AccompanyingPeriod
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Person|ThirdParty
|
* @return Person|ThirdParty
|
||||||
|
* @Groups({"read"})
|
||||||
*/
|
*/
|
||||||
public function getRequestor()
|
public function getRequestor()
|
||||||
{
|
{
|
||||||
|
@ -24,6 +24,7 @@ namespace Chill\PersonBundle\Entity\AccompanyingPeriod;
|
|||||||
|
|
||||||
use Chill\PersonBundle\Repository\AccompanyingPeriod\OriginRepository;
|
use Chill\PersonBundle\Repository\AccompanyingPeriod\OriginRepository;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
use Symfony\Component\Serializer\Annotation\Groups;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Entity(repositoryClass=OriginRepository::class)
|
* @ORM\Entity(repositoryClass=OriginRepository::class)
|
||||||
@ -35,16 +36,19 @@ class Origin
|
|||||||
* @ORM\Id
|
* @ORM\Id
|
||||||
* @ORM\GeneratedValue
|
* @ORM\GeneratedValue
|
||||||
* @ORM\Column(type="integer")
|
* @ORM\Column(type="integer")
|
||||||
|
* @Groups({"read"})
|
||||||
*/
|
*/
|
||||||
private $id;
|
private $id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="json")
|
* @ORM\Column(type="json")
|
||||||
|
* @Groups({"read"})
|
||||||
*/
|
*/
|
||||||
private $label;
|
private $label;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="date_immutable", nullable=true)
|
* @ORM\Column(type="date_immutable", nullable=true)
|
||||||
|
* @Groups({"read"})
|
||||||
*/
|
*/
|
||||||
private $noActiveAfter;
|
private $noActiveAfter;
|
||||||
|
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -7,10 +7,6 @@ services:
|
|||||||
tags:
|
tags:
|
||||||
- { name: 'serializer.normalizer', priority: 64 }
|
- { name: 'serializer.normalizer', priority: 64 }
|
||||||
|
|
||||||
Chill\PersonBundle\Serializer\Normalizer\AccompanyingPeriodNormalizer:
|
|
||||||
tags:
|
|
||||||
- { name: 'serializer.normalizer', priority: 64 }
|
|
||||||
|
|
||||||
Chill\PersonBundle\Serializer\Normalizer\AccompanyingPeriodParticipationNormalizer:
|
Chill\PersonBundle\Serializer\Normalizer\AccompanyingPeriodParticipationNormalizer:
|
||||||
tags:
|
tags:
|
||||||
- { name: 'serializer.normalizer', priority: 64 }
|
- { name: 'serializer.normalizer', priority: 64 }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user