-
Notifications
You must be signed in to change notification settings - Fork 0
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
10 changed files
with
218 additions
and
3 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
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,65 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ApiBundle package. | ||
* | ||
* (c) EXSyst | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace EXSyst\Bundle\ApiBundle\EventListener; | ||
|
||
use EXSyst\Component\Api\Parameter\ParameterReader; | ||
use EXSyst\Component\Api\Parameter\ParameterValidator; | ||
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; | ||
|
||
/** | ||
* @author Ener-Getick <[email protected]> | ||
*/ | ||
class ParameterValidationListener | ||
{ | ||
/** | ||
* @var ParameterReader | ||
*/ | ||
private $parameterReader; | ||
/** | ||
* @var ParameterValidator | ||
*/ | ||
private $parameterValidator; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ParameterReader $parameterReader | ||
* @param ParameterValidator $parameterValidator | ||
* @param string $attributeName | ||
*/ | ||
public function __construct(ParameterReader $parameterReader, ParameterValidator $parameterValidator, $attributeName) | ||
{ | ||
$this->parameterReader = $parameterReader; | ||
$this->parameterValidator = $parameterValidator; | ||
$this->attributeName = $attributeName; | ||
} | ||
|
||
/** | ||
* @param FilterControllerEvent $event | ||
*/ | ||
public function onKernelController(FilterControllerEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
|
||
$controller = $event->getController(); | ||
if (is_callable($controller) && method_exists($controller, '__invoke')) { | ||
$controller = [$controller, '__invoke']; | ||
} | ||
|
||
$parameters = $this->parameterReader->read( | ||
new \ReflectionMethod($controller[0], $controller[1]) | ||
); | ||
$errors = $this->parameterValidator->validateParameters($parameters); | ||
|
||
$request->attributes->set($this->attributeName, $errors); | ||
} | ||
} |
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,14 @@ | ||
services: | ||
exsyst_api.parameter.reader: | ||
class: EXSyst\Component\Api\Parameter\ParameterReader | ||
arguments: [ @annotation_reader ] | ||
|
||
exsyst_api.parameter.validator: | ||
class: EXSyst\Component\Api\Parameter\ParameterValidator | ||
arguments: [ @request_stack, @validator ] | ||
|
||
exsyst_api.parameter.validation_listener: | ||
class: EXSyst\Bundle\ApiBundle\EventListener\ParameterValidationListener | ||
arguments: [ @exsyst_api.parameter.reader, @exsyst_api.parameter.validator, ~ ] | ||
tags: | ||
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController, priority: 5 } |
32 changes: 32 additions & 0 deletions
32
Tests/Functional/Bundle/TestBundle/Controller/ParameterValidationController.php
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,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ApiBundle package. | ||
* | ||
* (c) EXSyst | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace EXSyst\Bundle\ApiBundle\Tests\Functional\Bundle\TestBundle\Controller; | ||
|
||
use EXSyst\Bundle\ApiBundle\Controller\ApiController; | ||
use EXSyst\Component\Api\Annotation; | ||
use Symfony\Component\Validator\Constraints; | ||
|
||
class ParameterValidationController extends ApiController | ||
{ | ||
/** | ||
* @Annotation\RequestParameter("foo", constraints={@Constraints\Email()}) | ||
* @Annotation\QueryParameter("bar", constraints={@Constraints\NotBlank()}, optional=false) | ||
*/ | ||
public function validateAction(array $apiErrors) | ||
{ | ||
$errors = array_map(function ($v) { | ||
return (string) $v; | ||
}, $apiErrors); | ||
|
||
return $this->serialize($errors); | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ApiBundle package. | ||
* | ||
* (c) EXSyst | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace EXSyst\Bundle\ApiBundle\Tests\Functional; | ||
|
||
/** | ||
* @author Ener-Getick <[email protected]> | ||
*/ | ||
class ParameterValidationTest extends WebTestCase | ||
{ | ||
public function setUp() | ||
{ | ||
$this->client = $this->createClient(['test_case' => 'ParameterValidation', 'root_config' => 'config.yml']); | ||
} | ||
|
||
public function testDefaultValidation() | ||
{ | ||
$this->client->request('GET', '/parameter-validation'); | ||
$response = $this->client->getResponse(); | ||
$this->assertEquals( | ||
'{"foo":"","bar":"bar:\n Parameter \"bar\" must be defined.\n"}', | ||
$response->getContent() | ||
); | ||
} | ||
|
||
public function testValidParameters() | ||
{ | ||
$this->client->request( | ||
'POST', | ||
'/parameter-validation?bar=null', | ||
['foo' => '[email protected]'] | ||
); | ||
$response = $this->client->getResponse(); | ||
$this->assertEquals( | ||
'{"foo":"","bar":""}', | ||
$response->getContent() | ||
); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ApiBundle package. | ||
* | ||
* (c) EXSyst | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
return [ | ||
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(), | ||
new \EXSyst\Bundle\ApiBundle\EXSystApiBundle(), | ||
new \EXSyst\Bundle\ApiBundle\Tests\Functional\Bundle\TestBundle\TestBundle(), | ||
]; |
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,12 @@ | ||
imports: | ||
- { resource: ../config/default.yml } | ||
|
||
framework: | ||
serializer: | ||
enabled: true | ||
|
||
exsyst_api: | ||
parameter: | ||
validation: | ||
enabled: true | ||
attributeName: apiErrors |