PropertyNormalizerWrapper::getAttributeValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 4
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Messages serializer implementation.
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\MessageSerializer\Symfony\Extensions;
14
15
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
16
17
/**
18
 * Disable the use of the constructor.
19
 *
20
 * @noinspection LongInheritanceChainInspection
21
 */
22
final class PropertyNormalizerWrapper extends PropertyNormalizer
23
{
24
    /**
25
     * @psalm-var array<string, array<array-key, string>>
26
     */
27
    private $localStorage = [];
28
29
    /**
30
     * {@inheritdoc}
31
     *
32
     * @param array|bool $allowedAttributes
33
     *
34
     * @throws \ReflectionException
35
     * @noinspection PhpMissingParamTypeInspection
36
     */
37 7
    protected function instantiateObject(
38
        array &$data,
39
        string $class,
40
        array &$context,
41
        \ReflectionClass $reflectionClass,
42
        $allowedAttributes,
43
        string $format = null
44
    ): object {
45 7
        return $reflectionClass->newInstanceWithoutConstructor();
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 8
    protected function extractAttributes(object $object, string $format = null, array $context = []): array
52
    {
53 8
        $class = \get_class($object);
54
55 8
        if (\array_key_exists($class, $this->localStorage) === false)
56
        {
57 8
            $this->localStorage[$class] = parent::extractAttributes($object, $format, $context);
58
        }
59
60 8
        return $this->localStorage[$class];
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     *
66
     * @throws \Error
67
     */
68 8
    protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
69
    {
70 8
        if (isset($object->{$attribute}))
71
        {
72 7
            return $object->{$attribute};
73
        }
74
75 3
        return parent::getAttributeValue($object, $attribute, $format, $context);
76
    }
77
78
    /**
79
     * @psalm-param mixed $value
80
     *
81
     * {@inheritdoc}
82
     */
83 7
    protected function setAttributeValue(object $object, string $attribute, $value, string $format = null, array $context = []): void
84
    {
85 7
        if (isset($object->{$attribute}))
86
        {
87
            $object->{$attribute} = $value;
88
89
            return;
90
        }
91
92 7
        parent::setAttributeValue($object, $attribute, $value, $format, $context);
93 7
    }
94
}
95