EmbedManyFieldNormalizer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
dl 0
loc 40
ccs 9
cts 9
cp 1
rs 10
c 1
b 0
f 1
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeField() 0 21 4
A __construct() 0 3 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