EmbedOneFieldNormalizer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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 EmbedOneFieldNormalizer implements FieldNormalizerInterface
14
{
15
    /**
16
     * @var AccessorInterface
17
     */
18
    private $accessor;
19
20
    public function __construct(AccessorInterface $accessor)
21
    {
22
        $this->accessor = $accessor;
23 3
    }
24
25 3
    /**
26 3
     * @param object $object
27
     *
28
     * @throws SerializerLogicException
29
     *
30
     * @return mixed
31
     */
32
    public function normalizeField(
33
        string $path,
34
        $object,
35
        NormalizerContextInterface $context,
36
        NormalizerInterface $normalizer = null
37
    ) {
38 3
        if (null === $normalizer) {
39
            throw SerializerLogicException::createMissingNormalizer($path);
40
        }
41
42
        if (null === $relatedObject = $this->accessor->getValue($object)) {
43
            return null;
44 3
        }
45 1
46
        return $normalizer->normalize($relatedObject, $context, $path);
47
    }
48
}
49