• 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.83
/src/Metadata/Property/Factory/SerializerPropertyMetadataFactory.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\Metadata\Property\Factory;
15

16
use ApiPlatform\Metadata\ApiProperty;
17
use ApiPlatform\Metadata\Exception\ResourceClassNotFoundException;
18
use ApiPlatform\Metadata\ResourceClassResolverInterface;
19
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
20
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
21
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface as SerializerClassMetadataFactoryInterface;
22

23
/**
24
 * Populates read/write and link status using serialization groups.
25
 *
26
 * @author Kévin Dunglas <dunglas@gmail.com>
27
 * @author Teoh Han Hui <teohhanhui@gmail.com>
28
 */
29
final class SerializerPropertyMetadataFactory implements PropertyMetadataFactoryInterface
30
{
31
    use ResourceClassInfoTrait;
32

33
    public function __construct(private readonly SerializerClassMetadataFactoryInterface $serializerClassMetadataFactory, private readonly PropertyMetadataFactoryInterface $decorated, ?ResourceClassResolverInterface $resourceClassResolver = null)
34
    {
UNCOV
35
        $this->resourceClassResolver = $resourceClassResolver;
2,251✔
36
    }
37

38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function create(string $resourceClass, string $property, array $options = []): ApiProperty
42
    {
UNCOV
43
        $propertyMetadata = $this->decorated->create($resourceClass, $property, $options);
532✔
44

45
        try {
UNCOV
46
            [$normalizationGroups, $denormalizationGroups] = $this->getEffectiveSerializerGroups($options);
532✔
47

UNCOV
48
            if ($normalizationGroups && !\is_array($normalizationGroups)) {
532✔
49
                $normalizationGroups = [$normalizationGroups];
×
50
            }
51

UNCOV
52
            if ($denormalizationGroups && !\is_array($denormalizationGroups)) {
532✔
UNCOV
53
                $denormalizationGroups = [$denormalizationGroups];
532✔
54
            }
55
        } catch (ResourceClassNotFoundException) {
×
56
            // TODO: for input/output classes, the serializer groups must be read from the actual resource class
57
            return $propertyMetadata;
×
58
        }
59

UNCOV
60
        $propertyMetadata = $this->transformReadWrite($propertyMetadata, $resourceClass, $property, $normalizationGroups, $denormalizationGroups);
532✔
UNCOV
61
        $types = $propertyMetadata->getBuiltinTypes() ?? [];
532✔
62

UNCOV
63
        if (!$this->isResourceClass($resourceClass) && $types) {
532✔
UNCOV
64
            foreach ($types as $builtinType) {
77✔
UNCOV
65
                if ($builtinType->isCollection()) {
77✔
UNCOV
66
                    return $propertyMetadata->withReadableLink(true)->withWritableLink(true);
14✔
67
                }
68
            }
69
        }
70

UNCOV
71
        return $this->transformLinkStatus($propertyMetadata, $normalizationGroups, $denormalizationGroups, $types);
532✔
72
    }
73

74
    /**
75
     * Sets readable/writable based on matching normalization/denormalization groups and property's ignorance.
76
     *
77
     * A false value is never reset as it could be unreadable/unwritable for other reasons.
78
     * If normalization/denormalization groups are not specified and the property is not ignored, the property is implicitly readable/writable.
79
     *
80
     * @param string[]|null $normalizationGroups
81
     * @param string[]|null $denormalizationGroups
82
     */
83
    private function transformReadWrite(ApiProperty $propertyMetadata, string $resourceClass, string $propertyName, ?array $normalizationGroups = null, ?array $denormalizationGroups = null): ApiProperty
84
    {
UNCOV
85
        $serializerAttributeMetadata = $this->getSerializerAttributeMetadata($resourceClass, $propertyName);
532✔
UNCOV
86
        $groups = $serializerAttributeMetadata ? $serializerAttributeMetadata->getGroups() : [];
532✔
UNCOV
87
        $ignored = $serializerAttributeMetadata && $serializerAttributeMetadata->isIgnored();
532✔
88

UNCOV
89
        if (false !== $propertyMetadata->isReadable()) {
532✔
UNCOV
90
            $propertyMetadata = $propertyMetadata->withReadable(!$ignored && (null === $normalizationGroups || array_intersect($normalizationGroups, $groups)));
515✔
91
        }
92

UNCOV
93
        if (false !== $propertyMetadata->isWritable()) {
532✔
UNCOV
94
            $propertyMetadata = $propertyMetadata->withWritable(!$ignored && (null === $denormalizationGroups || array_intersect($denormalizationGroups, $groups)));
489✔
95
        }
96

UNCOV
97
        return $propertyMetadata;
532✔
98
    }
99

100
    /**
101
     * Sets readableLink/writableLink based on matching normalization/denormalization groups.
102
     *
103
     * If normalization/denormalization groups are not specified,
104
     * set link status to false since embedding of resource must be explicitly enabled
105
     *
106
     * @param string[]|null $normalizationGroups
107
     * @param string[]|null $denormalizationGroups
108
     */
109
    private function transformLinkStatus(ApiProperty $propertyMetadata, ?array $normalizationGroups = null, ?array $denormalizationGroups = null, ?array $types = null): ApiProperty
110
    {
111
        // No need to check link status if property is not readable and not writable
UNCOV
112
        if (false === $propertyMetadata->isReadable() && false === $propertyMetadata->isWritable()) {
532✔
UNCOV
113
            return $propertyMetadata;
104✔
114
        }
115

UNCOV
116
        foreach ($types as $type) {
488✔
117
            if (
UNCOV
118
                $type->isCollection()
464✔
UNCOV
119
                && $collectionValueType = $type->getCollectionValueTypes()[0] ?? null
464✔
120
            ) {
UNCOV
121
                $relatedClass = $collectionValueType->getClassName();
116✔
122
            } else {
UNCOV
123
                $relatedClass = $type->getClassName();
464✔
124
            }
125

126
            // if property is not a resource relation, don't set link status (as it would have no meaning)
UNCOV
127
            if (null === $relatedClass || !$this->isResourceClass($relatedClass)) {
464✔
UNCOV
128
                continue;
451✔
129
            }
130

131
            // find the resource class
132
            // this prevents serializer groups on non-resource child class from incorrectly influencing the decision
UNCOV
133
            if (null !== $this->resourceClassResolver) {
225✔
UNCOV
134
                $relatedClass = $this->resourceClassResolver->getResourceClass(null, $relatedClass);
225✔
135
            }
136

UNCOV
137
            $relatedGroups = $this->getClassSerializerGroups($relatedClass);
225✔
138

UNCOV
139
            if (null === $propertyMetadata->isReadableLink()) {
225✔
UNCOV
140
                $propertyMetadata = $propertyMetadata->withReadableLink(null !== $normalizationGroups && !empty(array_intersect($normalizationGroups, $relatedGroups)));
225✔
141
            }
142

UNCOV
143
            if (null === $propertyMetadata->isWritableLink()) {
225✔
UNCOV
144
                $propertyMetadata = $propertyMetadata->withWritableLink(null !== $denormalizationGroups && !empty(array_intersect($denormalizationGroups, $relatedGroups)));
225✔
145
            }
146

UNCOV
147
            return $propertyMetadata;
225✔
148
        }
149

UNCOV
150
        return $propertyMetadata;
475✔
151
    }
152

153
    /**
154
     * Gets the effective serializer groups used in normalization/denormalization.
155
     *
156
     * Groups are extracted in the following order:
157
     *
158
     * - From the "serializer_groups" key of the $options array.
159
     * - From metadata of the given operation ("operation_name" key).
160
     * - From metadata of the current resource.
161
     *
162
     * @return (string[]|string|null)[]
163
     */
164
    private function getEffectiveSerializerGroups(array $options): array
165
    {
UNCOV
166
        if (isset($options['serializer_groups'])) {
532✔
UNCOV
167
            $groups = (array) $options['serializer_groups'];
229✔
168

UNCOV
169
            return [$groups, $groups];
229✔
170
        }
171

UNCOV
172
        if (\array_key_exists('normalization_groups', $options) && \array_key_exists('denormalization_groups', $options)) {
324✔
UNCOV
173
            return [$options['normalization_groups'] ?? null, $options['denormalization_groups'] ?? null];
291✔
174
        }
175

UNCOV
176
        return [null, null];
72✔
177
    }
178

179
    private function getSerializerAttributeMetadata(string $class, string $attribute): ?AttributeMetadataInterface
180
    {
UNCOV
181
        $serializerClassMetadata = $this->serializerClassMetadataFactory->getMetadataFor($class);
532✔
182

UNCOV
183
        foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) {
532✔
UNCOV
184
            if ($attribute === $serializerAttributeMetadata->getName()) {
531✔
UNCOV
185
                return $serializerAttributeMetadata;
515✔
186
            }
187
        }
188

UNCOV
189
        return null;
33✔
190
    }
191

192
    /**
193
     * Gets all serializer groups used in a class.
194
     *
195
     * @return string[]
196
     */
197
    private function getClassSerializerGroups(string $class): array
198
    {
UNCOV
199
        $serializerClassMetadata = $this->serializerClassMetadataFactory->getMetadataFor($class);
225✔
200

UNCOV
201
        $groups = [];
225✔
UNCOV
202
        foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) {
225✔
UNCOV
203
            $groups[] = $serializerAttributeMetadata->getGroups();
225✔
204
        }
205

UNCOV
206
        return array_unique(array_merge(...$groups));
225✔
207
    }
208
}
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

© 2024 Coveralls, Inc