Input/data validation.
Class constructor.
| Type | Description |
|---|---|
| array | Array to validate |
NULL
public function __construct(array $input)
{
$this->input = $input;
}
Factory method making method chaining possible right off the bat.
| Type | Description |
|---|---|
| array | Array to validate |
mako\Validate
public static function factory(array $input)
{
return new static($input);
}
Adds a validation rule to the list of callbacks.
| Type | Description |
|---|---|
| mixed | Field name |
| callback | Function to use for validation |
| string | Error message to return if validation fails |
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;
}
Runs all validation rules. Returns TRUE if all rules passed and FALSE if validation failed.
| Type | Description |
|---|---|
| array | (optional) If $errors is provided, then it is filled with all the error messages |
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);
}
Checks if field is empty or not
| Type | Description |
|---|---|
| string | The input string |
boolean
protected function required($input)
{
return ! empty($input);
}
Checks if input is long enough.
| Type | Description |
|---|---|
| string | The input string |
| int | Required min length |
boolean
protected function minLength($input, $length)
{
return (mb_strlen($input) >= $length);
}
Checks if input is short enough.
| Type | Description |
|---|---|
| string | The input string |
| int | Required max length |
boolean
protected function maxLength($input, $length)
{
return (mb_strlen($input) <= $length);
}
Checks if input is of the right length.
| Type | Description |
|---|---|
| string | The input string |
| int | The required length |
boolean
protected function exactLength($input, $length)
{
return (mb_strlen($input) === $length);
}
Check if field matches another field.
| Type | Description |
|---|---|
| string | The input string |
| string | Field name to match against |
boolean
protected function match($input, $field)
{
return ($input === $this->input[$field]);
}
Check if a field matches a custom regex pattern.
| Type | Description |
|---|---|
| string | The input string |
| string | Regex pattern to match against |
boolean
protected function regex($input, $pattern)
{
return (bool) preg_match($pattern, $input);
}
Validates an email address using PHPs own email validation filter.
| Type | Description |
|---|---|
| string | Email address to validate |
boolean
public static function email($input)
{
return (bool) filter_var($input, FILTER_VALIDATE_EMAIL);
}
Validates an email domain by looking for a MX reccord.
| Type | Description |
|---|---|
| string | Email address to validate |
boolean
public static function emailDomain($input)
{
if(empty($input))
{
return false;
}
$email = explode('@', $input);
return checkdnsrr(array_pop($email), 'MX');
}
Validates an IP using PHPs own IP validation filter.
| Type | Description |
|---|---|
| string | IP address to validate |
boolean
public function ip($input)
{
return (bool) filter_var($input, FILTER_VALIDATE_IP);
}
Validates an URL using PHPs own URL validation filter.
| Type | Description |
|---|---|
| string | URL to validate |
boolean
public function url($input)
{
return (bool) filter_var($input, FILTER_VALIDATE_URL);
}