mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 23:23:51 +00:00
add route for creating a person, and post api
This commit is contained in:
@@ -20,19 +20,32 @@
|
||||
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\MainBundle\Repository\CenterRepository;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
class CenterNormalizer implements NormalizerInterface
|
||||
class CenterNormalizer implements NormalizerInterface, DenormalizerInterface
|
||||
{
|
||||
private CenterRepository $repository;
|
||||
|
||||
|
||||
public function __construct(CenterRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
public function normalize($center, string $format = null, array $context = array())
|
||||
{
|
||||
/** @var Center $center */
|
||||
return [
|
||||
'id' => $center->getId(),
|
||||
'type' => 'center',
|
||||
'name' => $center->getName()
|
||||
];
|
||||
}
|
||||
@@ -41,4 +54,30 @@ class CenterNormalizer implements NormalizerInterface
|
||||
{
|
||||
return $data instanceof Center;
|
||||
}
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||
{
|
||||
if (FALSE === \array_key_exists('type', $data)) {
|
||||
throw new InvalidArgumentException('missing "type" key in data');
|
||||
}
|
||||
if ('center' !== $data['type']) {
|
||||
throw new InvalidArgumentException('type should be equal to "center"');
|
||||
}
|
||||
if (FALSE === \array_key_exists('id', $data)) {
|
||||
throw new InvalidArgumentException('missing "id" key in data');
|
||||
}
|
||||
|
||||
$center = $this->repository->find($data['id']);
|
||||
|
||||
if (null === $center) {
|
||||
throw new UnexpectedValueException("The type with id {$data['id']} does not exists");
|
||||
}
|
||||
|
||||
return $center;
|
||||
}
|
||||
|
||||
public function supportsDenormalization($data, string $type, string $format = null)
|
||||
{
|
||||
return $type === Center::class;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user