Skip to content

Commit

Permalink
Merge branch 'PARAMETERS'
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhemN committed Oct 20, 2015
2 parents 942af40 + 88262a5 commit 91ce359
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ abstract class ApiController extends Controller
*
* @return string
*/
protected function serializeView($value, $context, $format = null)
protected function serializeView($value, $context = null, $format = null)
{
if ($format === null) {
$format = $this->getParameter('exsyst_api.serializer.default_format');
}

$serializer = $this->get('exsyst_api.serializer');

return $serializer->serialize($value, $format, $context);
if ($context === null) {
return $serializer->serialize($value, $format);
} else {
return $serializer->serialize($value, $format, $context);
}
}

/**
Expand All @@ -44,7 +48,7 @@ protected function serializeView($value, $context, $format = null)
*
* @return Response
*/
protected function serialize($value, $context, $format = null, Response $response = null)
protected function serialize($value, $context = null, $format = null, Response $response = null)
{
$request = $this->get('request_stack')->getCurrentRequest();
if ($format === null) {
Expand Down
11 changes: 11 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('parameter')
->addDefaultsIfNotSet()
->children()
->arrayNode('validation')
->canBeEnabled()
->children()
->scalarNode('attributeName')->defaultValue('validationErrors')->end()
->end()
->end()
->end()
->end()
->end();

return $treeBuilder;
Expand Down
10 changes: 10 additions & 0 deletions DependencyInjection/EXSystApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->loadSerialization($config, $loader, $container);
$this->loadRouting($config, $loader, $container);
$this->loadVersioning($config, $loader, $container);
$this->loadParameterValidation($config, $loader, $container);
}

private function loadSerialization(array $config, YamlFileLoader $loader, ContainerBuilder $container)
Expand Down Expand Up @@ -95,6 +96,15 @@ private function loadVersioning(array $config, YamlFileLoader $loader, Container
}
}

private function loadParameterValidation(array $config, YamlFileLoader $loader, ContainerBuilder $container)
{
if ($config['parameter']['validation']['enabled']) {
$loader->load('parameter_validation.yml');

$container->getDefinition('exsyst_api.parameter.validation_listener')->replaceArgument(2, $config['parameter']['validation']['attributeName']);
}
}

public function getAlias()
{
return 'exsyst_api';
Expand Down
65 changes: 65 additions & 0 deletions EventListener/ParameterValidationListener.php
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);
}
}
14 changes: 14 additions & 0 deletions Resources/config/parameter_validation.yml
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 }
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ version_uri:
version_other:
path: /version
defaults: { _controller: TestBundle:Version:version }

parameter_validation:
path: /parameter-validation
defaults: { _controller: TestBundle:ParameterValidation:validate }
47 changes: 47 additions & 0 deletions Tests/Functional/ParameterValidationTest.php
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()
);
}
}
16 changes: 16 additions & 0 deletions Tests/Functional/app/ParameterValidation/bundles.php
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(),
];
12 changes: 12 additions & 0 deletions Tests/Functional/app/ParameterValidation/config.yml
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

0 comments on commit 91ce359

Please sign in to comment.