Skip to content
Craig Manley edited this page Feb 17, 2016 · 6 revisions

Validate\Validation

The Validation class encapsulates checks for validating single non-null values. This class may be used stand-alone, but it is typically used as a parameter for the Spec constructor.

  • Class name: Validation
  • Namespace: Validate

Methods

__construct

__construct(array $args)

The constructor takes an optional associative array of checks and/or options. Passing nothing or an empty array will cause the object to treat everything it validates as valid.

Supported check arguments:

Key Description
type allowed type as returned by gettype(), including 'scalar', 'int' (alias of 'integer'), 'float' (alias of 'double')
types array of allowed types (see type)
resource_type only used if 'resource' is in 'types' array
max_length max string length, for scalar types
min_length min string length, for scalar types
mb_max_length max multibyte string length, for scalar types
mb_min_length min multibyte string length, for scalar types
max_value for numeric types
min_value for numeric types
isa allowed object type
regex validation regex string, e.g. '/^.{1,50}$/s'
callback boolean closure function that receives the value as argument
callbacks associative array of boolean closure functions that receive the value as argument
allowed_values array of scalars;
if the test value is a scalar, then it must match one of the values in 'allowed_values';
if the test value is an array, then all of it's values must match any of the values in 'allowed_values'

Supported options:

Key Description
nocase (boolean, makes allowed_values check case insensitive)

All arguments passed to the constructor, can be accessed using read-only property accessors, e.g.: print $validation->regex . "\n";

validate

boolean validate(mixed $arg)

Validates the given argument and returns the boolean result. Null arguments are ignored (with true as result) since checking if a value is null or not is the task of the Spec class.

validate_ex

void validate_ex(mixed $arg)

This convenience method is meant for stand-alone use. It simply wraps the validate() method and throws a ValidationCheckException on failure.

getLastFailure

string getLastFailure()

Return the name of the check the last validation failed on. If the last validation was successful, then this method will return null. Examples of results:

  • 'max_length'
  • 'regex'
  • 'callback'
  • 'email (callback)' in case one passed a 'callbacks' argument with key 'email' to the constructor