ReferenceManyFieldNormalizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
dl 0
loc 41
ccs 7
cts 7
cp 1
rs 10
c 1
b 0
f 1
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeField() 0 16 3
A __construct() 0 4 1
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\FieldNormalizerInterface;
9
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
10
use Chubbyphp\Serialization\Normalizer\NormalizerInterface;
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
    public function __construct(AccessorInterface $identifierAccessor, AccessorInterface $accessor)
26
    {
27
        $this->identifierAccessor = $identifierAccessor;
28
        $this->accessor = $accessor;
29 3
    }
30
31 3
    /**
32 3
     * @param object $object
33 3
     *
34
     * @throws SerializerLogicException
35
     *
36
     * @return mixed
37
     */
38
    public function normalizeField(
39
        string $path,
40
        $object,
41
        NormalizerContextInterface $context,
42
        NormalizerInterface $normalizer = null
43
    ) {
44
        if (null === $relatedObjects = $this->accessor->getValue($object)) {
45 3
            return null;
46
        }
47
48
        $values = [];
49
        foreach ($relatedObjects as $i => $relatedObject) {
50
            $values[$i] = $this->identifierAccessor->getValue($relatedObject);
51 3
        }
52 1
53
        return $values;
54
    }
55
}
56