Test Failed
Push — master ( 7a5412...73978b )
by Dominik
01:59
created

CollectionFieldNormalizer::normalizeField()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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