Passed
Push — master ( 96a906...adf096 )
by Dominik
02:00
created

ReferenceOneFieldNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer\Relation;
6
7
use Chubbyphp\Serialization\Accessor\AccessorInterface;
8
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
9
use Chubbyphp\Serialization\Normalizer\NormalizerInterface;
10
use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface;
11
use Chubbyphp\Serialization\SerializerLogicException;
12
13
final class ReferenceOneFieldNormalizer implements FieldNormalizerInterface
14
{
15
    /**
16
     * @var AccessorInterface
17
     */
18
    private $childAccessor;
19
20
    /**
21
     * @var AccessorInterface
22
     */
23
    private $accessor;
24
25
    /**
26
     * @param AccessorInterface $childAccessor
27
     * @param AccessorInterface $accessor
28
     */
29
    public function __construct(AccessorInterface $childAccessor, AccessorInterface $accessor)
30
    {
31
        $this->childAccessor = $childAccessor;
32
        $this->accessor = $accessor;
33
    }
34
35
    /**
36
     * @param string                     $path
37
     * @param object                     $object
38
     * @param NormalizerContextInterface $context
39
     * @param NormalizerInterface|null   $normalizer
40
     *
41
     * @return mixed
42
     *
43
     * @throws SerializerLogicException
44
     */
45
    public function normalizeField(
46
        string $path,
47
        $object,
48
        NormalizerContextInterface $context,
49
        NormalizerInterface $normalizer = null
50
    ) {
51
        if (null === $childObject = $this->accessor->getValue($object)) {
52
            return null;
53
        }
54
55
        return $this->childAccessor->getValue($childObject);
56
    }
57
}
58