renegare / aiv
api input validator (with support for Silex)
v0.0.2
2014-06-12 17:01 UTC
Requires
- php: >=5.4.0
- symfony/validator: >=2.4,<2.6-dev
Requires (Dev)
- phpunit/phpunit: 3.7.*
- satooshi/php-coveralls: dev-master
- silex/silex: >=1.0
- symfony/browser-kit: >=2.3,<2.6-dev
- symfony/http-foundation: >=2.4,<2.6-dev
This package is not auto-updated.
Last update: 2024-12-17 02:22:53 UTC
README
Requirements
- PHP 5.4
- composer (preferably latest)
Installation
$ composer require renegare/aiv:dev-master
Usage Examples:
Silex Usage
<?php
$app = new \Silex\Application();
$app->register(new \AIV\Integration\SilexProvider, [
'aiv.validators' => [
'test-name' => [
'options' => [
'allow.extra.params' => true,
'allow.missing.params' => true
],
'params' => [
'name' => [
'not.blank',
[
'type' => 'length',
'options' => ['min' => 2, 'max' => 20]]],
'email' => ['not.blank', '%email.validator%'],
'password' => ['not.blank']]]]]);
$app['email.validator'] = $app->share(function() {
return new \Symfony\Component\Validator\Constraints\Email;
});
$app->post('/', function(Application $app) {
$apiValidator = $app['validator'];
if($apiValidator->hasErrors('test-name')) {
$errors = [];
foreach($apiValidator->getErrors('test-name') as $violation) {
$path = preg_replace('/[\[\]]/', '', $violation->getPropertyPath());
$errors[$path] = $violation->getMessage();
}
return sprintf('You have errors: <pre>%s</pre>', print_r($errors, true));
} else {
return sprintf('You sent me valid data:<br /><pre>%s</pre>', print_r($apiValidator->getData('test-name'), true));
}
});
$app->run();
JSON Input
Typical JSON Rest API applications take input from the body in the form of json. There the default input handler will not work as it effectively looks in the $_POST array.
Simply add this code after registering the provider:
$app['aiv.input'] = $this->share(function(){
new \AIV\Input\SymfonyRequest\JSONInput;
});
Test
Check out the repo and from the top level directory run the following command (xdebug required for coverage):
$ composer update && vendor/bin/phpunit --coverage-text
Road Map
- "Modelesque" Classes that represent validation requirements ($instance_variables vs annotations)
Behind The Scene Lib
The actual validation of data is handled by: Symfony/Validator Component