ReferenceOneFieldNormalizer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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