mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-19 07:16:12 +00:00
56 lines
1.8 KiB
PHP
56 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* 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\NormalizerAwareInterface;
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
|
|
|
class CollectionNormalizer implements NormalizerAwareInterface, NormalizerInterface
|
|
{
|
|
use NormalizerAwareTrait;
|
|
|
|
/**
|
|
* @param Collection $collection
|
|
* @param string|null $format
|
|
*/
|
|
public function normalize($collection, $format = null, array $context = []): string|int|float|bool|\ArrayObject|array|null
|
|
{
|
|
$paginator = $collection->getPaginator();
|
|
|
|
return [
|
|
'count' => $paginator->getTotalItems(),
|
|
'pagination' => [
|
|
'first' => $paginator->getCurrentPageFirstItemNumber(),
|
|
'items_per_page' => $paginator->getItemsPerPage(),
|
|
'next' => $paginator->hasNextPage() ? $paginator->getNextPage()->generateUrl() : null,
|
|
'previous' => $paginator->hasPreviousPage() ? $paginator->getPreviousPage()->generateUrl() : null,
|
|
'more' => $paginator->hasNextPage(),
|
|
],
|
|
'results' => $this->normalizer->normalize($collection->getItems(), $format, $context),
|
|
];
|
|
}
|
|
|
|
public function supportsNormalization($data, $format = null, array $context = []): bool
|
|
{
|
|
return $data instanceof Collection;
|
|
}
|
|
|
|
public function getSupportedTypes(?string $format): array
|
|
{
|
|
return [
|
|
Collection::class => true,
|
|
];
|
|
}
|
|
}
|