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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

92.11
/src/Hydra/State/JsonStreamerProcessor.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\State;
15

16
use ApiPlatform\Hydra\Collection;
17
use ApiPlatform\Hydra\State\Util\PaginationHelperTrait;
18
use ApiPlatform\Hydra\State\Util\SearchHelperTrait;
19
use ApiPlatform\Metadata\CollectionOperationInterface;
20
use ApiPlatform\Metadata\Error;
21
use ApiPlatform\Metadata\HttpOperation;
22
use ApiPlatform\Metadata\IriConverterInterface;
23
use ApiPlatform\Metadata\Operation;
24
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
25
use ApiPlatform\Metadata\ResourceClassResolverInterface;
26
use ApiPlatform\Metadata\UrlGeneratorInterface;
27
use ApiPlatform\State\Pagination\PaginatorInterface;
28
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
29
use ApiPlatform\State\ProcessorInterface;
30
use ApiPlatform\State\Util\HttpResponseHeadersTrait;
31
use ApiPlatform\State\Util\HttpResponseStatusTrait;
32
use Symfony\Component\HttpFoundation\Response;
33
use Symfony\Component\HttpFoundation\StreamedResponse;
34
use Symfony\Component\JsonStreamer\StreamWriterInterface;
35
use Symfony\Component\TypeInfo\Type;
36

37
/**
38
 * @implements ProcessorInterface<mixed,mixed>
39
 */
40
final class JsonStreamerProcessor implements ProcessorInterface
41
{
42
    use HttpResponseHeadersTrait;
43
    use HttpResponseStatusTrait;
44
    use PaginationHelperTrait;
45
    use SearchHelperTrait;
46

47
    /**
48
     * @param ProcessorInterface<mixed,mixed>|null       $processor
49
     * @param StreamWriterInterface<array<string,mixed>> $jsonStreamer
50
     */
51
    public function __construct(
52
        private readonly ?ProcessorInterface $processor,
53
        private readonly StreamWriterInterface $jsonStreamer,
54
        ?IriConverterInterface $iriConverter = null,
55
        ?ResourceClassResolverInterface $resourceClassResolver = null,
56
        ?OperationMetadataFactoryInterface $operationMetadataFactory = null,
57
        private readonly string $pageParameterName = 'page',
58
        private readonly string $enabledParameterName = 'pagination',
59
        private readonly int $urlGenerationStrategy = UrlGeneratorInterface::ABS_PATH,
60
    ) {
61
        $this->resourceClassResolver = $resourceClassResolver;
574✔
62
        $this->iriConverter = $iriConverter;
574✔
63
        $this->operationMetadataFactory = $operationMetadataFactory;
574✔
64
    }
65

66
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
67
    {
68
        if (
69
            $operation instanceof Error
311✔
70
            || $data instanceof Response
311✔
71
            || !$operation instanceof HttpOperation
311✔
72
            || !($request = $context['request'] ?? null)
311✔
73
            || !$operation->getJsonStream()
311✔
74
            || 'jsonld' !== $request->getRequestFormat()
311✔
75
        ) {
76
            return $this->processor?->process($data, $operation, $uriVariables, $context);
305✔
77
        }
78

79
        if ($operation instanceof CollectionOperationInterface) {
6✔
80
            $requestUri = $request->getRequestUri() ?? '';
2✔
81
            $collection = new Collection();
2✔
82
            $collection->member = $data;
2✔
83
            $collection->view = $this->getPartialCollectionView($data, $requestUri, $this->pageParameterName, $this->enabledParameterName, $operation->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy);
2✔
84

85
            if ($operation->getParameters()) {
2✔
86
                $parts = parse_url($requestUri);
×
87
                $collection->search = $this->getSearch($parts['path'] ?? '', $operation);
×
88
            }
89

90
            if ($data instanceof PaginatorInterface) {
2✔
91
                $collection->totalItems = $data->getTotalItems();
×
92
            }
93

94
            if (\is_array($data) || ($data instanceof \Countable && !$data instanceof PartialPaginatorInterface)) {
2✔
95
                $collection->totalItems = \count($data);
2✔
96
            }
97

98
            $data = $this->jsonStreamer->write(
2✔
99
                $collection,
2✔
100
                Type::generic(Type::object($collection::class), Type::object($operation->getClass())),
2✔
101
                ['data' => $data, 'operation' => $operation],
2✔
102
            );
2✔
103
        } else {
104
            $data = $this->jsonStreamer->write(
4✔
105
                $data,
4✔
106
                Type::object($operation->getClass()),
4✔
107
                ['data' => $data, 'operation' => $operation],
4✔
108
            );
4✔
109
        }
110

111
        /** @var iterable<string> $data */
112
        $response = new StreamedResponse(
6✔
113
            $data,
6✔
114
            $this->getStatus($request, $operation, $context),
6✔
115
            $this->getHeaders($request, $operation, $context)
6✔
116
        );
6✔
117

118
        return $this->processor ? $this->processor->process($response, $operation, $uriVariables, $context) : $response;
6✔
119
    }
120
}
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