dev-validate-event-hooks
dev-validate-event-hooksObject validaiton library
BSD-3-Clause
by Ivan Kerin
Object validaiton library
Harp Validate is a validation library. It generates errors for objects based on a predefined assertions., (*2)
Quick Example:, (*3)
use Harp\Validate\Assert; use Harp\Validate\Asserts; $asserts = new Asserts(array( new Assert\Present('title'), new Assert\LengthBetween('title', 20, 100), new Assert\Email('newsletter_email'), )); $subject = new stdClass(); $subject->title = 'small title'; $subject->newsletter_email = 'invalid email'; // title should be between 10 and 20 letters, newsletter_email should be a valid email echo $asserts->getErrors($subject);
The result of $asserts->getErrors($subject)
is actually an Errors
object. It's an Iterator that holds all the erorrs and has ->humanize()
mehtod to display all the errors.
You can also foreach it and get all the errors separately. Casting it to string calls ->humanize()
automatically., (*4)
$errors = $asserts->getErrors($subject); foreach($errors => $error) { echo $error->getName(); echo $error->getMessage(); }
You can also traverse through the Errors object using getFirst
and getNext
methods., (*5)
$errors = $asserts->validate($subject); echo $errors->getFirst(); echo $errors->getNext();
You can also add a special trait to an object to make it "validatable"., (*6)
use Harp\Validate\ValidateTrait; use Harp\Validate\Asserts; use Harp\Validate\Assert\Present; class Model { use ValidateTrait; public $test; public function getValidationAsserts() { return new Asserts(array( new Present('test'), )); } }
It will addd these methods to your class:, (*7)
Method | Description |
---|---|
validate() | Perform the assertions, specified in the getValidationAsserts(). Will return true or false, and will set the errors object |
getErrors() | Returns an Errors object. If validate has not been called yet, will return an empty Errors object |
isEmptyErrors() | Return true or false |
assertValid() | Throw Harp\Validate\InvalidException if there are any errors |
Callback, (*8)
Assert that the result of a given callback is true. You must use a closure object, and will recieve the subject and the value as arguments., (*9)
new Callback('state', function ($subject, $value) { return $value !== 'test'; })
Email, (*10)
Assert if the value is not a proper email address. uses a small and fast regex which should handle most cases., (*11)
new Email('email_address') new Email('email_address', 'some custom message')
EmailStrict, (*12)
Assert if the value is not a proper email address. Uses a slower but more comprehensive check thane Email
., (*13)
new EmailStrict('email_address') new EmailStrict('email_address', 'some custom message')
GreaterThan, (*14)
Assert that the value is greater than a set length. Value can be int or float or even numeric string, (*15)
new GreaterThan('price', 20) new GreaterThan('price', 20, 'some custom message')
InArray, (*16)
Assert if the value is present in an array, uses a simple in_array
call. Will throw InvalidArgumentException if the array is empty., (*17)
new InArray('state', array('big', 'small')) new InArray('state', array('big', 'small'), 'some custom message')
IP, (*18)
Assert that the value's is a valid IP address uses filter_var()
internally, (*19)
new IP('last_login_ip') new IP('last_login_ip', 'some custom message')
IsInstanceOf, (*20)
Assert if the value is an object of a given class is_a
call. Will throw InvalidArgumentException if the class does not exist., (*21)
new IsInstanceOf('state', 'My\Example\Item') new IsInstanceOf('state', 'My\Example\Item', 'some custom message')
LengthBetween, (*22)
Assert that the value's string length is between two set lengths (including). Uses mb_strlen()
internally., (*23)
new LengthLessThan('name', 10, 200) new LengthLessThan('name', 10, 200, 'some custom message')
LengthEquals, (*24)
Assert that the value is of exact string length. Uses mb_strlen()
internally., (*25)
new LengthEquals('name', 20), new LengthEquals('name', 20, 'some custom message')
LengthGreaterThan, (*26)
Assert that the value's string length is longer than a set length. Uses mb_strlen()
internally., (*27)
new LengthGreaterThan('name', 20) new LengthGreaterThan('name', 20, 'some custom message')
LengthLessThan, (*28)
Assert that the value's string length is shorter than a set length. Uses mb_strlen()
internally., (*29)
new LengthLessThan('name', 20) new LengthLessThan('name', 20, 'some custom message')
LessThan, (*30)
Assert that the value is less than a set length. Value can be int or float or even numeric string, (*31)
new LessThan('price', 20) new LessThan('price', 20, 'some custom message')
Matches, (*32)
Assert that a value of one property matches to the value of another, (*33)
new Matches('password', 'password_confirmation') new Matches('password', 'password_confirmation', 'some custom message')
IsInteger, (*34)
Assert that the value is a integer number., (*35)
new IsInteger('quantity') new IsInteger('quantity', 'some custom message')
IsFloat, (*36)
Assert that the value is a float number., (*37)
new IsFloat('frequency') new IsFloat('frequency', 'some custom message')
Present, (*38)
Assert if the value is empty, (*39)
new Present('title') new Present('title', 'some custom message if needed')
RegEx, (*40)
Assert that the value matches a given regex. Passed directly to preg_match()
, (*41)
new RegEx('card_number', '/\d{20}/') new RegEx('card_number', '/\d{20}/', 'some custom message')
URL, (*42)
Assert if the value is a valid url. Converts all UTF related charecters in the url to their proper encoding. It also will convert non-ASCII domain names, using "idn" if the "intl" extension is available. This is similar to what browsers normally do., (*43)
new URL('website') new URL('website', 'some custom message')
URLStrict, (*44)
Assert if the value is a valid url. Uses php's filter_var()
method., (*45)
new URLStrict('website') new URLStrict('website', 'some custom message')
This trait gives you the ability to easily add assertions to another object., (*46)
class TestConfig { use AssertsTrait; } $config = new TestConfig(); $config ->assertPresent('name') ->assertURL('homepage', 'must have a valid homepage'); // Return the Asserts object $config->getAsserts();
Here are all the methods added by this trait., (*47)
Method | Description |
---|---|
getAsserts() | Get the Asserts object |
addAssert(AbstractAssertion) | Add arbitrary asserts |
assertCallback($name, $message) | Add an Assert\Callback object |
assertEmail($name, $message) | Add an Assert\Email object |
assertEmailStrict($name, $message) | Add an Assert\EmailStrict object |
assertGreaterThan($name, $value, $message) | Add an Assert\GreaterThan object |
assertInArray($name, $array, $message) | Add an Assert\InArray object |
assertIP($name, $message) | Add an Assert\IP object |
assertIsInteger($name, $message) | Add an Assert\IsInteger object |
assertIsInstanceOf($name, $class, $message) | Add an Assert\IsInstanceOf object |
assertIsFloat($name, $message) | Add an Assert\IsFloat object |
assertLengthBetween($name, $min, $max, $message) | Add an Assert\LengthBetween object |
assertLengthEquals($name, $length, $message) | Add an Assert\LengthEquals object |
assertLengthGreaterThan($name, $length, $message) | Add an Assert\LengthGreaterThan object |
assertLengthLessThan($name, $length, $message) | Add an Assert\LengthLessThan object |
assertLessThan($name, $value, $message) | Add an Assert\LessThan object |
assertMatches($name, $property, $message) | Add an Assert\Matches object |
assertPresent($name, $message) | Add an Assert\Present object |
assertRegEx($name, $pattern, $message) | Add an Assert\RegEx object |
assertURL($name, $message) | Add an Assert\URL object |
assertURLStrict($name, $message) | Add an Assert\URLStrict object |
Copyright (c) 2014, Clippings Ltd. Developed by Ivan Kerin as part of clippings.com, (*48)
Under BSD-3-Clause license, read LICENSE file., (*49)
Object validaiton library
BSD-3-Clause