mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,28 +1,35 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Serializer\Model;
|
||||
|
||||
use Chill\MainBundle\Pagination\PaginatorInterface;
|
||||
|
||||
class Collection
|
||||
{
|
||||
private PaginatorInterface $paginator;
|
||||
|
||||
private $items;
|
||||
|
||||
private PaginatorInterface $paginator;
|
||||
|
||||
public function __construct($items, PaginatorInterface $paginator)
|
||||
{
|
||||
$this->items = $items;
|
||||
$this->paginator = $paginator;
|
||||
}
|
||||
|
||||
public function getPaginator(): PaginatorInterface
|
||||
{
|
||||
return $this->paginator;
|
||||
}
|
||||
|
||||
public function getItems()
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
public function getPaginator(): PaginatorInterface
|
||||
{
|
||||
return $this->paginator;
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||
@@ -17,11 +24,11 @@ class AddressNormalizer implements NormalizerAwareInterface, NormalizerInterface
|
||||
/**
|
||||
* @param Address $address
|
||||
*/
|
||||
public function normalize($address, string $format = null, array $context = [])
|
||||
public function normalize($address, ?string $format = null, array $context = [])
|
||||
{
|
||||
$data = [
|
||||
return [
|
||||
'address_id' => $address->getId(),
|
||||
'text' => $address->isNoAddress() ? '' : $address->getStreetNumber().', '.$address->getStreet(),
|
||||
'text' => $address->isNoAddress() ? '' : $address->getStreetNumber() . ', ' . $address->getStreet(),
|
||||
'street' => $address->getStreet(),
|
||||
'streetNumber' => $address->getStreetNumber(),
|
||||
'postcode' => [
|
||||
@@ -49,14 +56,10 @@ class AddressNormalizer implements NormalizerAwareInterface, NormalizerInterface
|
||||
[AbstractNormalizer::GROUPS => ['read']]
|
||||
),
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, string $format = null)
|
||||
public function supportsNormalization($data, ?string $format = null)
|
||||
{
|
||||
return $data instanceof Address;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -1,69 +1,42 @@
|
||||
<?php
|
||||
/*
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
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;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use function array_key_exists;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
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())
|
||||
public function denormalize($data, string $type, ?string $format = null, array $context = [])
|
||||
{
|
||||
/** @var Center $center */
|
||||
return [
|
||||
'id' => $center->getId(),
|
||||
'type' => 'center',
|
||||
'name' => $center->getName()
|
||||
];
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, string $format = null): bool
|
||||
{
|
||||
return $data instanceof Center && $format === 'json';
|
||||
}
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||
{
|
||||
if (FALSE === \array_key_exists('type', $data)) {
|
||||
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)) {
|
||||
|
||||
if (false === array_key_exists('id', $data)) {
|
||||
throw new InvalidArgumentException('missing "id" key in data');
|
||||
}
|
||||
|
||||
@@ -76,8 +49,23 @@ class CenterNormalizer implements NormalizerInterface, DenormalizerInterface
|
||||
return $center;
|
||||
}
|
||||
|
||||
public function supportsDenormalization($data, string $type, string $format = null)
|
||||
public function normalize($center, ?string $format = null, array $context = [])
|
||||
{
|
||||
return $type === Center::class;
|
||||
/** @var Center $center */
|
||||
return [
|
||||
'id' => $center->getId(),
|
||||
'type' => 'center',
|
||||
'name' => $center->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
public function supportsDenormalization($data, string $type, ?string $format = null)
|
||||
{
|
||||
return Center::class === $type;
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, ?string $format = null): bool
|
||||
{
|
||||
return $data instanceof Center && 'json' === $format;
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +1,18 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||
|
||||
use Chill\MainBundle\Serializer\Model\Collection;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
class CollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
|
||||
{
|
||||
@@ -14,7 +21,7 @@ class CollectionNormalizer implements NormalizerInterface, NormalizerAwareInterf
|
||||
/**
|
||||
* @param Collection $collection
|
||||
*/
|
||||
public function normalize($collection, string $format = null, array $context = [])
|
||||
public function normalize($collection, ?string $format = null, array $context = [])
|
||||
{
|
||||
$paginator = $collection->getPaginator();
|
||||
|
||||
@@ -31,7 +38,7 @@ class CollectionNormalizer implements NormalizerInterface, NormalizerAwareInterf
|
||||
];
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, string $format = null): bool
|
||||
public function supportsNormalization($data, ?string $format = null): bool
|
||||
{
|
||||
return $data instanceof Collection;
|
||||
}
|
||||
|
@@ -1,115 +1,112 @@
|
||||
<?php
|
||||
/*
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use IntlDateFormatter;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use function is_array;
|
||||
|
||||
class DateNormalizer implements ContextAwareNormalizerInterface, DenormalizerInterface
|
||||
{
|
||||
private RequestStack $requestStack;
|
||||
private ParameterBagInterface $parameterBag;
|
||||
|
||||
private RequestStack $requestStack;
|
||||
|
||||
public function __construct(RequestStack $requestStack, ParameterBagInterface $parameterBag)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->parameterBag = $parameterBag;
|
||||
}
|
||||
|
||||
public function normalize($date, string $format = null, array $context = array())
|
||||
public function denormalize($data, string $type, ?string $format = null, array $context = [])
|
||||
{
|
||||
/** @var \DateTimeInterface $date */
|
||||
switch($format) {
|
||||
switch ($type) {
|
||||
case DateTime::class:
|
||||
return DateTime::createFromFormat(DateTimeInterface::ISO8601, $data['datetime']);
|
||||
|
||||
case DateTimeInterface::class:
|
||||
case DateTimeImmutable::class:
|
||||
default:
|
||||
return DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, $data['datetime']);
|
||||
}
|
||||
}
|
||||
|
||||
public function normalize($date, ?string $format = null, array $context = [])
|
||||
{
|
||||
/** @var DateTimeInterface $date */
|
||||
switch ($format) {
|
||||
case 'json':
|
||||
return [
|
||||
'datetime' => $date->format(\DateTimeInterface::ISO8601)
|
||||
'datetime' => $date->format(DateTimeInterface::ISO8601),
|
||||
];
|
||||
case 'docgen':
|
||||
|
||||
case 'docgen':
|
||||
if (null === $date) {
|
||||
return [
|
||||
'long' => '', 'short' => ''
|
||||
'long' => '', 'short' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$hasTime = $date->format('His') !== "000000";
|
||||
$hasTime = $date->format('His') !== '000000';
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
$locale = null !== $request ? $request->getLocale() : $this->parameterBag->get('kernel.default_locale');
|
||||
$formatterLong = \IntlDateFormatter::create(
|
||||
$formatterLong = IntlDateFormatter::create(
|
||||
$locale,
|
||||
\IntlDateFormatter::LONG,
|
||||
$hasTime ? \IntlDateFormatter::SHORT: \IntlDateFormatter::NONE
|
||||
IntlDateFormatter::LONG,
|
||||
$hasTime ? IntlDateFormatter::SHORT : IntlDateFormatter::NONE
|
||||
);
|
||||
$formatterShort = \IntlDateFormatter::create(
|
||||
$formatterShort = IntlDateFormatter::create(
|
||||
$locale,
|
||||
\IntlDateFormatter::SHORT,
|
||||
$hasTime ? \IntlDateFormatter::SHORT: \IntlDateFormatter::NONE
|
||||
IntlDateFormatter::SHORT,
|
||||
$hasTime ? IntlDateFormatter::SHORT : IntlDateFormatter::NONE
|
||||
);
|
||||
|
||||
return [
|
||||
'short' => $formatterShort->format($date),
|
||||
'long' => $formatterLong->format($date)
|
||||
'long' => $formatterLong->format($date),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, string $format = null, array $context = []): bool
|
||||
public function supportsDenormalization($data, string $type, ?string $format = null): bool
|
||||
{
|
||||
if ($format === 'json') {
|
||||
return $data instanceof \DateTimeInterface;
|
||||
} elseif ($format === 'docgen') {
|
||||
return $data instanceof \DateTimeInterface || (
|
||||
$data === null
|
||||
return DateTimeInterface::class === $type
|
||||
|| DateTime::class === $type
|
||||
|| DateTimeImmutable::class === $type
|
||||
|| (is_array($data) && array_key_exists('datetime', $data));
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
|
||||
{
|
||||
if ('json' === $format) {
|
||||
return $data instanceof DateTimeInterface;
|
||||
}
|
||||
|
||||
if ('docgen' === $format) {
|
||||
return $data instanceof DateTimeInterface || (
|
||||
null === $data
|
||||
&& \array_key_exists('docgen:expects', $context)
|
||||
&& (
|
||||
$context['docgen:expects'] === \DateTimeInterface::class
|
||||
|| $context['docgen:expects'] === \DateTime::class
|
||||
|| $context['docgen:expects'] === \DateTimeImmutable::class
|
||||
DateTimeInterface::class === $context['docgen:expects']
|
||||
|| DateTime::class === $context['docgen:expects']
|
||||
|| DateTimeImmutable::class === $context['docgen:expects']
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||
{
|
||||
switch ($type) {
|
||||
case \DateTime::class:
|
||||
return \DateTime::createFromFormat(\DateTimeInterface::ISO8601, $data['datetime']);
|
||||
case \DateTimeInterface::class:
|
||||
case \DateTimeImmutable::class:
|
||||
default:
|
||||
return \DateTimeImmutable::createFromFormat(\DateTimeInterface::ISO8601, $data['datetime']);
|
||||
}
|
||||
}
|
||||
|
||||
public function supportsDenormalization($data, string $type, string $format = null): bool
|
||||
{
|
||||
return $type === \DateTimeInterface::class ||
|
||||
$type === \DateTime::class ||
|
||||
$type === \DateTimeImmutable::class ||
|
||||
(\is_array($data) && array_key_exists('datetime', $data));
|
||||
}
|
||||
}
|
||||
|
@@ -1,62 +1,64 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||
|
||||
use LogicException;
|
||||
use Symfony\Component\Serializer\Exception\RuntimeException;
|
||||
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Exception\RuntimeException;
|
||||
|
||||
use function implode;
|
||||
|
||||
/**
|
||||
* Denormalize an object given a list of supported class
|
||||
* Denormalize an object given a list of supported class.
|
||||
*/
|
||||
class DiscriminatedObjectDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface
|
||||
{
|
||||
use DenormalizerAwareTrait;
|
||||
|
||||
/**
|
||||
* The type to set for enabling this type
|
||||
*/
|
||||
public const TYPE = '@multi';
|
||||
|
||||
/**
|
||||
* Should be present in context and contains an array of
|
||||
* allowed types.
|
||||
* allowed types.
|
||||
*/
|
||||
public const ALLOWED_TYPES = 'denormalize_multi.allowed_types';
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* The type to set for enabling this type.
|
||||
*/
|
||||
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||
public const TYPE = '@multi';
|
||||
|
||||
public function denormalize($data, string $type, ?string $format = null, array $context = [])
|
||||
{
|
||||
foreach ($context[self::ALLOWED_TYPES] as $localType) {
|
||||
if ($this->denormalizer->supportsDenormalization($data, $localType, $format)) {
|
||||
try {
|
||||
return $this->denormalizer->denormalize($data, $localType, $format, $context); } catch (RuntimeException $e) {
|
||||
return $this->denormalizer->denormalize($data, $localType, $format, $context);
|
||||
} catch (RuntimeException $e) {
|
||||
$lastException = $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException(sprintf("Could not find any denormalizer for those ".
|
||||
"ALLOWED_TYPES: %s", \implode(", ", $context[self::ALLOWED_TYPES])));
|
||||
throw new RuntimeException(sprintf('Could not find any denormalizer for those ' .
|
||||
'ALLOWED_TYPES: %s', implode(', ', $context[self::ALLOWED_TYPES])));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function supportsDenormalization($data, string $type, string $format = null, array $context = [])
|
||||
public function supportsDenormalization($data, string $type, ?string $format = null, array $context = [])
|
||||
{
|
||||
if (self::TYPE !== $type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (0 === count($context[self::ALLOWED_TYPES] ?? [])) {
|
||||
throw new \LogicException("The context should contains a list of
|
||||
allowed types");
|
||||
throw new LogicException('The context should contains a list of
|
||||
allowed types');
|
||||
}
|
||||
|
||||
foreach ($context[self::ALLOWED_TYPES] as $localType) {
|
||||
@@ -67,5 +69,4 @@ class DiscriminatedObjectDenormalizer implements ContextAwareDenormalizerInterfa
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,14 +1,21 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface as SerializerMetadata;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use function array_key_exists;
|
||||
use function is_array;
|
||||
|
||||
class DoctrineExistingEntityNormalizer implements DenormalizerInterface
|
||||
{
|
||||
@@ -16,16 +23,15 @@ class DoctrineExistingEntityNormalizer implements DenormalizerInterface
|
||||
|
||||
private ClassMetadataFactoryInterface $serializerMetadataFactory;
|
||||
|
||||
|
||||
public function __construct(EntityManagerInterface $em, ClassMetadataFactoryInterface $serializerMetadataFactory)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->serializerMetadataFactory = $serializerMetadataFactory;
|
||||
}
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||
public function denormalize($data, string $type, ?string $format = null, array $context = [])
|
||||
{
|
||||
if (\array_key_exists(AbstractNormalizer::OBJECT_TO_POPULATE, $context)) {
|
||||
if (array_key_exists(AbstractNormalizer::OBJECT_TO_POPULATE, $context)) {
|
||||
return $context[AbstractNormalizer::OBJECT_TO_POPULATE];
|
||||
}
|
||||
|
||||
@@ -33,23 +39,22 @@ class DoctrineExistingEntityNormalizer implements DenormalizerInterface
|
||||
->find($data['id']);
|
||||
}
|
||||
|
||||
public function supportsDenormalization($data, string $type, string $format = null)
|
||||
public function supportsDenormalization($data, string $type, ?string $format = null)
|
||||
{
|
||||
if (FALSE === \is_array($data)) {
|
||||
if (false === is_array($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (FALSE === \array_key_exists('id', $data)) {
|
||||
if (false === array_key_exists('id', $data)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (FALSE === $this->em->getClassMetadata($type) instanceof ClassMetadata) {
|
||||
return false;
|
||||
if (false === $this->em->getClassMetadata($type) instanceof ClassMetadata) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// does have serializer metadata, and class discriminator ?
|
||||
if ($this->serializerMetadataFactory->hasMetadataFor($type)) {
|
||||
|
||||
$classDiscriminator = $this->serializerMetadataFactory
|
||||
->getMetadataFor($type)->getClassDiscriminatorMapping();
|
||||
|
||||
@@ -58,14 +63,14 @@ class DoctrineExistingEntityNormalizer implements DenormalizerInterface
|
||||
|
||||
// check that only 2 keys
|
||||
// that the second key is property
|
||||
// and that the type match the class for given type property
|
||||
// and that the type match the class for given type property
|
||||
return count($data) === 2
|
||||
&& \array_key_exists($typeProperty, $data)
|
||||
&& $type === $classDiscriminator->getClassForType($data[$typeProperty]);
|
||||
&& array_key_exists($typeProperty, $data)
|
||||
&& $classDiscriminator->getClassForType($data[$typeProperty]) === $type;
|
||||
}
|
||||
}
|
||||
|
||||
// we do not have any class discriminator. Check that the id is the only one key
|
||||
return count($data) === 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,34 +1,36 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use Chill\MainBundle\Doctrine\Model\Point;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
|
||||
use Chill\MainBundle\Doctrine\Model\Point;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
|
||||
class PointNormalizer implements DenormalizerInterface
|
||||
{
|
||||
|
||||
public function supportsDenormalization($data, string $type, string $format = null) : bool
|
||||
public function denormalize($data, string $type, ?string $format = null, array $context = [])
|
||||
{
|
||||
return $type === Point::class;
|
||||
}
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||
{
|
||||
|
||||
if (!is_array($data)) {
|
||||
throw new InvalidArgumentException('point data is not an array. It should be an array of 2 coordinates.');
|
||||
} else {
|
||||
if (count($data) !== 2) {
|
||||
throw new InvalidArgumentException('point data is not an array of 2 elements. It should be an array of 2 coordinates.');
|
||||
} else {
|
||||
return Point::fromLonLat($data[0], $data[1]);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($data) !== 2) {
|
||||
throw new InvalidArgumentException('point data is not an array of 2 elements. It should be an array of 2 coordinates.');
|
||||
}
|
||||
|
||||
return Point::fromLonLat($data[0], $data[1]);
|
||||
}
|
||||
|
||||
public function supportsDenormalization($data, string $type, ?string $format = null): bool
|
||||
{
|
||||
return Point::class === $type;
|
||||
}
|
||||
}
|
||||
|
@@ -1,20 +1,10 @@
|
||||
<?php
|
||||
/*
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||
@@ -28,6 +18,7 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
class UserNormalizer implements NormalizerInterface, NormalizerAwareInterface
|
||||
{
|
||||
use NormalizerAwareTrait;
|
||||
|
||||
private UserRender $userRender;
|
||||
|
||||
public function __construct(UserRender $userRender)
|
||||
@@ -35,7 +26,7 @@ class UserNormalizer implements NormalizerInterface, NormalizerAwareInterface
|
||||
$this->userRender = $userRender;
|
||||
}
|
||||
|
||||
public function normalize($user, string $format = null, array $context = array())
|
||||
public function normalize($user, ?string $format = null, array $context = [])
|
||||
{
|
||||
/** @var User $user */
|
||||
return [
|
||||
@@ -50,8 +41,8 @@ class UserNormalizer implements NormalizerInterface, NormalizerAwareInterface
|
||||
];
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, string $format = null): bool
|
||||
public function supportsNormalization($data, ?string $format = null): bool
|
||||
{
|
||||
return $format === 'json' && $data instanceof User;
|
||||
return 'json' === $format && $data instanceof User;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user