Passed
Pull Request — master (#2)
by Vitaliy
03:08 queued 36s
created

supportsNormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the FiveLab Resource package
7
 *
8
 * (c) FiveLab
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace FiveLab\Component\Resource\Serializers\WebApi\Normalizer;
15
16
use FiveLab\Component\Resource\Resource\PaginatedResourceCollection;
17
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
18
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
21
/**
22
 * Normalizer for normalize paginated collection.
23
 *
24
 * @author Vitaliy Zhuk <[email protected]>
25
 */
26
class PaginatedCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
27
{
28
    use NormalizerAwareTrait;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function supportsNormalization($data, $format = null): bool
34
    {
35
        return $data instanceof PaginatedResourceCollection;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @param PaginatedResourceCollection $object
42
     */
43
    public function normalize($object, $format = null, array $context = []): array
44
    {
45
        $normalized = [
46
            'page' => $object->getPage(),
47
            'limit' => $object->getLimit(),
48
            'total' => $object->getTotal(),
49
            'items' => [],
50
        ];
51
52
        foreach ($object as $item) {
53
            $normalized['items'][] = $this->normalizer->normalize($item, $format, $context);
54
        }
55
56
        return $normalized;
57
    }
58
}
59