Skip to content

Commit 4f59677

Browse files
aegypiussoyuka
andcommitted
feat(parametervalidator): create api-platform/parameter-validator component
Co-authored-by: Antoine Bluchet <[email protected]>
1 parent bbc99cf commit 4f59677

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+885
-114
lines changed

.commitlintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"jsonapi",
1717
"graphql",
1818
"openapi",
19+
"parametervalidator",
1920
"serializer",
2021
"jsonschema",
2122
"validation",

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
with:
2323
fetch-depth: 0
2424
- name: Run commitlint
25-
run: |
25+
run: |
2626
commit=$(gh api \
2727
/repos/${{ github.repository }}/pulls/${{github.event.number}}/commits \
2828
| jq -r '.[0].commit.message' \
@@ -197,11 +197,13 @@ jobs:
197197
- JsonSchema
198198
- OpenApi
199199
- Metadata
200+
- ParameterValidator
200201
- Elasticsearch
201202
- HttpCache
202203
- RamseyUuid
203204
- GraphQl
204205
- Serializer
206+
- Symfony
205207
fail-fast: false
206208
steps:
207209
- name: Checkout
@@ -1139,4 +1141,3 @@ jobs:
11391141
name: openapi-docs-php${{ matrix.php }}
11401142
path: build/out/openapi
11411143
continue-on-error: true
1142-

src/Api/QueryParameterValidator/QueryParameterValidator.php

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,63 +13,13 @@
1313

1414
namespace ApiPlatform\Api\QueryParameterValidator;
1515

16-
use ApiPlatform\Api\FilterLocatorTrait;
17-
use ApiPlatform\Api\QueryParameterValidator\Validator\ArrayItems;
18-
use ApiPlatform\Api\QueryParameterValidator\Validator\Bounds;
19-
use ApiPlatform\Api\QueryParameterValidator\Validator\Enum;
20-
use ApiPlatform\Api\QueryParameterValidator\Validator\Length;
21-
use ApiPlatform\Api\QueryParameterValidator\Validator\MultipleOf;
22-
use ApiPlatform\Api\QueryParameterValidator\Validator\Pattern;
23-
use ApiPlatform\Api\QueryParameterValidator\Validator\Required;
24-
use ApiPlatform\Exception\FilterValidationException;
25-
use Psr\Container\ContainerInterface;
16+
use ApiPlatform\ParameterValidator\ParameterValidator as NewQueryParameterValidator;
2617

2718
/**
2819
* Validates query parameters depending on filter description.
2920
*
30-
* @author Julien Deniau <[email protected]>
21+
* @deprecated use ApiPlatform\QueryParameterValidator\QueryParameterValidator instead
3122
*/
32-
class QueryParameterValidator
23+
class QueryParameterValidator extends NewQueryParameterValidator
3324
{
34-
use FilterLocatorTrait;
35-
36-
private array $validators;
37-
38-
public function __construct(ContainerInterface $filterLocator)
39-
{
40-
$this->setFilterLocator($filterLocator);
41-
42-
$this->validators = [
43-
new ArrayItems(),
44-
new Bounds(),
45-
new Enum(),
46-
new Length(),
47-
new MultipleOf(),
48-
new Pattern(),
49-
new Required(),
50-
];
51-
}
52-
53-
public function validateFilters(string $resourceClass, array $resourceFilters, array $queryParameters): void
54-
{
55-
$errorList = [];
56-
57-
foreach ($resourceFilters as $filterId) {
58-
if (!$filter = $this->getFilter($filterId)) {
59-
continue;
60-
}
61-
62-
foreach ($filter->getDescription($resourceClass) as $name => $data) {
63-
foreach ($this->validators as $validator) {
64-
if ($errors = $validator->validate($name, $data, $queryParameters)) {
65-
$errorList[] = $errors;
66-
}
67-
}
68-
}
69-
}
70-
71-
if ($errorList) {
72-
throw new FilterValidationException(array_merge(...$errorList));
73-
}
74-
}
7525
}

src/Api/QueryParameterValidator/Validator/ArrayItems.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
namespace ApiPlatform\Api\QueryParameterValidator\Validator;
1515

16+
use ApiPlatform\ParameterValidator\Validator\CheckFilterDeprecationsTrait;
17+
use ApiPlatform\ParameterValidator\Validator\ValidatorInterface;
18+
19+
/**
20+
* @deprecated use \ApiPlatform\ParameterValidator\Validator\ArrayItems instead
21+
*/
1622
final class ArrayItems implements ValidatorInterface
1723
{
1824
use CheckFilterDeprecationsTrait;

src/Api/QueryParameterValidator/Validator/Bounds.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
namespace ApiPlatform\Api\QueryParameterValidator\Validator;
1515

16+
use ApiPlatform\ParameterValidator\Validator\CheckFilterDeprecationsTrait;
17+
use ApiPlatform\ParameterValidator\Validator\ValidatorInterface;
18+
19+
/**
20+
* @deprecated use \ApiPlatform\ParameterValidator\Validator\Bounds instead
21+
*/
1622
final class Bounds implements ValidatorInterface
1723
{
1824
use CheckFilterDeprecationsTrait;

src/Api/QueryParameterValidator/Validator/Enum.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
namespace ApiPlatform\Api\QueryParameterValidator\Validator;
1515

16+
use ApiPlatform\ParameterValidator\Validator\CheckFilterDeprecationsTrait;
17+
use ApiPlatform\ParameterValidator\Validator\ValidatorInterface;
18+
19+
/**
20+
* @deprecated use \ApiPlatform\ParameterValidator\Validator\Enum instead
21+
*/
1622
final class Enum implements ValidatorInterface
1723
{
1824
use CheckFilterDeprecationsTrait;

src/Api/QueryParameterValidator/Validator/Length.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
namespace ApiPlatform\Api\QueryParameterValidator\Validator;
1515

16+
use ApiPlatform\ParameterValidator\Validator\CheckFilterDeprecationsTrait;
17+
use ApiPlatform\ParameterValidator\Validator\ValidatorInterface;
18+
19+
/**
20+
* @deprecated use \ApiPlatform\ParameterValidator\Validator\Length instead
21+
*/
1622
final class Length implements ValidatorInterface
1723
{
1824
use CheckFilterDeprecationsTrait;

src/Api/QueryParameterValidator/Validator/MultipleOf.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
namespace ApiPlatform\Api\QueryParameterValidator\Validator;
1515

16+
use ApiPlatform\ParameterValidator\Validator\CheckFilterDeprecationsTrait;
17+
use ApiPlatform\ParameterValidator\Validator\ValidatorInterface;
18+
19+
/**
20+
* @deprecated use \ApiPlatform\ParameterValidator\Validator\MultipleOf instead
21+
*/
1622
final class MultipleOf implements ValidatorInterface
1723
{
1824
use CheckFilterDeprecationsTrait;

src/Api/QueryParameterValidator/Validator/Pattern.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
namespace ApiPlatform\Api\QueryParameterValidator\Validator;
1515

16+
use ApiPlatform\ParameterValidator\Validator\CheckFilterDeprecationsTrait;
17+
use ApiPlatform\ParameterValidator\Validator\ValidatorInterface;
18+
19+
/**
20+
* @deprecated use \ApiPlatform\ParameterValidator\Validator\Pattern instead
21+
*/
1622
final class Pattern implements ValidatorInterface
1723
{
1824
use CheckFilterDeprecationsTrait;

src/Api/QueryParameterValidator/Validator/Required.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313

1414
namespace ApiPlatform\Api\QueryParameterValidator\Validator;
1515

16+
use ApiPlatform\ParameterValidator\Validator\CheckFilterDeprecationsTrait;
17+
use ApiPlatform\ParameterValidator\Validator\ValidatorInterface;
1618
use ApiPlatform\State\Util\RequestParser;
1719

20+
/**
21+
* @deprecated use \ApiPlatform\ParameterValidator\Validator\Required instead
22+
*/
1823
final class Required implements ValidatorInterface
1924
{
2025
use CheckFilterDeprecationsTrait;

src/Api/QueryParameterValidator/Validator/ValidatorInterface.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@
1313

1414
namespace ApiPlatform\Api\QueryParameterValidator\Validator;
1515

16-
interface ValidatorInterface
16+
use ApiPlatform\ParameterValidator\Validator as ParameterValidatorComponent;
17+
18+
/** @deprecated use \ApiPlatform\ParameterValidator\Validator\ValidatorInterface instead */
19+
interface ValidatorInterface extends ParameterValidatorComponent\ValidatorInterface
1720
{
18-
/**
19-
* @param string $name the parameter name to validate
20-
* @param array<string, mixed> $filterDescription the filter descriptions as returned by `\ApiPlatform\Api\FilterInterface::getDescription()`
21-
* @param array<string, mixed> $queryParameters the list of query parameter
22-
*/
23-
public function validate(string $name, array $filterDescription, array $queryParameters): array;
2421
}

src/Exception/FilterValidationException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313

1414
namespace ApiPlatform\Exception;
1515

16+
use ApiPlatform\ParameterValidator\Exception\ValidationExceptionInterface;
17+
1618
/**
1719
* Filter validation exception.
1820
*
1921
* @author Julien DENIAU <[email protected]>
22+
*
23+
* @deprecated use \ApiPlatform\Metadata\Exception\ValidationException instead
2024
*/
21-
final class FilterValidationException extends \Exception implements ExceptionInterface, \Stringable
25+
final class FilterValidationException extends \Exception implements ValidationExceptionInterface, ExceptionInterface, \Stringable
2226
{
2327
public function __construct(private readonly array $constraintViolationList, string $message = '', int $code = 0, \Exception $previous = null)
2428
{

src/ParameterValidator/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/composer.lock
2+
/vendor
3+
/.phpunit.result.cache
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\ParameterValidator\Exception;
15+
16+
/**
17+
* Filter validation exception.
18+
*
19+
* @author Julien DENIAU <[email protected]>
20+
*/
21+
final class ValidationException extends \Exception implements ValidationExceptionInterface
22+
{
23+
public function __construct(private readonly array $constraintViolationList, string $message = '', int $code = 0, \Exception $previous = null)
24+
{
25+
parent::__construct($message ?: $this->__toString(), $code, $previous);
26+
}
27+
28+
public function __toString(): string
29+
{
30+
return implode("\n", $this->constraintViolationList);
31+
}
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\ParameterValidator\Exception;
15+
16+
use ApiPlatform\Metadata\Exception\ExceptionInterface;
17+
18+
/**
19+
* This Exception is thrown when any parameter validation fails.
20+
*/
21+
interface ValidationExceptionInterface extends ExceptionInterface, \Stringable
22+
{
23+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\ParameterValidator;
15+
16+
use ApiPlatform\Exception\InvalidArgumentException;
17+
use ApiPlatform\Metadata\FilterInterface;
18+
use Psr\Container\ContainerInterface;
19+
20+
/**
21+
* Manipulates filters with a backward compatibility between the new filter locator and the deprecated filter collection.
22+
*
23+
* @author Baptiste Meyer <[email protected]>
24+
*
25+
* @deprecated
26+
*
27+
* @internal
28+
*/
29+
trait FilterLocatorTrait
30+
{
31+
private ?ContainerInterface $filterLocator = null;
32+
33+
/**
34+
* Sets a filter locator with a backward compatibility.
35+
*/
36+
private function setFilterLocator(?ContainerInterface $filterLocator, bool $allowNull = false): void
37+
{
38+
if ($filterLocator instanceof ContainerInterface || (null === $filterLocator && $allowNull)) {
39+
$this->filterLocator = $filterLocator;
40+
} else {
41+
throw new InvalidArgumentException(sprintf('The "$filterLocator" argument is expected to be an implementation of the "%s" interface%s.', ContainerInterface::class, $allowNull ? ' or null' : ''));
42+
}
43+
}
44+
45+
/**
46+
* Gets a filter with a backward compatibility.
47+
*/
48+
private function getFilter(string $filterId): null|FilterInterface
49+
{
50+
if ($this->filterLocator && $this->filterLocator->has($filterId)) {
51+
return $this->filterLocator->get($filterId);
52+
}
53+
54+
return null;
55+
}
56+
}

src/ParameterValidator/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT license
2+
3+
Copyright (c) 2015-present Kévin Dunglas
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is furnished
10+
to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

0 commit comments

Comments
 (0)