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

ReferenceManyFieldNormalizer::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 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 === $relatedObjects = $this->accessor->getValue($object)) {
52 1
            return null;
53
        }
54
55 2
        $values = [];
56 2
        foreach ($relatedObjects as $i => $relatedObject) {
57 1
            $values[$i] = $this->identifierAccessor->getValue($relatedObject);
58
        }
59
60 2
        return $values;
61
    }
62
}
63