-
Notifications
You must be signed in to change notification settings - Fork 31
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
5 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/ZfrRest/Mvc/Controller/MethodHandler/DataValidationTrait.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,72 @@ | ||
<?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\MethodHandler; | ||
|
||
use Zend\InputFilter\InputFilterPluginManager; | ||
use ZfrRest\Mvc\Exception\RuntimeException; | ||
use ZfrRest\Options\ControllerBehavioursOptions; | ||
use ZfrRest\Resource\ResourceInterface; | ||
|
||
/** | ||
* This trait is responsible for validating data for any method handler | ||
* | ||
* @author Michaël Gallego <mic.gallego@gmail.com> | ||
* @licence MIT | ||
*/ | ||
trait DataValidationTrait | ||
{ | ||
/** | ||
* @var InputFilterPluginManager | ||
*/ | ||
protected $inputFilterPluginManager; | ||
|
||
/** | ||
* Filter and validate the data | ||
* | ||
* @param ResourceInterface $resource | ||
* @param array $data | ||
* @return array | ||
* @throws RuntimeException If no input filter metadata has been found | ||
*/ | ||
public function validateData(ResourceInterface $resource, array $data) | ||
{ | ||
if (!$this->getControllerBehavioursOptions()->getAutoValidate()) { | ||
return $data; | ||
} | ||
|
||
if (!($inputFilterName = $resource->getMetadata()->getInputFilterName())) { | ||
throw new RuntimeException('No input filter name has been found in resource metadata'); | ||
} | ||
|
||
/* @var \Zend\InputFilter\InputFilter $inputFilter */ | ||
$inputFilter = $this->inputFilterPluginManager->get($inputFilterName); | ||
$inputFilter->setData($data); | ||
|
||
if (!$inputFilter->isValid()) { | ||
|
||
} | ||
|
||
return $inputFilter->getValues(); | ||
} | ||
|
||
/** | ||
* @return ControllerBehavioursOptions | ||
*/ | ||
abstract public function getControllerBehavioursOptions(); | ||
} |
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,83 @@ | ||
<?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\Options; | ||
|
||
use Zend\Stdlib\AbstractOptions; | ||
|
||
/** | ||
* Options class to define which behaviours method handlers should follow | ||
* | ||
* @license MIT | ||
* @author Michaël Gallego <mic.gallego@gmail.com> | ||
*/ | ||
class ControllerBehavioursOptions extends AbstractOptions | ||
{ | ||
/** | ||
* If this is set to true, then controller will automatically instantiate the input filter specified in | ||
* resource metadata (if there is one) - from service locator first, or directly instantiate it if not found -, | ||
* and validate data. If data is incorrect, it will return a 400 HTTP error (Bad Request) with the failed | ||
* validation messages in it). | ||
* | ||
* @var bool | ||
*/ | ||
protected $autoValidate = true; | ||
|
||
/** | ||
* If this is set to true, then controller will automatically instantiate the hydrator specified in resource | ||
* metadata (if there is one) - from service locator first, or directly instantiate it if not found - and | ||
* hydrate resource object with previously validated data | ||
* | ||
* @var bool | ||
*/ | ||
protected $autoHydrate = true; | ||
|
||
/** | ||
* @param bool $autoValidate | ||
* @return void | ||
*/ | ||
public function setAutoValidate($autoValidate) | ||
{ | ||
$this->autoValidate = (bool) $autoValidate; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function getAutoValidate() | ||
{ | ||
return $this->autoValidate; | ||
} | ||
|
||
/** | ||
* @param bool $autoHydrate | ||
* @return void | ||
*/ | ||
public function setAutoHydrate($autoHydrate) | ||
{ | ||
$this->autoHydrate = (bool) $autoHydrate; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function getAutoHydrate() | ||
{ | ||
return $this->autoHydrate; | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
tests/ZfrRestTest/Options/ControllerBehavioursOptionsTest.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,51 @@ | ||
<?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\Options; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
use ZfrRest\Options\ControllerBehavioursOptions; | ||
|
||
/** | ||
* @licence MIT | ||
* @author Michaël Gallego <mic.gallego@gmail.com> | ||
* | ||
* @group Coverage | ||
* @covers \ZfrRest\Options\ControllerBehavioursOptions | ||
*/ | ||
class ControllerBehavioursOptionsTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testAssertDefaultValue() | ||
{ | ||
$options = new ControllerBehavioursOptions(); | ||
|
||
$this->assertTrue($options->getAutoHydrate()); | ||
$this->assertTrue($options->getAutoValidate()); | ||
} | ||
|
||
public function testSettersAndGetters() | ||
{ | ||
$options = new ControllerBehavioursOptions([ | ||
'auto_hydrate' => false, | ||
'auto_validate' => false | ||
]); | ||
|
||
$this->assertFalse($options->getAutoHydrate()); | ||
$this->assertFalse($options->getAutoValidate()); | ||
} | ||
} |
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