-
-
Notifications
You must be signed in to change notification settings - Fork 849
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
validate maxItems, minItems & uniqueItems filter
- Loading branch information
Showing
6 changed files
with
224 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Filter\Validator; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class ArrayItems implements ValidatorInterface | ||
{ | ||
public function validate(string $name, array $filterDescription, Request $request): array | ||
{ | ||
if (!$request->query->has($name)) { | ||
return []; | ||
} | ||
|
||
$maxItems = $filterDescription['swagger']['maxItems'] ?? null; | ||
$minItems = $filterDescription['swagger']['minItems'] ?? null; | ||
$uniqueItems = $filterDescription['swagger']['uniqueItems'] ?? false; | ||
|
||
$errorList = []; | ||
|
||
$value = $this->getValue($name, $filterDescription, $request); | ||
$nbItems = count($value); | ||
|
||
if (null !== $maxItems && $nbItems > $maxItems) { | ||
$errorList[] = sprintf('Query parameter "%s" must contain less than %d values', $name, $maxItems); | ||
} | ||
|
||
if (null !== $minItems && $nbItems < $minItems) { | ||
$errorList[] = sprintf('Query parameter "%s" must contain more than %d values', $name, $minItems); | ||
} | ||
|
||
if (true === $uniqueItems && $nbItems > count(array_unique($value))) { | ||
$errorList[] = sprintf('Query parameter "%s" must contain unique values', $name); | ||
} | ||
|
||
return $errorList; | ||
} | ||
|
||
private function getValue(string $name, array $filterDescription, Request $request): array | ||
{ | ||
$value = $request->query->get($name); | ||
|
||
if (empty($value) && '0' !== $value) { | ||
return []; | ||
} | ||
|
||
if (is_array($value)) { | ||
return $value; | ||
} | ||
|
||
$collectionFormat = $filterDescription['swagger']['collectionFormat'] ?? 'csv'; | ||
|
||
return explode(self::getSeparator($collectionFormat), $value); | ||
} | ||
|
||
private static function getSeparator(string $collectionFormat): string | ||
{ | ||
switch ($collectionFormat) { | ||
case 'csv': | ||
return ','; | ||
case 'ssv': | ||
return ' '; | ||
case 'tsv': | ||
return '\t'; | ||
case 'pipes': | ||
return '|'; | ||
default: | ||
throw new \InvalidArgumentException(sprintf('Unkwown collection format %s', $collectionFormat)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Filter; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractFilter; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
class ArrayItemsFilter extends AbstractFilter | ||
{ | ||
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null) | ||
{ | ||
} | ||
|
||
// This function is only used to hook in documentation generators (supported by Swagger and Hydra) | ||
public function getDescription(string $resourceClass): array | ||
{ | ||
return [ | ||
'csv-min-2' => [ | ||
'property' => 'csv-min-2', | ||
'type' => 'array', | ||
'required' => false, | ||
'swagger' => [ | ||
'minItems' => 2, | ||
], | ||
], | ||
'csv-max-3' => [ | ||
'property' => 'csv-max-3', | ||
'type' => 'array', | ||
'required' => false, | ||
'swagger' => [ | ||
'maxItems' => 3, | ||
], | ||
], | ||
'ssv-min-2' => [ | ||
'property' => 'ssv-min-2', | ||
'type' => 'array', | ||
'required' => false, | ||
'swagger' => [ | ||
'collectionFormat' => 'ssv', | ||
'minItems' => 2, | ||
], | ||
], | ||
'tsv-min-2' => [ | ||
'property' => 'tsv-min-2', | ||
'type' => 'array', | ||
'required' => false, | ||
'swagger' => [ | ||
'collectionFormat' => 'tsv', | ||
'minItems' => 2, | ||
], | ||
], | ||
'pipes-min-2' => [ | ||
'property' => 'pipes-min-2', | ||
'type' => 'array', | ||
'required' => false, | ||
'swagger' => [ | ||
'collectionFormat' => 'pipes', | ||
'minItems' => 2, | ||
], | ||
], | ||
'csv-uniques' => [ | ||
'property' => 'csv-uniques', | ||
'type' => 'array', | ||
'required' => false, | ||
'swagger' => [ | ||
'uniqueItems' => true, | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters