Completed
Push — master ( f73309...4d4b19 )
by Antoine
03:25
created

CollectionNormalizer::normalize()   D

Complexity

Conditions 9
Paths 14

Size

Total Lines 34
Code Lines 20

Duplication

Lines 11
Ratio 32.35 %

Importance

Changes 0
Metric Value
dl 11
loc 34
c 0
b 0
f 0
rs 4.909
cc 9
eloc 20
nc 14
nop 3
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Hydra\Serializer;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use ApiPlatform\Core\Api\OperationType;
18
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
19
use ApiPlatform\Core\DataProvider\PaginatorInterface;
20
use ApiPlatform\Core\JsonLd\ContextBuilderInterface;
21
use ApiPlatform\Core\JsonLd\Serializer\JsonLdContextTrait;
22
use ApiPlatform\Core\Serializer\ContextTrait;
23
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
24
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
25
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
26
27
/**
28
 * This normalizer handles collections.
29
 *
30
 * @author Kevin Dunglas <[email protected]>
31
 * @author Samuel ROZE <[email protected]>
32
 */
33
final class CollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
34
{
35
    use ContextTrait;
36
    use JsonLdContextTrait;
37
    use NormalizerAwareTrait;
38
39
    const FORMAT = 'jsonld';
40
41
    private $contextBuilder;
42
    private $resourceClassResolver;
43
    private $iriConverter;
44
45
    public function __construct(ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, IriConverterInterface $iriConverter)
46
    {
47
        $this->contextBuilder = $contextBuilder;
48
        $this->resourceClassResolver = $resourceClassResolver;
49
        $this->iriConverter = $iriConverter;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function supportsNormalization($data, $format = null)
56
    {
57
        return self::FORMAT === $format && (is_array($data) || ($data instanceof \Traversable));
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function normalize($object, $format = null, array $context = [])
64
    {
65 View Code Duplication
        if (isset($context['api_sub_level'])) {
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...
66
            $data = [];
67
            foreach ($object as $index => $obj) {
68
                $data[$index] = $this->normalizer->normalize($obj, $format, $context);
69
            }
70
71
            return $data;
72
        }
73
74
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);
75
        $data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
76
        $context = $this->initContext($resourceClass, $context);
77
78
        if (isset($context['operation_type']) && $context['operation_type'] === OperationType::SUBRESOURCE) {
79
            $data['@id'] = $this->iriConverter->getSubresourceIriFromResourceClass($resourceClass, $context['subresource_identifiers']);
80
        } else {
81
            $data['@id'] = $this->iriConverter->getIriFromResourceClass($resourceClass);
82
        }
83
84
        $data['@type'] = 'hydra:Collection';
85
86
        $data['hydra:member'] = [];
87
        foreach ($object as $obj) {
88
            $data['hydra:member'][] = $this->normalizer->normalize($obj, $format, $context);
89
        }
90
91 View Code Duplication
        if (is_array($object) || $object instanceof \Countable) {
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...
92
            $data['hydra:totalItems'] = $object instanceof PaginatorInterface ? $object->getTotalItems() : count($object);
93
        }
94
95
        return $data;
96
    }
97
}
98