mako\Validate


Description


Input/data validation.


Class methods


Toggle source

public __construct($input)


Class constructor.


Parameters

Type Description
array Array to validate
Return value

NULL

public function __construct(array $input)
{
	$this->input = $input;
}

Toggle source

public static factory($input)


Factory method making method chaining possible right off the bat.


Parameters

Type Description
array Array to validate
Return value

mako\Validate

public static function factory(array $input)
{
	return new static($input);
}

Toggle source

public rule($field, $function, $error)


Adds a validation rule to the list of callbacks.


Parameters

Type Description
mixed Field name
callback Function to use for validation
string Error message to return if validation fails
Return value

mako\Validate

public function rule($field, $function, $error)
{
	!is_array($function) && $function = array($function);

	if(is_string($function[0]) && method_exists(__CLASS__, $function[0]))
	{
		$function[0] = array(__CLASS__, $function[0]);
	}

	$callback['function'] = $function[0];
	$callback['params']   = isset($function[1]) ? $function[1] : array();
	$callback['error']    = $error;

	if($field === '*')
	{
		foreach(array_keys($this->input) as $field)
		{
			$this->rules[$field][] = $callback;
		}
	}
	else
	{
		$this->rules[$field][] = $callback;
	}

	return $this;
}

Toggle source

public process(&$errors = NULL)


Runs all validation rules. Returns TRUE if all rules passed and FALSE if validation failed.


Parameters

Type Description
array (optional) If $errors is provided, then it is filled with all the error messages
Return value

boolean

public function process(& $errors = null)
{
	foreach($this->rules as $field => $rules)
	{
		foreach($rules as $callback)
		{
			$params = array_merge(array($this->input[$field]), $callback['params']);

			if(call_user_func_array($callback['function'], $params) === false)
			{
				$this->errors[$field] = $callback['error'];

				break; // Jump to next field if an error is found
			}
		}
	}

	$errors = $this->errors;

	return empty($this->errors);
}

Toggle source

protected required($input)


Checks if field is empty or not


Parameters

Type Description
string The input string
Return value

boolean

protected function required($input)
{
	return ! empty($input);
}

Toggle source

protected minLength($input, $length)


Checks if input is long enough.


Parameters

Type Description
string The input string
int Required min length
Return value

boolean

protected function minLength($input, $length)
{
	return (mb_strlen($input) >= $length);
}

Toggle source

protected maxLength($input, $length)


Checks if input is short enough.


Parameters

Type Description
string The input string
int Required max length
Return value

boolean

protected function maxLength($input, $length)
{
	return (mb_strlen($input) <= $length);
}

Toggle source

protected exactLength($input, $length)


Checks if input is of the right length.


Parameters

Type Description
string The input string
int The required length
Return value

boolean

protected function exactLength($input, $length)
{
	return (mb_strlen($input) === $length);
}

Toggle source

protected match($input, $field)


Check if field matches another field.


Parameters

Type Description
string The input string
string Field name to match against
Return value

boolean

protected function match($input, $field)
{
	return ($input === $this->input[$field]);
}

Toggle source

protected regex($input, $pattern)


Check if a field matches a custom regex pattern.


Parameters

Type Description
string The input string
string Regex pattern to match against
Return value

boolean

protected function regex($input, $pattern)
{
	return (bool) preg_match($pattern, $input);
}

Toggle source

public static email($input)


Validates an email address using PHPs own email validation filter.


Parameters

Type Description
string Email address to validate
Return value

boolean

public static function email($input)
{
	return (bool) filter_var($input, FILTER_VALIDATE_EMAIL);
}

Toggle source

public static emailDomain($input)


Validates an email domain by looking for a MX reccord.


Parameters

Type Description
string Email address to validate
Return value

boolean

public static function emailDomain($input)
{
	if(empty($input))
	{
		return false;
	}

	$email = explode('@', $input);

	return checkdnsrr(array_pop($email), 'MX');
}

Toggle source

public ip($input)


Validates an IP using PHPs own IP validation filter.


Parameters

Type Description
string IP address to validate
Return value

boolean

public function ip($input)
{
	return (bool) filter_var($input, FILTER_VALIDATE_IP);
}

Toggle source

public url($input)


Validates an URL using PHPs own URL validation filter.


Parameters

Type Description
string URL to validate
Return value

boolean

public function url($input)
{
	return (bool) filter_var($input, FILTER_VALIDATE_URL);
}