EmbedManyFieldNormalizer::__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 EmbedManyFieldNormalizer implements FieldNormalizerInterface
14
{
15
    /**
16
     * @var AccessorInterface
17
     */
18
    private $accessor;
19
20
    public function __construct(AccessorInterface $accessor)
21
    {
22
        $this->accessor = $accessor;
23 4
    }
24
25 4
    /**
26 4
     * @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 4
        if (null === $normalizer) {
39
            throw SerializerLogicException::createMissingNormalizer($path);
40
        }
41
42
        if (null === $relatedObjects = $this->accessor->getValue($object)) {
43
            return null;
44 4
        }
45 1
46
        $values = [];
47
        foreach ($relatedObjects as $i => $relatedObject) {
48 3
            $subPath = $path.'['.$i.']';
49 1
            $values[$i] = $normalizer->normalize($relatedObject, $context, $subPath);
50
        }
51
52 2
        return $values;
53 2
    }
54
}
55