Passed
Push — master ( 96a906...adf096 )
by Dominik
02:00
created

EmbedManyFieldNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
    public function __construct(AccessorInterface $accessor)
24
    {
25
        $this->accessor = $accessor;
26
    }
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
    public function normalizeField(
39
        string $path,
40
        $object,
41
        NormalizerContextInterface $context,
42
        NormalizerInterface $normalizer = null
43
    ) {
44
        if (null === $normalizer) {
45
            throw SerializerLogicException::createMissingNormalizer($path);
46
        }
47
48
        if (null === $childObjects = $this->accessor->getValue($object)) {
49
            return null;
50
        }
51
52
        $values = [];
53 View Code Duplication
        foreach ($childObjects as $i => $childObject) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            $subPath = $path.'['.$i.']';
55
            $values[$i] = $normalizer->normalize($childObject, $context, $subPath);
56
        }
57
58
        return $values;
59
    }
60
}
61