136 lines
4.9 KiB
PHP

<?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;
use Chill\DocGeneratorBundle\Serializer\Helper\NormalizeNullValueHelper;
use Chill\MainBundle\Entity\Address;
use DateTimeInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
class AddressNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
private const NULL_POSTCODE_COUNTRY = [
'id', 'name', 'code',
];
private const NULL_VALUE = [
'address_id',
'text',
'street',
'streetNumber',
'postcode',
'country',
'floor',
'corridor',
'steps',
'flat',
'buildingName',
'distribution',
'extra',
'validFrom' => DateTimeInterface::class,
'validTo' => DateTimeInterface::class,
];
/**
* @param Address $address
*/
public function normalize($address, ?string $format = null, array $context = [])
{
if ($address instanceof Address) {
$text = $address->isNoAddress() ? '' : $address->getStreet() . ', ' . $address->getStreetNumber();
if (null !== $address->getPostCode()->getCountry()->getCountryCode()) {
if ($address->getPostCode()->getCountry()->getCountryCode() === 'FR') {
$text = $address->isNoAddress() ? '' : $address->getStreetNumber() . ', ' . $address->getStreet();
} else {
$text = $address->isNoAddress() ? '' : $address->getStreetNumber() . ', ' . $address->getStreet();
}
}
$data = [
'address_id' => $address->getId(),
'text' => $text,
'street' => $address->getStreet(),
'streetNumber' => $address->getStreetNumber(),
'postcode' => [
'id' => $address->getPostCode()->getId(),
'name' => $address->getPostCode()->getName(),
'code' => $address->getPostCode()->getCode(),
],
'country' => [
'id' => $address->getPostCode()->getCountry()->getId(),
'name' => $address->getPostCode()->getCountry()->getName(),
'code' => $address->getPostCode()->getCountry()->getCountryCode(),
],
'floor' => $address->getFloor(),
'corridor' => $address->getCorridor(),
'steps' => $address->getSteps(),
'flat' => $address->getFlat(),
'buildingName' => $address->getBuildingName(),
'distribution' => $address->getDistribution(),
'extra' => $address->getExtra(),
];
if ('json' === $format) {
$data['addressReference'] = $this->normalizer->normalize(
$address->getAddressReference(),
$format,
[AbstractNormalizer::GROUPS => ['read']]
);
$data['validFrom'] = $address->getValidFrom();
$data['validTo'] = $address->getValidTo();
} elseif ('docgen' === $format) {
$dateContext = array_merge($context, ['docgen:expects' => DateTimeInterface::class]);
$data['validFrom'] = $this->normalizer->normalize($address->getValidFrom(), $format, $dateContext);
$data['validTo'] = $this->normalizer->normalize($address->getValidTo(), $format, $dateContext);
}
return $data;
}
if (null === $address) {
$helper = new NormalizeNullValueHelper($this->normalizer);
return array_merge(
$helper->normalize(self::NULL_VALUE, $format, $context),
[
'postcode' => $helper->normalize(self::NULL_POSTCODE_COUNTRY, $format, $context),
'country' => $helper->normalize(self::NULL_POSTCODE_COUNTRY, $format, $context),
]
);
}
throw new UnexpectedValueException();
}
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
if ('json' === $format) {
return $data instanceof Address;
}
if ('docgen' === $format) {
return
$data instanceof Address
|| (null === $data && Address::class === ($context['docgen:expects'] ?? null));
}
return false;
}
}