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

api-platform / core / 10739011304

06 Sep 2024 01:11PM UTC coverage: 7.159% (-0.5%) from 7.645%
10739011304

push

github

web-flow
 feat(laravel): eloquent filters (search, date, equals, or) (#6593)

* feat(laravel): Eloquent filters search date or

* feat(laravel): eloquent filters order range equals afterdate

* fix(laravel): order afterDate filters

* temp

* test(laravel): filter with eloquent

---------

Co-authored-by: Nathan <nathan@les-tilleuls.coop>

0 of 144 new or added lines in 16 files covered. (0.0%)

4792 existing lines in 155 files now uncovered.

11736 of 163930 relevant lines covered (7.16%)

22.8 hits per line

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

94.44
/src/JsonLd/ContextBuilder.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\JsonLd;
15

16
use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait;
17
use ApiPlatform\Metadata\Get;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\IriConverterInterface;
20
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
24
use ApiPlatform\Metadata\UrlGeneratorInterface;
25
use ApiPlatform\Metadata\Util\ClassInfoTrait;
26
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
27

28
/**
29
 * {@inheritdoc}
30
 *
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
final class ContextBuilder implements AnonymousContextBuilderInterface
34
{
35
    use ClassInfoTrait;
36
    use HydraPrefixTrait;
37

38
    public const FORMAT = 'jsonld';
39
    public const HYDRA_PREFIX = 'hydra:';
40
    public const HYDRA_CONTEXT_HAS_PREFIX = 'hydra_prefix';
41

42
    public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly UrlGeneratorInterface $urlGenerator, private readonly ?IriConverterInterface $iriConverter = null, private readonly ?NameConverterInterface $nameConverter = null, private array $defaultContext = [])
43
    {
UNCOV
44
    }
2,258✔
45

46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getBaseContext(int $referenceType = UrlGeneratorInterface::ABS_URL): array
50
    {
UNCOV
51
        return [
116✔
UNCOV
52
            '@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
116✔
UNCOV
53
            'hydra' => self::HYDRA_NS,
116✔
UNCOV
54
        ];
116✔
55
    }
56

57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getEntrypointContext(int $referenceType = UrlGeneratorInterface::ABS_PATH): array
61
    {
UNCOV
62
        $context = $this->getBaseContext($referenceType);
3✔
63

UNCOV
64
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
3✔
UNCOV
65
            $shortName = $this->resourceMetadataFactory->create($resourceClass)[0]->getShortName();
3✔
UNCOV
66
            $resourceName = lcfirst($shortName);
3✔
67

UNCOV
68
            $context[$resourceName] = [
3✔
UNCOV
69
                '@id' => 'Entrypoint/'.$resourceName,
3✔
UNCOV
70
                '@type' => '@id',
3✔
UNCOV
71
            ];
3✔
72
        }
73

UNCOV
74
        return $context;
3✔
75
    }
76

77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getResourceContext(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): array
81
    {
82
        /** @var HttpOperation $operation */
UNCOV
83
        $operation = $this->resourceMetadataFactory->create($resourceClass)->getOperation(null, false, true);
18✔
UNCOV
84
        if (null === $shortName = $operation->getShortName()) {
18✔
85
            return [];
×
86
        }
87

UNCOV
88
        $context = $operation->getNormalizationContext();
18✔
UNCOV
89
        if ($context['iri_only'] ?? false) {
18✔
UNCOV
90
            $context = $this->getBaseContext($referenceType);
9✔
UNCOV
91
            $context[$this->getHydraPrefix($context).'member']['@type'] = '@id';
9✔
92

UNCOV
93
            return $context;
9✔
94
        }
95

UNCOV
96
        return $this->getResourceContextWithShortname($resourceClass, $referenceType, $shortName, $operation);
9✔
97
    }
98

99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getResourceContextUri(string $resourceClass, ?int $referenceType = null): string
103
    {
UNCOV
104
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass)[0];
1,259✔
UNCOV
105
        if (null === $referenceType) {
1,259✔
UNCOV
106
            $referenceType = $resourceMetadata->getUrlGenerationStrategy();
1,259✔
107
        }
108

UNCOV
109
        return $this->urlGenerator->generate('api_jsonld_context', ['shortName' => $resourceMetadata->getShortName()], $referenceType ?? UrlGeneratorInterface::ABS_PATH);
1,259✔
110
    }
111

