HateoasSerializer::fixRelations()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.6
c 0
b 0
f 0
cc 4
nc 8
nop 1
crap 4
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;
15
16
use FiveLab\Component\Resource\Resource\ResourceInterface;
17
use FiveLab\Component\Resource\Serializer\Context\ResourceSerializationContext;
18
use FiveLab\Component\Resource\Serializer\Exception\DeserializationNotSupportException;
19
use FiveLab\Component\Resource\Serializer\ResourceSerializerInterface;
20
use FiveLab\Component\Resource\Serializer\SerializerInterface;
21
use FiveLab\Component\Resource\Serializers\Hateoas\Normalizer\PaginatedCollectionObjectNormalizer;
22
use FiveLab\Component\Resource\Serializers\Hateoas\Normalizer\RelationCollectionObjectNormalizer;
23
use FiveLab\Component\Resource\Serializers\Hateoas\Normalizer\RelationObjectNormalizer;
24
use FiveLab\Component\Resource\Serializers\Hateoas\Normalizer\ResourceCollectionObjectNormalizer;
25
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
26
27
/**
28
 * Hateoas serializer.
29
 *
30
 * @author Vitaliy Zhuk <[email protected]>
31
 */
32
class HateoasSerializer implements ResourceSerializerInterface
33
{
34
    /**
35
     * @var SerializerInterface
36
     */
37
    private $serializer;
38
39
    /**
40
     * @var string
41
     */
42
    private $format;
43
44
    /**
45
     * @var array|NormalizerInterface[]
46
     */
47
    private $normalizers;
48
49
    /**
50
     * Constructor.
51
     *
52
     * @param SerializerInterface $serializer
53
     * @param array               $normalizers
54
     * @param string              $format
55
     */
56 2
    public function __construct(SerializerInterface $serializer, array $normalizers, string $format)
57
    {
58 2
        $this->serializer = $serializer;
59 2
        $this->format = $format;
60 2
        $this->normalizers = $normalizers;
61 2
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public function serialize(ResourceInterface $resource, ResourceSerializationContext $context): string
67
    {
68
        $innerContext = [
69
            'after_normalization' => function (array $data) {
70 1
                return $this->fixRelations($data);
71 1
            },
72 1
            'normalizers'         => $this->normalizers,
73
        ];
74
75 1
        return $this->serializer->serialize($resource, $this->format, $innerContext);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @throws DeserializationNotSupportException
82
     */
83 1
    public function deserialize(string $data, string $resourceClass, ResourceSerializationContext $context): ResourceInterface
84
    {
85 1
        throw new DeserializationNotSupportException('The hateoas not support deserialization.');
86
    }
87
88
    /**
89
     * Fix relations after normalization
90
     *
91
     * @param array $data
92
     *
93
     * @return array
94
     */
95 1
    protected function fixRelations(array $data): array
96
    {
97 1
        $links = [];
98
99 1
        if (\array_key_exists('relations', $data)) {
100 1
            $links = \array_merge($links, $data['relations']);
101 1
            unset($data['relations']);
102
        }
103
104 1
        if (\array_key_exists('actions', $data)) {
105 1
            $links = \array_merge($links, $data['actions']);
106 1
            unset($data['actions']);
107
        }
108
109 1
        if (\count($links)) {
110 1
            $data['_links'] = $links;
111
        }
112
113 1
        return $data;
114
    }
115
}
116