-
-
Notifications
You must be signed in to change notification settings - Fork 844
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
2,095 additions
and
157 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,55 @@ | ||
<?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\EventListener; | ||
|
||
use ApiPlatform\Core\Filter\QueryParameterValidator; | ||
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; | ||
use ApiPlatform\Core\Util\RequestAttributesExtractor; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
|
||
/** | ||
* Validates query parameters depending on filter description. | ||
* | ||
* @author Julien Deniau <julien.deniau@mapado.com> | ||
*/ | ||
final class QueryParameterValidateListener | ||
{ | ||
private $resourceMetadataFactory; | ||
|
||
private $queryParameterValidator; | ||
|
||
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, QueryParameterValidator $queryParameterValidator) | ||
{ | ||
$this->resourceMetadataFactory = $resourceMetadataFactory; | ||
$this->queryParameterValidator = $queryParameterValidator; | ||
} | ||
|
||
public function onKernelRequest(GetResponseEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
if ( | ||
!$request->isMethodSafe(false) | ||
|| !($attributes = RequestAttributesExtractor::extractAttributes($request)) | ||
|| !isset($attributes['collection_operation_name']) | ||
|| 'get' !== ($operationName = $attributes['collection_operation_name']) | ||
) { | ||
return; | ||
} | ||
|
||
$resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']); | ||
$resourceFilters = $resourceMetadata->getCollectionOperationAttribute($operationName, 'filters', [], true); | ||
|
||
$this->queryParameterValidator->validateFilters($attributes['resource_class'], $resourceFilters, $request); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,66 @@ | ||
<?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; | ||
|
||
use ApiPlatform\Core\Api\FilterLocatorTrait; | ||
use ApiPlatform\Core\Exception\FilterValidationException; | ||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Validates query parameters depending on filter description. | ||
* | ||
* @author Julien Deniau <julien.deniau@gmail.com> | ||
*/ | ||
class QueryParameterValidator | ||
{ | ||
use FilterLocatorTrait; | ||
|
||
private $validators; | ||
|
||
public function __construct(ContainerInterface $filterLocator) | ||
{ | ||
$this->setFilterLocator($filterLocator); | ||
|
||
$this->validators = [ | ||
new Validator\ArrayItems(), | ||
new Validator\Bounds(), | ||
new Validator\Enum(), | ||
new Validator\Length(), | ||
new Validator\MultipleOf(), | ||
new Validator\Pattern(), | ||
new Validator\Required(), | ||
]; | ||
} | ||
|
||
public function validateFilters(string $resourceClass, array $resourceFilters, Request $request) | ||
{ | ||
$errorList = []; | ||
foreach ($resourceFilters as $filterId) { | ||
if (!$filter = $this->getFilter($filterId)) { | ||
continue; | ||
} | ||
|
||
foreach ($filter->getDescription($resourceClass) as $name => $data) { | ||
foreach ($this->validators as $validator) { | ||
$errorList = \array_merge($errorList, $validator->validate($name, $data, $request)); | ||
} | ||
} | ||
} | ||
|
||
if ($errorList) { | ||
throw new FilterValidationException($errorList); | ||
} | ||
} | ||
} |
Oops, something went wrong.