Passed
Push — master ( bcaf80...1c3796 )
by Dominik
02:30
created

EmbedManyFieldNormalizer::resolveProxy()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 5

Importance

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