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

supportsNormalization()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 1
c 0
b 0
f 0
nc 3
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\Hateoas\Normalizer;
15
16
use FiveLab\Component\Resource\Resource\Action\ActionCollection;
17
use FiveLab\Component\Resource\Resource\Relation\RelationCollection;
18
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
22
/**
23
 * Normalizer for normalize relation collection for HATEOAS.
24
 *
25
 * @author Vitaliy Zhuk <[email protected]>
26
 */
27
class RelationCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
28
{
29
    use NormalizerAwareTrait;
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @param RelationCollection $object
35
     *
36
     * @throws \InvalidArgumentException
37
     */
38
    public function normalize($object, $format = null, array $context = []): array
39
    {
40
        $data = [];
41
42
        foreach ($object as $relation) {
43
            $data[$relation->getName()] = $this->normalizer->normalize($relation, $format);
44
        }
45
46
        return $data;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function supportsNormalization($data, $format = null): bool
53
    {
54
        return \is_object($data) && ($data instanceof RelationCollection || $data instanceof ActionCollection);
55
    }
56
}
57