supportsNormalization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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\Serializer\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 2
    public function supportsNormalization($data, $format = null): bool
34
    {
35 2
        return $data instanceof PaginatedResourceCollection;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @param PaginatedResourceCollection $object
42
     */
43 1
    public function normalize($object, $format = null, array $context = []): array
44
    {
45
        $normalized = [
46 1
            'page' => $object->getPage(),
47 1
            'limit' => $object->getLimit(),
48 1
            'total' => $object->getTotal(),
49
            'items' => [],
50
        ];
51
52 1
        foreach ($object as $item) {
53 1
            $normalized['items'][] = $this->normalizer->normalize($item, $format, $context);
54
        }
55
56 1
        return $normalized;
57
    }
58
}
59