112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function getAnonymousResourceContext(object $object, array $context = [], int $referenceType = UrlGeneratorInterface::ABS_PATH): array
116
    {
UNCOV
117
        $outputClass = $this->getObjectClass($object);
95✔
UNCOV
118
        $operation = $context['operation'] ?? new Get(shortName: (new \ReflectionClass($outputClass))->getShortName());
95✔
UNCOV
119
        $shortName = $operation->getShortName();
95✔
120

UNCOV
121
        $jsonLdContext = [
95✔
UNCOV
122
            '@context' => $this->getResourceContextWithShortname(
95✔
UNCOV
123
                $outputClass,
95✔
UNCOV
124
                $referenceType,
95✔
UNCOV
125
                $shortName
95✔
UNCOV
126
            ),
95✔
UNCOV
127
            '@type' => $shortName,
95✔
UNCOV
128
        ];
95✔
129

UNCOV
130
        if (isset($context['iri'])) {
95✔
131
            $jsonLdContext['@id'] = $context['iri'];
×
UNCOV
132
        } elseif (true === ($context['gen_id'] ?? true) && $this->iriConverter) {
95✔
UNCOV
133
            $jsonLdContext['@id'] = $this->iriConverter->getIriFromResource($object);
91✔
134
        }
135

UNCOV
136
        if ($context['has_context'] ?? false) {
95✔
UNCOV
137
            unset($jsonLdContext['@context']);
80✔
138
        }
139

140
        // here the object can be different from the resource given by the $context['api_resource'] value
UNCOV
141
        if (isset($context['api_resource'])) {
95✔
142
            $jsonLdContext['@type'] = $this->resourceMetadataFactory->create($this->getObjectClass($context['api_resource']))[0]->getShortName();
×
143
        }
144

UNCOV
145
        return $jsonLdContext;
95✔
146
    }
147

148
    private function getResourceContextWithShortname(string $resourceClass, int $referenceType, string $shortName, ?HttpOperation $operation = null): array
149
    {
UNCOV
150
        $context = $this->getBaseContext($referenceType);
104✔
UNCOV
151
        $propertyContext = $operation ? ['normalization_groups' => $operation->getNormalizationContext()['groups'] ?? null, 'denormalization_groups' => $operation->getDenormalizationContext()['groups'] ?? null] : ['normalization_groups' => [], 'denormalization_groups' => []];
104✔
152

UNCOV
153
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
104✔
UNCOV
154
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName, $propertyContext);
103✔
155

UNCOV
156
            if ($propertyMetadata->isIdentifier() && true !== $propertyMetadata->isWritable()) {
103✔
UNCOV
157
                continue;
35✔
158
            }
159

UNCOV
160
            $convertedName = $this->nameConverter ? $this->nameConverter->normalize($propertyName, $resourceClass, self::FORMAT) : $propertyName;
103✔
UNCOV
161
            $jsonldContext = $propertyMetadata->getJsonldContext() ?? [];
103✔
162

UNCOV
163
            if ($id = $propertyMetadata->getIris()) {
103✔
UNCOV
164
                $id = 1 === (is_countable($id) ? \count($id) : 0) ? $id[0] : $id;
3✔
165
            }
166

UNCOV
167
            if (!$id) {
103✔
UNCOV
168
                $id = \sprintf('%s/%s', $shortName, $convertedName);
103✔
169
            }
170

UNCOV
171
            if (false === $propertyMetadata->isReadableLink()) {
103✔
UNCOV
172
                $jsonldContext += [
3✔
UNCOV
173
                    '@id' => $id,
3✔
UNCOV
174
                    '@type' => '@id',
3✔
UNCOV
175
                ];
3✔
176
            }
177

UNCOV
178
            if (empty($jsonldContext)) {
103✔
UNCOV
179
                $context[$convertedName] = $id;
100✔
180
            } else {
UNCOV
181
                $context[$convertedName] = $jsonldContext + [
6✔
UNCOV
182
                    '@id' => $id,
6✔
UNCOV
183
                ];
6✔
184
            }
185
        }
186

UNCOV
187
        if (false === ($this->defaultContext[self::HYDRA_CONTEXT_HAS_PREFIX] ?? true)) {
104✔
188
            return ['http://www.w3.org/ns/hydra/context.jsonld', $context];
×
189
        }
190

UNCOV
191
        return $context;
104✔
192
    }
193
}
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

© 2026 Coveralls, Inc