Validate
› Kohana_Validate
› ArrayObject
Class Contents
Constants
Properties
- None
Methods
- __construct()
- alpha()
- alpha_dash()
- alpha_numeric()
- as_array()
- callback()
- callbacks()
- check()
- color()
- copy()
- credit_card()
- date()
- decimal()
- digit()
- email()
- email_domain()
- equals()
- error()
- errors()
- exact_length()
- factory()
- filter()
- filters()
- ip()
- label()
- labels()
- luhn()
- matches()
- max_length()
- min_length()
- not_empty()
- numeric()
- phone()
- range()
- regex()
- rule()
- rules()
- url()
- __construct()
- append()
- append()
- asort()
- asort()
- count()
- count()
- exchangeArray()
- exchangeArray()
- getArrayCopy()
- getArrayCopy()
- getFlags()
- getFlags()
- getIterator()
- getIterator()
- getIteratorClass()
- getIteratorClass()
- ksort()
- ksort()
- natcasesort()
- natcasesort()
- natsort()
- natsort()
- offsetExists()
- offsetExists()
- offsetGet()
- offsetGet()
- offsetSet()
- offsetSet()
- offsetUnset()
- offsetUnset()
- setFlags()
- setFlags()
- setIteratorClass()
- setIteratorClass()
- uasort()
- uasort()
- uksort()
- uksort()
Class declared in SYSPATH/classes/validate.php on line 3.
Constants
- STD_PROP_LIST
integer 1
- ARRAY_AS_PROPS
integer 2
Methods
public __construct( )
› Kohana_Validate
Source Code
public function __construct(array $array)
{
parent::__construct($array, ArrayObject::STD_PROP_LIST);
}
public static alpha( )
› Kohana_Validate
Source Code
public static function alpha($str, $utf8 = FALSE)
{
$str = (string) $str;
if ($utf8 === TRUE)
{
return (bool) preg_match('/^\pL++$/uD', $str);
}
else
{
return ctype_alpha($str);
}
}
public static alpha_dash( )
› Kohana_Validate
Source Code
public static function alpha_dash($str, $utf8 = FALSE)
{
if ($utf8 === TRUE)
{
$regex = '/^[-\pL\pN_]++$/uD';
}
else
{
$regex = '/^[-a-z0-9_]++$/iD';
}
return (bool) preg_match($regex, $str);
}
public static alpha_numeric( )
› Kohana_Validate
Source Code
public static function alpha_numeric($str, $utf8 = FALSE)
{
if ($utf8 === TRUE)
{
return (bool) preg_match('/^[\pL\pN]++$/uD', $str);
}
else
{
return ctype_alnum($str);
}
}
public as_array( )
› Kohana_Validate
Source Code
public function as_array()
{
return $this->getArrayCopy();
}
public callback( )
› Kohana_Validate
Source Code
public function callback($field, $callback, array $params = array())
{
if ( ! isset($this->_callbacks[$field]))
{
// Create the list for this field
$this->_callbacks[$field] = array();
}
if ($field !== TRUE AND ! isset($this->_labels[$field]))
{
// Set the field label to the field name
$this->_labels[$field] = preg_replace('/[^\pL]+/u', ' ', $field);
}
if ( ! in_array($callback, $this->_callbacks[$field], TRUE))
{
// Store the callback
$this->_callbacks[$field][] = array($callback, $params);
}
return $this;
}
public callbacks( )
› Kohana_Validate
Source Code
public function callbacks($field, array $callbacks)
{
foreach ($callbacks as $callback)
{
$this->callback($field, $callback);
}
return $this;
}
public check( )
› Kohana_Validate
Source Code
public function check($allow_empty = FALSE)
{
if (Kohana::$profiling === TRUE)
{
// Start a new benchmark
$benchmark = Profiler::start('Validation', __FUNCTION__);
}
// New data set
$data = $this->_errors = array();
// Assume nothing has been submitted
$submitted = FALSE;
// Get a list of the expected fields
$expected = array_keys($this->_labels);
// Import the filters, rules, and callbacks locally
$filters = $this->_filters;
$rules = $this->_rules;
$callbacks = $this->_callbacks;
foreach ($expected as $field)
{
if (isset($this[$field]))
{
// Some data has been submitted, continue validation
$submitted = TRUE;
// Use the submitted value
$data[$field] = $this[$field];
}
else
{
// No data exists for this field
$data[$field] = NULL;
}
if (isset($filters[TRUE]))
{
if ( ! isset($filters[$field]))
{
// Initialize the filters for this field
$filters[$field] = array();
}
// Append the filters
$filters[$field] += $filters[TRUE];
}
if (isset($rules[TRUE]))
{
if ( ! isset($rules[$field]))
{
// Initialize the rules for this field
$rules[$field] = array();
}
// Append the rules
$rules[$field] += $rules[TRUE];
}
if (isset($callbacks[TRUE]))
{
if ( ! isset($callbacks[$field]))
{
// Initialize the callbacks for this field
$callbacks[$field] = array();
}
// Append the callbacks
$callbacks[$field] += $callbacks[TRUE];
}
}
// Overload the current array with the new one
$this->exchangeArray($data);
if ($submitted === FALSE AND ! $allow_empty)
{
// Because no data was submitted, validation will not be forced
return FALSE;
}
// Remove the filters, rules, and callbacks that apply to every field
unset($filters[TRUE], $rules[TRUE], $callbacks[TRUE]);
// Execute the filters
foreach ($filters as $field => $set)
{
// Get the field value
$value = $this[$field];
foreach ($set as $filter => $params)
{
// Add the field value to the parameters
array_unshift($params, $value);
if (strpos($filter, '::') === FALSE)
{
// Use a function call
$function = new ReflectionFunction($filter);
// Call $function($this[$field], $param, ...) with Reflection
$value = $function->invokeArgs($params);
}
else
{
// Split the class and method of the rule
list($class, $method) = explode('::', $filter, 2);
// Use a static method call
$method = new ReflectionMethod($class, $method);
// Call $Class::$method($this[$field], $param, ...) with Reflection
$value = $method->invokeArgs(NULL, $params);
}
}
// Set the filtered value
$this[$field] = $value;
}
// Execute the rules
foreach ($rules as $field => $set)
{
// Get the field value
$value = $this[$field];
foreach ($set as $rule => $params)
{
if ( ! in_array($rule, $this->_empty_rules) AND ! Validate::not_empty($value))
{
// Skip this rule for empty fields
continue;
}
// Add the field value to the parameters
array_unshift($params, $value);
if (method_exists($this, $rule))
{
// Use a method in this object
$method = new ReflectionMethod($this, $rule);
if ($method->isStatic())
{
// Call static::$rule($this[$field], $param, ...) with Reflection
$passed = $method->invokeArgs(NULL, $params);
}
else
{
// Do not use Reflection here, the method may be protected
$passed = call_user_func_array(array($this, $rule), $params);
}
}
elseif (strpos($rule, '::') === FALSE)
{
// Use a function call
$function = new ReflectionFunction($rule);
// Call $function($this[$field], $param, ...) with Reflection
$passed = $function->invokeArgs($params);
}
else
{
// Split the class and method of the rule
list($class, $method) = explode('::', $rule, 2);
// Use a static method call
$method = new ReflectionMethod($class, $method);
// Call $Class::$method($this[$field], $param, ...) with Reflection
$passed = $method->invokeArgs(NULL, $params);
}
if ($passed === FALSE)
{
// Remove the field value from the parameters
array_shift($params);
// Add the rule to the errors
$this->error($field, $rule, $params);
// This field has an error, stop executing rules
break;
}
}
}
// Execute the callbacks
foreach ($callbacks as $field => $set)
{
if (isset($this->_errors[$field]))
{
// Skip any field that already has an error
continue;
}
foreach ($set as $callback_array)
{
list($callback, $params) = $callback_array;
if (is_string($callback) AND strpos($callback, '::') !== FALSE)
{
// Make the static callback into an array
$callback = explode('::', $callback, 2);
}
if (is_array($callback))
{
// Separate the object and method
list ($object, $method) = $callback;
// Use a method in the given object
$method = new ReflectionMethod($object, $method);
if ( ! is_object($object))
{
// The object must be NULL for static calls
$object = NULL;
}
// Call $object->$method($this, $field, $errors) with Reflection
$method->invoke($object, $this, $field, $params);
}
else
{
// Use a function call
$function = new ReflectionFunction($callback);
// Call $function($this, $field, $errors) with Reflection
$function->invoke($this, $field, $params);
}
if (isset($this->_errors[$field]))
{
// An error was added, stop processing callbacks
break;
}
}
}
if (isset($benchmark))
{
// Stop benchmarking
Profiler::stop($benchmark);
}
return empty($this->_errors);
}
public static color( )
› Kohana_Validate
Source Code
public static function color($str)
{
return (bool) preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/iD', $str);
}
public copy( )
› Kohana_Validate
Source Code
public function copy(array $array)
{
// Create a copy of the current validation set
$copy = clone $this;
// Replace the data set
$copy->exchangeArray($array);
return $copy;
}
public static credit_card( )
› Kohana_Validate
Source Code
public static function credit_card($number, $type = NULL)
{
// Remove all non-digit characters from the number
if (($number = preg_replace('/\D+/', '', $number)) === '')
return FALSE;
if ($type == NULL)
{
// Use the default type
$type = 'default';
}
elseif (is_array($type))
{
foreach ($type as $t)
{
// Test each type for validity
if (Validate::credit_card($number, $t))
return TRUE;
}
return FALSE;
}
$cards = Kohana::config('credit_cards');
// Check card type
$type = strtolower($type);
if ( ! isset($cards[$type]))
return FALSE;
// Check card number length
$length = strlen($number);
// Validate the card length by the card type
if ( ! in_array($length, preg_split('/\D+/', $cards[$type]['length'])))
return FALSE;
// Check card number prefix
if ( ! preg_match('/^'.$cards[$type]['prefix'].'/', $number))
return FALSE;
// No Luhn check required
if ($cards[$type]['luhn'] == FALSE)
return TRUE;
return Validate::luhn($number);
}
public static date( )
› Kohana_Validate
Source Code
public static function date($str)
{
return (strtotime($str) !== FALSE);
}
public static decimal( )
› Kohana_Validate
Source Code
public static function decimal($str, $places = 2, $digits = NULL)
{
if ($digits > 0)
{
// Specific number of digits
$digits = '{'. (int) $digits.'}';
}
else
{
// Any number of digits
$digits = '+';
}
// Get the decimal point for the current locale
list($decimal) = array_values(localeconv());
return (bool) preg_match('/^[0-9]'.$digits.preg_quote($decimal).'[0-9]{'. (int) $places.'}$/D', $str);
}
public static digit( )
› Kohana_Validate
Source Code
public static function digit($str, $utf8 = FALSE)
{
if ($utf8 === TRUE)
{
return (bool) preg_match('/^\pN++$/uD', $str);
}
else
{
return (is_int($str) AND $str >= 0) OR ctype_digit($str);
}
}
public static email( )
› Kohana_Validate
Source Code
public static function email($email, $strict = FALSE)
{
if ($strict === TRUE)
{
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$pair = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext|$pair)*\\x5d";
$quoted_string = "\\x22($qtext|$pair)*\\x22";
$sub_domain = "($atom|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$expression = "/^$local_part\\x40$domain$/D";
}
else
{
$expression = '/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD';
}
return (bool) preg_match($expression, (string) $email);
}
public static email_domain( )
› Kohana_Validate
Source Code
public static function email_domain($email)
{
// Check if the email domain has a valid MX record
return (bool) checkdnsrr(preg_replace('/^[^@]++@/', '', $email), 'MX');
}
public static equals( )
› Kohana_Validate
Source Code
public static function equals($value, $required)
{
return ($value === $required);
}
public error( )
› Kohana_Validate
Source Code
public function error($field, $error, array $params = NULL)
{
$this->_errors[$field] = array($error, $params);
return $this;
}
public errors( )
› Kohana_Validate
Source Code
public function errors($file = NULL, $translate = TRUE)
{
if ($file === NULL)
{
// Return the error list
return $this->_errors;
}
// Create a new message list
$messages = array();
foreach ($this->_errors as $field => $set)
{
list($error, $params) = $set;
// Get the label for this field
$label = $this->_labels[$field];
if ($translate)
{
if (is_string($translate))
{
// Translate the label using the specified language
$label = __($label, NULL, $translate);
}
else
{
// Translate the label
$label = __($label);
}
}
// Start the translation values list
$values = array(
':field' => $label,
':value' => $this[$field],
);
if (is_array($values[':value']))
{
// All values must be strings
$values[':value'] = implode(', ', Arr::flatten($values[':value']));
}
if ($params)
{
foreach ($params as $key => $value)
{
if (is_array($value))
{
// All values must be strings
$value = implode(', ', Arr::flatten($value));
}
// Check if a label for this parameter exists
if (isset($this->_labels[$value]))
{
// Use the label as the value, eg: related field name for "matches"
$value = $this->_labels[$value];
if ($translate)
{
if (is_string($translate))
{
// Translate the value using the specified language
$value = __($value, NULL, $translate);
}
else
{
// Translate the value
$value = __($value);
}
}
}
// Add each parameter as a numbered value, starting from 1
$values[':param'.($key + 1)] = $value;
}
}
if ($message = Kohana::message($file, "{$field}.{$error}"))
{
// Found a message for this field and error
}
elseif ($message = Kohana::message($file, "{$field}.default"))
{
// Found a default message for this field
}
elseif ($message = Kohana::message($file, $error))
{
// Found a default message for this error
}
elseif ($message = Kohana::message('validate', $error))
{
// Found a default message for this error
}
else
{
// No message exists, display the path expected
$message = "{$file}.{$field}.{$error}";
}
if ($translate)
{
if (is_string($translate))
{
// Translate the message using specified language
$message = __($message, $values, $translate);
}
else
{
// Translate the message using the default language
$message = __($message, $values);
}
}
else
{
// Do not translate, just replace the values
$message = strtr($message, $values);
}
// Set the message for this field
$messages[$field] = $message;
}
return $messages;
}
public static exact_length( )
› Kohana_Validate
Source Code
public static function exact_length($value, $length)
{
return UTF8::strlen($value) === $length;
}
public static factory( )
› Kohana_Validate
Source Code
public static function factory(array $array)
{
return new Validate($array);
}
public filter( )
› Kohana_Validate
Source Code
public function filter($field, $filter, array $params = NULL)
{
if ($field !== TRUE AND ! isset($this->_labels[$field]))
{
// Set the field label to the field name
$this->_labels[$field] = preg_replace('/[^\pL]+/u', ' ', $field);
}
// Store the filter and params for this rule
$this->_filters[$field][$filter] = (array) $params;
return $this;
}
public filters( )
› Kohana_Validate
Source Code
public function filters($field, array $filters)
{
foreach ($filters as $filter => $params)
{
$this->filter($field, $filter, $params);
}
return $this;
}
public static ip( )
› Kohana_Validate
Source Code
public static function ip($ip, $allow_private = TRUE)
{
// Do not allow reserved addresses
$flags = FILTER_FLAG_NO_RES_RANGE;
if ($allow_private === FALSE)
{
// Do not allow private or reserved addresses
$flags = $flags | FILTER_FLAG_NO_PRIV_RANGE;
}
return (bool) filter_var($ip, FILTER_VALIDATE_IP, $flags);
}
public label( )
› Kohana_Validate
Source Code
public function label($field, $label)
{
// Set the label for this field
$this->_labels[$field] = $label;
return $this;
}
public labels( )
› Kohana_Validate
Source Code
public function labels(array $labels)
{
$this->_labels = $labels + $this->_labels;
return $this;
}
public static luhn( )
› Kohana_Validate
Source Code
public static function luhn($number)
{
// Force the value to be a string as this method uses string functions.
// Converting to an integer may pass PHP_INT_MAX and result in an error!
$number = (string) $number;
if ( ! ctype_digit($number))
{
// Luhn can only be used on numbers!
return FALSE;
}
// Check number length
$length = strlen($number);
// Checksum of the card number
$checksum = 0;
for ($i = $length - 1; $i >= 0; $i -= 2)
{
// Add up every 2nd digit, starting from the right
$checksum += substr($number, $i, 1);
}
for ($i = $length - 2; $i >= 0; $i -= 2)
{
// Add up every 2nd digit doubled, starting from the right
$double = substr($number, $i, 1) * 2;
// Subtract 9 from the double where value is greater than 10
$checksum += ($double >= 10) ? ($double - 9) : $double;
}
// If the checksum is a multiple of 10, the number is valid
return ($checksum % 10 === 0);
}
protected matches( )
› Kohana_Validate
Source Code
protected function matches($value, $match)
{
return ($value === $this[$match]);
}
public static max_length( )
› Kohana_Validate
Source Code
public static function max_length($value, $length)
{
return UTF8::strlen($value) <= $length;
}
public static min_length( )
› Kohana_Validate
Source Code
public static function min_length($value, $length)
{
return UTF8::strlen($value) >= $length;
}
public static not_empty( )
› Kohana_Validate
Source Code
public static function not_empty($value)
{
if (is_object($value) AND $value instanceof ArrayObject)
{
// Get the array from the ArrayObject
$value = $value->getArrayCopy();
}
// Value cannot be NULL, FALSE, '', or an empty array
return ! in_array($value, array(NULL, FALSE, '', array()), TRUE);
}
public static numeric( )
› Kohana_Validate
Source Code
public static function numeric($str)
{
// Get the decimal point for the current locale
list($decimal) = array_values(localeconv());
// A lookahead is used to make sure the string contains at least one digit (before or after the decimal point)
return (bool) preg_match('/^-?+(?=.*[0-9])[0-9]*+'.preg_quote($decimal).'?+[0-9]*+$/D', (string) $str);
}
public static phone( )
› Kohana_Validate
Source Code
public static function phone($number, $lengths = NULL)
{
if ( ! is_array($lengths))
{
$lengths = array(7,10,11);
}
// Remove all non-digit characters from the number
$number = preg_replace('/\D+/', '', $number);
// Check if the number is within range
return in_array(strlen($number), $lengths);
}
public static range( )
› Kohana_Validate
Source Code
public static function range($number, $min, $max)
{
return ($number >= $min AND $number <= $max);
}
public static regex( )
› Kohana_Validate
Source Code
public static function regex($value, $expression)
{
return (bool) preg_match($expression, (string) $value);
}
public rule( )
› Kohana_Validate
Source Code
public function rule($field, $rule, array $params = NULL)
{
if ($field !== TRUE AND ! isset($this->_labels[$field]))
{
// Set the field label to the field name
$this->_labels[$field] = trim(preg_replace('/[^\pL]+/u', ' ', $field));
}
if ('matches' === $rule AND ! isset($this->_labels[$params[0]]))
{
$match_field = $params[0];
$this->_labels[$match_field] = trim(preg_replace('/[^\pL]+/u', ' ', $match_field));
}
// Store the rule and params for this rule
$this->_rules[$field][$rule] = (array) $params;
return $this;
}
public rules( )
› Kohana_Validate
Source Code
public function rules($field, array $rules)
{
foreach ($rules as $rule => $params)
{
$this->rule($field, $rule, $params);
}
return $this;
}
public static url( )
› Kohana_Validate
Source Code
public static function url($url)
{
// Based on http://www.apps.ietf.org/rfc/rfc1738.html#sec-5
if ( ! preg_match(
'~^
# scheme
[-a-z0-9+.]++://
# username:password (optional)
(?:
[-a-z0-9$_.+!*\'(),;?&=%]++ # username
(?::[-a-z0-9$_.+!*\'(),;?&=%]++)? # password (optional)
@
)?
(?:
# ip address
\d{1,3}+(?:\.\d{1,3}+){3}+
| # or
# hostname (captured)
(
(?!-)[-a-z0-9]{1,63}+(?<!-)
(?:\.(?!-)[-a-z0-9]{1,63}+(?<!-)){0,126}+
)
)
# port (optional)
(?::\d{1,5}+)?
# path (optional)
(?:/.*)?
$~iDx', $url, $matches))
return FALSE;
// We matched an IP address
if ( ! isset($matches[1]))
return TRUE;
// Check maximum length of the whole hostname
// http://en.wikipedia.org/wiki/Domain_name#cite_note-0
if (strlen($matches[1]) > 253)
return FALSE;
// An extra check for the top level domain
// It must start with a letter
$tld = ltrim(substr($matches[1], (int) strrpos($matches[1], '.')), '.');
return ctype_alpha($tld[0]);
}
public __construct( )
› Kohana_Validate
Source Code
public function __construct(array $array)
{
parent::__construct($array, ArrayObject::STD_PROP_LIST);
}