Skip to content
Patrick Kuijvenhoven edited this page May 26, 2018 · 4 revisions

Validator

Validator objects use their internal Specs to validate and possibly modify (associative) arrays. Typical arrays that often require validation are those from form submissions, reading CSV records, and function array-type arguments.

SYNOPSIS

# Using named parameters:
$validator = new Validate\Validator(array(
	'remove_extra' => true,
	'specs' => array(
		'name'	=> array(
			'type'		=> 'string',
			'max_length'	=> 2,
			'max_length'	=> 30,
		),
		'birthdate' => array(
			'type'	=> 'string',
			'regex'	=> '#^[0-3]\d/[01]\d/\d{4}$#', // expect dd/mm/yyyy
			'after'	=> function(&$value) { // want yyyy-mm-dd
				if (is_string($value) && preg_match('#^(\d{2})/(\d{2})/(\d{4})$#', $value, $matches)) {
					$value = $matches[3] . '-' . $matches[2] . '-' . $matches[1];
				}
			},
			'optional' => true,
		),
		'score' => array(
			'types' => array('float', 'integer'),
			'max_value'	=> 10,
			'min_value'	=> 0,
		),
	),
));

$params = array(
	'name'		=> 'Jane',
	'birthdate'	=> '31/01/1984',
	'score'		=> 7,
	'height'	=> 160,
),
$params = $validator->validate($params);


# Using positional parameters:
function my_str_replace() {
	$args = (new Validate\Validator(array(
		'specs' => array(
			array(
				'type' => 'scalar',
			),
			array(
				'type' => 'scalar',
			),
			array(
 				'type' => 'scalar',
			),
		),
	)))->validate_pos(func_get_args());
	$search		= $args[0];
	$replace	= $args[1];
	$subject	= $args[2];
	return str_replace($search, $replace, $subject);
}
print my_str_replace('him', 'her', $string);
  • Class name: Validator
  • Namespace: Validate

Methods

__construct

__construct(array $args)

Constructor.

The following options are supported:

Key Description
allow_extra allow extra parameters for which there are no specs.
empty_delete delete empty key value pairs; default true
empty_null null values of empty key value pairs
prefix for nested validators: set this to whatever you want to prefix parameter names with in exception messages.
remove_extra remove extra parameters for which there are no specs.
specs if not given, then no validation will take place

Arguments

  • $args array

__get

mixed __get($key)

This is a PHP magic method that provides public read-only property accessors to the options passed into the constructor, e.g. print $spec->optional . "\n";

Arguments

  • $key mixed

specs

array|null specs()

Returns the Specs object passed to the constructor.

validate

array validate(array $args)

Uses the specs passed into the constructor to validate the given associative array and returns the possibly modified array.

Throws a ValidationNamedCheckException on validation errors for $arg keys that do have a matching key in the specs. Throws a ValidationException on validation errors for $arg keys that have no key the specs.

Arguments

  • $args array - associative array

validate_pos

array validate_pos(array $args)

Validates a plain positional array of arguments and returns the possibly modified array.

Since all PHP arrays are really associative, this function simply matches the values of the $args with the values of the specs. It throws the same exceptions as validate().

Arguments

  • $args array