• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10943429050

19 Sep 2024 02:48PM UTC coverage: 7.647% (-0.03%) from 7.675%
10943429050

push

github

web-flow
feat: api-platform/json-hal component (#6621)

* feat: add hal support for laravel

* feat: quick review

* fix: typo & cs-fixer

* fix: typo in composer.json

* fix: cs-fixer & phpstan

* fix: forgot about hal item normalizer, therefore there's no more createbook nor updatebook test as Hal is a readonly format

0 of 94 new or added lines in 2 files covered. (0.0%)

9082 existing lines in 291 files now uncovered.

12629 of 165144 relevant lines covered (7.65%)

22.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.0
/src/Hydra/Serializer/CollectionNormalizer.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Hydra\Serializer;
15

16
use ApiPlatform\JsonLd\ContextBuilderInterface;
17
use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait;
18
use ApiPlatform\JsonLd\Serializer\JsonLdContextTrait;
19
use ApiPlatform\Metadata\IriConverterInterface;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\ResourceClassResolverInterface;
22
use ApiPlatform\Metadata\UrlGeneratorInterface;
23
use ApiPlatform\Serializer\AbstractCollectionNormalizer;
24
use ApiPlatform\State\Pagination\PaginatorInterface;
25
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
26

27
/**
28
 * This normalizer handles collections.
29
 *
30
 * @author Kevin Dunglas <dunglas@gmail.com>
31
 * @author Samuel ROZE <samuel.roze@gmail.com>
32
 */
33
final class CollectionNormalizer extends AbstractCollectionNormalizer
34
{
35
    use HydraPrefixTrait;
36
    use JsonLdContextTrait;
37

38
    public const FORMAT = 'jsonld';
39
    public const IRI_ONLY = 'iri_only';
40
    private array $defaultContext = [
41
        self::IRI_ONLY => false,
42
    ];
43

44
    public function __construct(private readonly ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, private readonly IriConverterInterface $iriConverter, readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, array $defaultContext = [])
45
    {
UNCOV
46
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
2,262✔
47

UNCOV
48
        if ($resourceMetadataCollectionFactory) {
2,262✔
49
            trigger_deprecation('api-platform/core', '3.0', \sprintf('Injecting "%s" within "%s" is not needed anymore and this dependency will be removed in 4.0.', ResourceMetadataCollectionFactoryInterface::class, self::class));
×
50
        }
51

UNCOV
52
        parent::__construct($resourceClassResolver, '');
2,262✔
53
    }
54

55
    /**
56
     * Gets the pagination data.
57
     */
58
    protected function getPaginationData(iterable $object, array $context = []): array
59
    {
UNCOV
60
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
551✔
UNCOV
61
        $hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
551✔
62
        // This adds "jsonld_has_context" by reference, we moved the code to this class.
63
        // To follow a note I wrote in the ItemNormalizer, we need to change the JSON-LD context generation as it is more complicated then it should.
UNCOV
64
        $data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
551✔
UNCOV
65
        $data['@id'] = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
551✔
UNCOV
66
        $data['@type'] = $hydraPrefix.'Collection';
551✔
67

UNCOV
68
        if ($object instanceof PaginatorInterface) {
551✔
UNCOV
69
            $data[$hydraPrefix.'totalItems'] = $object->getTotalItems();
494✔
70
        }
71

UNCOV
72
        if (\is_array($object) || ($object instanceof \Countable && !$object instanceof PartialPaginatorInterface)) {
551✔
UNCOV
73
            $data[$hydraPrefix.'totalItems'] = \count($object);
49✔
74
        }
75

UNCOV
76
        return $data;
551✔
77
    }
78

79
    /**
80
     * Gets items data.
81
     */
82
    protected function getItemsData(iterable $object, ?string $format = null, array $context = []): array
83
    {
UNCOV
84
        $hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
551✔
UNCOV
85
        $data = [$hydraPrefix.'member' => []];
551✔
UNCOV
86
        $iriOnly = $context[self::IRI_ONLY] ?? $this->defaultContext[self::IRI_ONLY];
551✔
87

UNCOV
88
        foreach ($object as $obj) {
551✔
UNCOV
89
            if ($iriOnly) {
499✔
UNCOV
90
                $data[$hydraPrefix.'member'][] = $this->iriConverter->getIriFromResource($obj);
3✔
91
            } else {
UNCOV
92
                $data[$hydraPrefix.'member'][] = $this->normalizer->normalize($obj, $format, $context + ['jsonld_has_context' => true]);
496✔
93
            }
94
        }
95

UNCOV
96
        return $data;
551✔
97
    }
98

99
    protected function initContext(string $resourceClass, array $context): array
100
    {
UNCOV
101
        $context = parent::initContext($resourceClass, $context);
551✔
UNCOV
102
        $context['api_collection_sub_level'] = true;
551✔
103

UNCOV
104
        return $context;
551✔
105
    }
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc