Passed
Pull Request — master (#15)
by
unknown
03:16
created

RootAwareFieldNormalizerDecorator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 41
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A normalizeField() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
final class RootAwareFieldNormalizerDecorator implements FieldNormalizerInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    const ATTRIBUTE_ROOT = 'root';
13
14
    /**
15
     * @var FieldNormalizerInterface
16
     */
17
    private $fieldNormalizer;
18
19
    /**
20
     * @param FieldNormalizerInterface $fieldNormalizer
21
     */
22
    public function __construct(FieldNormalizerInterface $fieldNormalizer)
23
    {
24
        $this->fieldNormalizer = $fieldNormalizer;
25
    }
26
27
    /**
28
     * @param string                     $path
29
     * @param object|mixed               $object
30
     * @param NormalizerContextInterface $context
31
     * @param NormalizerInterface|null   $normalizer
32
     *
33
     * @return mixed|void
34
     */
35
    public function normalizeField(
36
        string $path,
37
        $object,
38
        NormalizerContextInterface $context,
39
        NormalizerInterface $normalizer = null
40
    ) {
41
        if (null === $context->getAttribute(self::ATTRIBUTE_ROOT)) {
42
            $context = $context->withAttribute(self::ATTRIBUTE_ROOT, $object);
43
        }
44
45
        return $this->fieldNormalizer->normalizeField($path, $object, $context, $normalizer);
46
    }
47
}
48