2021-12-21 12:13:20 +01:00

58 lines
1.5 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\DocGeneratorBundle\Serializer\Normalizer;
use ArrayObject;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
/**
* Normalize a collection for docgen format.
*/
class CollectionDocGenNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
/**
* @param Collection $object
* @param null|string $format
*
* @return array|ArrayObject|bool|float|int|string|void|null
*/
public function normalize($object, $format = null, array $context = [])
{
$data = [];
if (null === $object || 0 === $object->count()) {
return $data;
}
foreach ($object->getIterator() as $item) {
$data[] = $this->normalizer->normalize($item, $format, $context);
}
return $data;
}
public function supportsNormalization($data, $format = null, array $context = [])
{
if ('docgen' !== $format) {
return false;
}
return $data instanceof Collection
|| (null === $data && Collection::class === ($context['docgen:expects'] ?? null));
}
}