mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
add a discrimnator type on onbjects
This commit is contained in:
@@ -28,6 +28,7 @@ use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
|
||||
|
||||
/**
|
||||
* ThirdParty is a party recorded in the database.
|
||||
@@ -38,7 +39,9 @@ use Chill\MainBundle\Entity\Address;
|
||||
*
|
||||
* @ORM\Table(name="chill_3party.third_party")
|
||||
* @ORM\Entity(repositoryClass="Chill\ThirdPartyBundle\Repository\ThirdPartyRepository")
|
||||
*
|
||||
* @DiscriminatorMap(typeProperty="type", mapping={
|
||||
* "thirdparty"=ThirdParty::class
|
||||
*})
|
||||
*/
|
||||
class ThirdParty
|
||||
{
|
||||
|
@@ -6,31 +6,19 @@ use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use Symfony\Component\Serializer\Exception\RuntimeException;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository;
|
||||
|
||||
|
||||
class ThirdPartyNormalizer implements NormalizerInterface, DenormalizerInterface, NormalizerAwareInterface
|
||||
class ThirdPartyNormalizer implements NormalizerInterface, NormalizerAwareInterface
|
||||
{
|
||||
private ThirdPartyRepository $repository;
|
||||
|
||||
use NormalizerAwareTrait;
|
||||
|
||||
public function __construct(ThirdPartyRepository $repository)
|
||||
{
|
||||
$this->repository= $repository;
|
||||
}
|
||||
|
||||
public function normalize($thirdParty, string $format = null, array $context = [])
|
||||
{
|
||||
/** @var $thirdParty ThirdParty */
|
||||
$data['type'] = 'thirdparty';
|
||||
// TODO should be replaced by a "render entity"
|
||||
$data['text'] = $thirdParty->getName();
|
||||
$data['thirdparty_id'] = $thirdParty->getId();
|
||||
$data['id'] = $thirdParty->getId();
|
||||
$data['address'] = $this->normalizer->normalize($thirdParty->getAddress(), $format,
|
||||
[ 'address_rendering' => 'short' ]);
|
||||
|
||||
@@ -42,29 +30,4 @@ class ThirdPartyNormalizer implements NormalizerInterface, DenormalizerInterface
|
||||
return $data instanceof ThirdParty;
|
||||
}
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||
{
|
||||
$thirdParty = $context[AbstractNormalizer::OBJECT_TO_POPULATE] ?? NULL;
|
||||
if (NULL === $thirdParty) {
|
||||
$id = $data['thirdparty_id'] ?? NULL;
|
||||
if (NULL === $id) {
|
||||
throw new RuntimeException("mission thirdparty_id into body");
|
||||
}
|
||||
|
||||
$thirdParty = $this->repository->findOneById($id);
|
||||
|
||||
if (NULL === $thirdParty) {
|
||||
throw new UnexpectedValueException("thirdparty not found");
|
||||
}
|
||||
}
|
||||
|
||||
return $thirdParty;
|
||||
}
|
||||
|
||||
public function supportsDenormalization($data, string $type, string $format = null)
|
||||
{
|
||||
return ThirdParty::class === $type;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user