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

api-platform / core / 10885997573

16 Sep 2024 02:31PM UTC coverage: 6.961% (-0.3%) from 7.281%
10885997573

push

github

web-flow
 feat(laravel): filter validations rules

* refactor(metadata): move parameter validation to the validator component

* feat(laravel): validations rules filters

* cs fixes

* fix(laravel): eloquent filters validation

* fix(laravel): eloquent filters

* fixes

* fix

---------

Co-authored-by: soyuka <soyuka@users.noreply.github.com>
Co-authored-by: Nathan <nathan@les-tilleuls.coop>

87 of 188 new or added lines in 6 files covered. (46.28%)

7 existing lines in 2 files now uncovered.

11475 of 164843 relevant lines covered (6.96%)

14.22 hits per line

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

0.0
/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.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\Laravel\Metadata;
15

16
use ApiPlatform\Metadata\Parameter;
17
use ApiPlatform\Metadata\Parameters;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
20
use Illuminate\Validation\Rule;
21
use Psr\Container\ContainerInterface;
22

23
final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
24
{
25
    public function __construct(
26
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
27
        private readonly ?ContainerInterface $filterLocator = null,
28
    ) {
NEW
29
    }
×
30

31
    public function create(string $resourceClass): ResourceMetadataCollection
32
    {
NEW
33
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
×
34

NEW
35
        foreach ($resourceMetadataCollection as $i => $resource) {
×
NEW
36
            $operations = $resource->getOperations();
×
37

NEW
38
            foreach ($operations as $operationName => $operation) {
×
NEW
39
                $parameters = $operation->getParameters() ?? new Parameters();
×
NEW
40
                foreach ($parameters as $key => $parameter) {
×
NEW
41
                    $parameters->add($key, $this->addSchemaValidation($parameter));
×
42
                }
43

NEW
44
                if (\count($parameters) > 0) {
×
NEW
45
                    $operations->add($operationName, $operation->withParameters($parameters));
×
46
                }
47
            }
48

NEW
49
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
×
50

NEW
51
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
×
NEW
52
                continue;
×
53
            }
54

NEW
55
            foreach ($graphQlOperations as $operationName => $operation) {
×
NEW
56
                $parameters = $operation->getParameters() ?? new Parameters();
×
NEW
57
                foreach ($operation->getParameters() ?? [] as $key => $parameter) {
×
NEW
58
                    $parameters->add($key, $this->addSchemaValidation($parameter));
×
59
                }
60

NEW
61
                if (\count($parameters) > 0) {
×
NEW
62
                    $graphQlOperations[$operationName] = $operation->withParameters($parameters);
×
63
                }
64
            }
65

NEW
66
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
×
67
        }
68

NEW
69
        return $resourceMetadataCollection;
×
70
    }
71

72
    private function addSchemaValidation(Parameter $parameter): Parameter
73
    {
NEW
74
        $schema = $parameter->getSchema();
×
NEW
75
        $required = $parameter->getRequired();
×
NEW
76
        $openApi = $parameter->getOpenApi();
×
77

78
        // When it's an array of openapi parameters take the first one as it's probably just a variant of the query parameter,
79
        // only getAllowEmptyValue is used here anyways
NEW
80
        if (\is_array($openApi)) {
×
NEW
81
            $openApi = $openApi[0];
×
82
        }
NEW
83
        $assertions = [];
×
NEW
84
        $allowEmptyValue = $openApi?->getAllowEmptyValue();
×
NEW
85
        if ($required || (false === $required && false === $allowEmptyValue)) {
×
NEW
86
            $assertions[] = 'required';
×
87
        }
88

NEW
89
        if (true === $allowEmptyValue) {
×
NEW
90
            $assertions[] = 'nullable';
×
91
        }
92

NEW
93
        if (isset($schema['exclusiveMinimum'])) {
×
NEW
94
            $assertions[] = 'gt:'.$schema['exclusiveMinimum'];
×
95
        }
96

NEW
97
        if (isset($schema['exclusiveMaximum'])) {
×
NEW
98
            $assertions[] = 'lt:'.$schema['exclusiveMaximum'];
×
99
        }
100

NEW
101
        if (isset($schema['minimum'])) {
×
NEW
102
            $assertions[] = 'gte:'.$schema['minimum'];
×
103
        }
104

NEW
105
        if (isset($schema['maximum'])) {
×
NEW
106
            $assertions[] = 'lte:'.$schema['maximum'];
×
107
        }
108

NEW
109
        if (isset($schema['pattern'])) {
×
NEW
110
            $assertions[] = 'regex:'.$schema['pattern'];
×
111
        }
112

NEW
113
        $minLength = isset($schema['minLength']);
×
NEW
114
        $maxLength = isset($schema['maxLength']);
×
115

NEW
116
        if ($minLength && $maxLength) {
×
NEW
117
            $assertions[] = \sprintf('between:%s,%s', $schema['minLength'], $schema['maxLength']);
×
NEW
118
        } elseif ($minLength) {
×
NEW
119
            $assertions[] = 'min:'.$schema['minLength'];
×
NEW
120
        } elseif ($maxLength) {
×
NEW
121
            $assertions[] = 'max:'.$schema['maxLength'];
×
122
        }
123

NEW
124
        $minItems = isset($schema['minItems']);
×
NEW
125
        $maxItems = isset($schema['maxItems']);
×
126

NEW
127
        if ($minItems && $maxItems) {
×
NEW
128
            $assertions[] = \sprintf('between:%s,%s', $schema['minItems'], $schema['maxItems']);
×
NEW
129
        } elseif ($minItems) {
×
NEW
130
            $assertions[] = 'min:'.$schema['minItems'];
×
NEW
131
        } elseif ($maxItems) {
×
NEW
132
            $assertions[] = 'max:'.$schema['maxItems'];
×
133
        }
134

NEW
135
        if (isset($schema['multipleOf'])) {
×
NEW
136
            $assertions[] = 'multiple_of:'.$schema['multipleOf'];
×
137
        }
138

139
        //        if (isset($schema['enum'])) {
140
        //            $assertions[] = [Rule::enum($schema['enum'])];
141
        //        }
142

NEW
143
        if (isset($schema['type']) && 'array' === $schema['type']) {
×
NEW
144
            $assertions[] = 'array';
×
145
        }
146

NEW
147
        if (!$assertions) {
×
NEW
148
            return $parameter;
×
149
        }
150

NEW
151
        if (1 === \count($assertions)) {
×
NEW
152
            return $parameter->withConstraints($assertions[0]);
×
153
        }
154

NEW
155
        return $parameter->withConstraints($assertions);
×
156
    }
157
}
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