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

ErrorResourceNormalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 48
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 31 6
A supportsNormalization() 0 3 2
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\VndError\Normalizer;
15
16
use FiveLab\Component\Resource\Resource\Error\ErrorResourceInterface;
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 error resource for Vnd.Error format.
23
 *
24
 * @author Vitaliy Zhuk <[email protected]>
25
 */
26
class ErrorResourceNormalizer implements NormalizerInterface, NormalizerAwareInterface
27
{
28
    use NormalizerAwareTrait;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function supportsNormalization($data, $format = null): bool
34
    {
35
        return \is_object($data) && $data instanceof ErrorResourceInterface;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @param ErrorResourceInterface $object
42
     */
43
    public function normalize($object, $format = null, array $context = []): array
44
    {
45
        $links = [];
46
47
        if (\count($object->getRelations())) {
48
            // @phpstan-ignore-next-line
49
            $links = \array_merge($links, $this->normalizer->normalize($object->getRelations(), $format, $context));
50
        }
51
52
        if (\count($object->getActions())) {
53
            // @phpstan-ignore-next-line
54
            $links = \array_merge($links, $this->normalizer->normalize($object->getActions(), $format, $context));
55
        }
56
57
        $data = [
58
            'message' => $object->getMessage(),
59
        ];
60
61
        if ($object->getPath()) {
62
            $data['path'] = $object->getPath();
63
        }
64
65
        if ($object->getIdentifier()) {
66
            $data['logref'] = $object->getIdentifier();
67
        }
68
69
        if (\count($links)) {
70
            $data['_links'] = $links;
71
        }
72
73
        return $data;
74
    }
75
}
76