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

RelationNormalizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 30
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 15 3
A supportsNormalization() 0 3 3
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\ActionInterface;
17
use FiveLab\Component\Resource\Resource\Relation\RelationInterface;
18
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19
20
/**
21
 * Normalizer for normalize relation object for HATEOAS.
22
 *
23
 * @author Vitaliy Zhuk <[email protected]>
24
 */
25
class RelationNormalizer implements NormalizerInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     *
30
     * @throws \InvalidArgumentException
31
     */
32
    public function normalize($object, $format = null, array $context = []): array
33
    {
34
        $data = [
35
            'href' => $object->getHref()->getPath(),
36
        ];
37
38
        if ($object->getHref()->isTemplated()) {
39
            $data['templated'] = true;
40
        }
41
42
        if (\count($object->getAttributes())) {
43
            $data['attributes'] = $object->getAttributes();
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 RelationInterface || $data instanceof ActionInterface);
55
    }
56
}
57