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

ReferenceManyFieldNormalizer::normalizeField()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 15
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 4
crap 12
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 ReferenceManyFieldNormalizer implements FieldNormalizerInterface
14
{
15
    /**
16
     * @var AccessorInterface
17
     */
18
    private $childAccessor;
19
20
    /**
21
     * @var AccessorInterface
22
     */
23
    private $accessor;
24
25
    /**
26
     * @param AccessorInterface $childAccessor
27
     * @param AccessorInterface $accessor
28
     */
29
    public function __construct(AccessorInterface $childAccessor, AccessorInterface $accessor)
30
    {
31
        $this->childAccessor = $childAccessor;
32
        $this->accessor = $accessor;
33
    }
34
35
    /**
36
     * @param string                     $path
37
     * @param object                     $object
38
     * @param NormalizerContextInterface $context
39
     * @param NormalizerInterface|null   $normalizer
40
     *
41
     * @return mixed
42
     *
43
     * @throws SerializerLogicException
44
     */
45
    public function normalizeField(
46
        string $path,
47
        $object,
48
        NormalizerContextInterface $context,
49
        NormalizerInterface $normalizer = null
50
    ) {
51
        if (null === $childObjects = $this->accessor->getValue($object)) {
52
            return null;
53
        }
54
55
        $values = [];
56
        foreach ($childObjects as $i => $childObject) {
57
            $values[$i] = $this->childAccessor->getValue($childObject);
58
        }
59
60
        return $values;
61
    }
62
}
63