vendor/api-platform/core/src/Api/QueryParameterValidator/QueryParameterValidator.php line 55

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Api\QueryParameterValidator;
  12. use ApiPlatform\Api\FilterLocatorTrait;
  13. use ApiPlatform\Exception\FilterValidationException;
  14. use Psr\Container\ContainerInterface;
  15. /**
  16.  * Validates query parameters depending on filter description.
  17.  *
  18.  * @author Julien Deniau <julien.deniau@gmail.com>
  19.  */
  20. class QueryParameterValidator
  21. {
  22.     use FilterLocatorTrait;
  23.     private $validators;
  24.     public function __construct(ContainerInterface $filterLocator)
  25.     {
  26.         $this->setFilterLocator($filterLocator);
  27.         $this->validators = [
  28.             new Validator\ArrayItems(),
  29.             new Validator\Bounds(),
  30.             new Validator\Enum(),
  31.             new Validator\Length(),
  32.             new Validator\MultipleOf(),
  33.             new Validator\Pattern(),
  34.             new Validator\Required(),
  35.         ];
  36.     }
  37.     public function validateFilters(string $resourceClass, array $resourceFilters, array $queryParameters): void
  38.     {
  39.         $errorList = [];
  40.         foreach ($resourceFilters as $filterId) {
  41.             if (!$filter $this->getFilter($filterId)) {
  42.                 continue;
  43.             }
  44.             foreach ($filter->getDescription($resourceClass) as $name => $data) {
  45.                 foreach ($this->validators as $validator) {
  46.                     $errorList array_merge($errorList$validator->validate($name$data$queryParameters));
  47.                 }
  48.             }
  49.         }
  50.         if ($errorList) {
  51.             throw new FilterValidationException($errorList);
  52.         }
  53.     }
  54. }
  55. class_alias(QueryParameterValidator::class, \ApiPlatform\Core\Filter\QueryParameterValidator::class);