Passed
Push — master ( edadc9...bfe05e )
by Dominik
02:02
created

ReferenceManyFieldNormalizer::normalizeField()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 4
crap 3
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 $identifierAccessor;
19
20
    /**
21
     * @var AccessorInterface
22
     */
23
    private $accessor;
24
25
    /**
26
     * @param AccessorInterface $identifierAccessor
27
     * @param AccessorInterface $accessor
28
     */
29 3
    public function __construct(AccessorInterface $identifierAccessor, AccessorInterface $accessor)
30
    {
31 3
        $this->identifierAccessor = $identifierAccessor;
32 3
        $this->accessor = $accessor;
33 3
    }
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 3
    public function normalizeField(
46
        string $path,
47
        $object,
48
        NormalizerContextInterface $context,
49
        NormalizerInterface $normalizer = null
50
    ) {
51 3
        if (null === $childObjects = $this->accessor->getValue($object)) {
52 1
            return null;
53
        }
54
55 2
        $values = [];
56 2
        foreach ($childObjects as $i => $childObject) {
57 1
            $values[$i] = $this->identifierAccessor->getValue($childObject);
58
        }
59
60 2
        return $values;
61
    }
62
}
63