dev-master
9999999-dev https://github.com/enygma/pv.gitpv : php validation
MIT
The Requires
- php >=5.3.1
validation variable type
Wallogit.com
2017 © Pedro Peláez
pv : php validation
The pv project is an experiment in trying to make strongly-typed objects for PHP. There are currently five types of objects in the set, some which mimic PHP's current variables:, (*1)
Each of these methods allow you to specify validation methods to check the contents of
the variable. These can be run by calling the validate method at any time. They can also
be added or removed as needed., (*2)
The variables also allow for conversion between the types (a more structured method than
PHP's loose typing). When the types are converted, a "to" method is executed when the
convert method is called., (*3)
validate()); echo "\n\n"; // false
### Array example
$str = array('test' => 'foo');
$s1 = new Pv\PArray($str, array('length[1]'));
echo 'RESULT: '; var_dump($s1->validate()); echo "\n\n"; // true
### Object example
$obj = new stdClass();
$obj->foo = 'bar';
$s2 = new Pv\PObject($obj, array('hasproperty[foo]'));
echo 'RESULT: '; var_dump($s2->validate()); echo "\n\n"; // true
### Boolean example
$s3 = new Pv\PBoolean(true);
## Adding more validation
$str = '12345678901';
$s = new Pv\PString($str,array('length[1,10]'));
$s->addValidation('numeric');
// since our string is numeric but is longer than 10 characters
echo 'RESULT: '; var_dump($s->validate()); echo "\n\n"; // false
?>
You can negate any check by adding a "not:" before it. For example:, (*5)
<?php
$str = '12345678901';
$s = new Pv\PString($str, array('not:length[1,10]'));
?>
The above returns true because the string of numbers is longer than 10 characters., (*6)
When you assign your validation, you can give them unique naming for reference later:, (*7)
'not:length[1,10]'
));
// or you can set it with addValidate
$s->addValidation('not:length[1,10]','validate1');
?>
You can also remove them with the same name:, (*8)
<?php
$str = '12345678901';
$s = new Pv\PString($str, array(
'validate1' => 'not:length[1,10]'
));
$s->removeValidation('validate1');
// you can also just use a numeric index here too
?>
If there's more than one validator on an object, you have the choice of executing all of the validators (default behavior), or you can execute a single validator:, (*9)
validate(0)); // fails var_export($s->validate(1)); // passes ?>
This also works with named validators as well:, (*10)
<?php
var_export($s->validate('validate1'));
?>
If you have validation that you want to execute but don't want to add it to the set, you
can execute a single validation with the single method:, (*11)
<?php
$str = new Pv\PString('testing');
$str->single('length[1,3]');
?>
In this case, the validation fails so a ValidationException will be immediately thrown., (*12)
Additionally, since the single call returns the current object so you can chain single
calls:, (*13)
<?php
$str = new Pv\PString('me@me.com');
$str->single('length[1,10]')->single('email');
?>
The above example would pass validation and not throw an exception., (*14)
You can also specify a closure to that returns either a true or false value to allow
for custom validation:, (*15)
addValidation(function($value) { return false; }) );
?>
When you create a new object, if the data given doesn't match the type for the object,
you'll be thrown a ValidationException., (*16)
When the validate() call fails, you'll be thrown an exception, a ValidationException.
You can catch this just like any other exception and find out what validation failed:, (*17)
validate();
} catch(\Exception $e) {
echo 'ERROR: '.$e->getMessage();
// Output: "Failure on validation Pv\Validate\Length"
}
?>
The objects allow you to convert between types easily. PHP does native type jugging, but the results can sometimes be unpredictable. Each object has methods to convert to the other types. Here's an example:, (*18)
convert('string');
var_export($ret); // outputs the string "true"
?>
String:, (*19)
length[min,max]: Check to be sure the string's length is between min/max, (*20)
int: Check to see if the string is an integer, (*21)
numeric: Check to see if the string is numeric, (*22)
url: See if the string validates as a URL, (*23)
range[min,max]: Checks to see if the value is in a given range, (*24)
contains[find]: "Find" is a substring in the value, (*25)
Array:, (*26)
length[min,max]: Check to be sure an array's length is between the min/max, (*27)
contains[find]: "Find" is a value in the array, (*28)
Object:, (*29)
hasproperty[name]: Check to see if object has a property, (*30)
instance[class name]: Check to see if object is an instance of the class name, (*31)
Boolean:, (*32)
Date:, (*33)
@author Chris Cornutt ccornutt@phpdeveloper.org, (*34)
@license MIT, (*35)
pv : php validation
MIT
validation variable type