Skip to content

Commit

Permalink
Merge 3bfdfa6 into ce3346e
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Apr 2, 2014
2 parents ce3346e + 3bfdfa6 commit 72f9f47
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 9 deletions.
24 changes: 20 additions & 4 deletions src/ZfrRest/Mvc/Controller/MethodHandler/DataValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
namespace ZfrRest\Mvc\Controller\MethodHandler;

use Zend\InputFilter\InputFilterPluginManager;
use Zend\Mvc\Controller\AbstractController;
use ZfrRest\Http\Exception\Client\UnprocessableEntityException;
use ZfrRest\Mvc\Controller\ValidationGroupProviderInterface;
use ZfrRest\Mvc\Exception\RuntimeException;
use ZfrRest\Resource\ResourceInterface;

Expand All @@ -39,14 +41,20 @@ trait DataValidationTrait
/**
* Filter and validate the data
*
* @param ResourceInterface $resource
* @param array $data
* @param ResourceInterface $resource
* @param array $data
* @param AbstractController|null $controller
* @param string|null $validationGroupName
* @return array
* @throws RuntimeException If no input filter is bound to the resource
* @throws UnprocessableEntityException If validation fails
*/
public function validateData(ResourceInterface $resource, array $data)
{
public function validateData(
ResourceInterface $resource,
array $data,
AbstractController $controller = null,
$validationGroupName = null
) {
if (!($inputFilterName = $resource->getMetadata()->getInputFilterName())) {
throw new RuntimeException('No input filter name has been found in resource metadata');
}
Expand All @@ -55,6 +63,14 @@ public function validateData(ResourceInterface $resource, array $data)
$inputFilter = $this->inputFilterPluginManager->get($inputFilterName);
$inputFilter->setData($data);

if ($controller instanceof ValidationGroupProviderInterface && $validationGroupName) {
$validationGroups = $controller->getValidationGroupSpecification();

if (isset($validationGroups[$validationGroupName])) {
$inputFilter->setValidationGroup($validationGroups[$validationGroupName]);
}
}

if (!$inputFilter->isValid()) {
throw new UnprocessableEntityException(
'Validation error',
Expand Down
2 changes: 1 addition & 1 deletion src/ZfrRest/Mvc/Controller/MethodHandler/PostHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function handleMethod(AbstractRestfulController $controller, ResourceInte
$data = json_decode($controller->getRequest()->getContent(), true);

if ($controller->getAutoValidate()) {
$data = $this->validateData($singleResource, $data);
$data = $this->validateData($singleResource, $data, $controller, 'post');
}

if ($controller->getAutoHydrate()) {
Expand Down
2 changes: 1 addition & 1 deletion src/ZfrRest/Mvc/Controller/MethodHandler/PutHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function handleMethod(AbstractRestfulController $controller, ResourceInte
$data = json_decode($controller->getRequest()->getContent(), true);

if ($controller->getAutoValidate()) {
$data = $this->validateData($resource, $data);
$data = $this->validateData($resource, $data, $controller, 'put');
}

if ($controller->getAutoHydrate()) {
Expand Down
35 changes: 35 additions & 0 deletions src/ZfrRest/Mvc/Controller/ValidationGroupProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace ZfrRest\Mvc\Controller;

/**
* Interface that allows to provide validation group, indexed by HTTP verb
*
* @author Michaël Gallego <mic.gallego@gmail.com>
* @licence MIT
*/
interface ValidationGroupProviderInterface
{
/**
* Get the validation group specification
*
* @return array
*/
public function getValidationGroupSpecification();
}
36 changes: 36 additions & 0 deletions tests/ZfrRestTest/Asset/Mvc/ControllerWithValidationGroupSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace ZfrRestTest\Asset\Mvc;


use ZfrRest\Mvc\Controller\AbstractRestfulController;
use ZfrRest\Mvc\Controller\ValidationGroupProviderInterface;

class ControllerWithValidationGroupSpec extends AbstractRestfulController implements ValidationGroupProviderInterface
{
/**
* {@inheritDoc}
*/
public function getValidationGroupSpecification()
{
return [
'post' => ['field']
];
}
}
7 changes: 4 additions & 3 deletions tests/ZfrRestTest/Asset/Mvc/DataValidationObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ class DataValidationObject
{
use DataValidationTrait;

protected $controller;

/**
* @param InputFilterPluginManager $inputFilterPluginManager
* @param InputFilterPluginManager $inputFilterPluginManager
*/
public function __construct(InputFilterPluginManager $inputFilterPluginManager)
{
$this->inputFilterPluginManager = $inputFilterPluginManager;
$this->inputFilterPluginManager = $inputFilterPluginManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use PHPUnit_Framework_TestCase;
use ZfrRest\Options\ControllerBehavioursOptions;
use ZfrRestTest\Asset\Mvc\ControllerWithValidationGroupSpec;
use ZfrRestTest\Asset\Mvc\DataValidationObject;

/**
Expand Down Expand Up @@ -117,4 +118,40 @@ public function testThrowExceptionOnFailedValidation()

$this->dataValidation->validateData($resource, $data);
}

public function testCanUseValidationGroup()
{
$resource = $this->getMock('ZfrRest\Resource\ResourceInterface');
$metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface');

$resource->expects($this->once())->method('getMetadata')->will($this->returnValue($metadata));
$metadata->expects($this->once())->method('getInputFilterName')->will($this->returnValue('inputFilter'));

$data = ['foo'];

$inputFilter = $this->getMock('Zend\InputFilter\InputFilterInterface');
$inputFilter->expects($this->once())->method('setData')->with($data);
$inputFilter->expects($this->once())
->method('isValid')
->will($this->returnValue(true));

$inputFilter->expects($this->once())
->method('setValidationGroup')
->with(['field']);

$this->inputFilterPluginManager->expects($this->once())
->method('get')
->with('inputFilter')
->will($this->returnValue($inputFilter));

$inputFilter->expects($this->once())
->method('getValues')
->will($this->returnValue(['filtered']));

$controller = new ControllerWithValidationGroupSpec();

$result = $this->dataValidation->validateData($resource, $data, $controller, 'post');

$this->assertEquals(['filtered'], $result);
}
}

0 comments on commit 72f9f47

Please sign in to comment.