Source for file Valid.class.php
Documentation is available at Valid.class.php
* Gumbo Library Framework
* This library is being released under the terms of the New BSD License. A
* copy of the license is packaged with the software (LICENSE.txt). If no
* copy is found, a copy of the license template can be found at:
* http://www.opensource.org/licenses/bsd-license.php
* @copyright Copyright (c) 2007, iBayou, Michael Luster
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @author Michael Luster <mluster79@yahoo.com>
* @link http://sourceforge.net/projects/phpgumbo
* The Validation class is responsible for performing basic Validation on
* data. The methods supplied are generic and commonly used. The class
* defines a default implementation for the Interface. It also provides an
* additional feature for turning Exceptions On, temporarily. This is
* performed by child Validation classes.
* All Validation Exceptions are not caught internally. Any class or code
* that is performing Validation should catch any Exceptions. To check if
* the parent is throwing Exceptions, simply implement a Validation method
* 1. Define a boolean variable as false: 'passed'
* 2. Perform any checks against the data, setting 'passed' to true on success
* 3. Check if parent is throwing Exceptions, throw Exception
* // perform check on data
* if (!$passed && $this->isThrowing ()) {
* throw new Gumbo_Exception ("Validation Failed: <some message>", $this->getLevel());
* A Validation class should extend this class. It already has the defined
* implementation for Validation features, and provides easy access to generic
* methods. The child Validation class would define the type of data it will
* validate. For example, Valid_Address will validate address information.
* The methods it defined narrow down parts of the Address.
* The On/Off feature comes in handy for child classes. This assists the class
* for ensuring that Exceptions are thrown based on the parent setting. To
* implement, simply turn on Exceptions, catch any failures, and turn off
* Exceptions. The next step determines if the primary Exception should be
* public function isName ($val) {
* $this->isString ($val);
* $this->isLessThanEqualTo ($val, 150);
* $this->isMatch ($val, "^([A-Za-z0-9[:space:]\.,_-]){0,}$");
* } catch (Exception $e) {
* if (!$passed && $this->isThrowing ()) {
* throw new Gumbo_Exception ("Validation Failed: Name", $this->getLevel ());
* Here's how it works. First, turn on Exceptions. This means that any Exceptions
* thrown will be caught inside the 'try...catch' block. The block will perform
* various Validation methods on the data, setting 'passed' to true if no Exceptions
* were thrown. Any caught Exception will be discarded. Turn off Exceptions, and
* check if the Validator object is throwing Exceptions.
* @copyright Copyright (c) 2007, iBayou, Michael Luster
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @author Michael Luster <mluster79@yahoo.com>
* @link http://sourceforge.net/projects/phpgumbo
gumbo_load ("Interface_Valid");
/** @var bool $_throw indicates if the class should throw Exceptions */
/** @var bool $_on if throwing Exceptions is temporarily on */
/** @var int $_level Error Level for any thrown Exceptions */
* @param bool $throw throw Exceptions
* @param int $level Error Level thrown
public function __construct ($throw=
false, $level=
null) {
* Turns throwing Exceptions ON (temporarily)
protected function turnOn () {
* Turns throwing Exception OFF
* Sets if the class should throw Exceptions
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Returns if Exceptions should be thrown from this class
* Returns if Exceptions was turned on (temporarily)
protected function isOn () {
* Returns the Error Level for Exceptions thrown by the class
* Returns if the value is one of the preconditions
* The method will require additional arguments, each with a different
* data type (as strings). The method will then check to see if the
* value is either of the types. For example, if a value can be a string
* Gumbo_Valid::precondition ($val, "str", "arr");
* The following are the strings to reference types:
* - 'num' => numeric, float, double
* - 'obj' => object (any type of object)
* @throws Gumbo_Exception
// if no arguments are given
if ($key ==
0) { continue; }
if (is_bool ($val)) { $passed =
true; }
if (is_int ($val)) { $passed =
true; }
if (is_array ($val)) { $passed =
true; }
if (is_null ($val)) { $passed =
true; }
* Returns if the given value is a primitive data type
* @precondition is a boolean, number, string, null
* @throws Gumbo_Exception
* Returns if the given value is an array
* @throws Gumbo_Exception
* Returns if the value is a boolean
* @throws Gumbo_Exception
public function isBool ($val) {
* Returns if the value is a Float
* @throws Gumbo_Exception
* Returns if the value is a Long
* @throws Gumbo_Exception
public function isLong ($val) {
* Returns if the value is Null
* @throws Gumbo_Exception
public function isNull ($val) {
* Returns if the value is Numeric
* @throws Gumbo_Exception
* Returns if the value is a Resource
* @throws Gumbo_Exception
* Returns if the value is an Object
* @param string $cls instance of class
* @throws Gumbo_Exception
public function isObject ($val, $cls=
null) {
$passed =
($val instanceof
$cls);
* Returns if the value is a String
* @throws Gumbo_Exception
* Returns if the value is an Integer
* @throws Gumbo_Exception
public function isInt ($val) {
* Returns if the value is a Match to the pattern
* @throws Gumbo_Exception
public function isMatch ($val, $pattern=
"[]") {
* Returns if the value is True
* This method will check if the value is identical to the 'true'
* value. The method will also check if the value is equal to
* the values of "yes" and "y". These values are common affirmative
* values used in many programs.
* @throws Gumbo_Exception
public function isTrue ($val) {
* Returns if the value is False
* This method will check if the value is identical to the 'false'
* value. The method will also check if the value is equal to
* the values of "no" and "n". These values are common negative
* values used in many programs.
* @throws Gumbo_Exception
* Returns if the value is Less Than the test case
* The value will check against the case value. The value can be
* three different value types:
* - case (numeric) : will compare values
* - case (int) : compare value string length to case value
* - case (string) : compare value string length to case string length
* - case (int) : compare value count of elements to case value
* - case (array) : compare value count of elements to case count of elements
* @throws Gumbo_Exception
if (is_numeric ($case)) { if ($val <
$case) { $passed =
true; } }
* Returns if the value is Less Than, or Equal to, the test case
* The value will check against the case value. The value can be
* three different value types:
* - case (numeric) : will compare values
* - case (numeric) : compare value string length to case value
* - case (string) : compare value string length to case string length
* - case (numeric) : compare value count of elements to case value
* - case (array) : compare value count of elements to case count of elements
* @throws Gumbo_Exception
if (is_numeric ($case)) { if ($val <=
$case) { $passed =
true; } }
* Returns if the value is Greater Than the test case
* The value will check against the case value. The value can be
* three different value types:
* - case (numeric) : will compare values
* - case (numeric) : compare value string length to case value
* - case (string) : compare value string length to case string length
* - case (numeric) : compare value count of elements to case value
* - case (array) : compare value count of elements to case count of elements
* @throws Gumbo_Exception
if (is_numeric ($case)) { if ($val >
$case) { $passed =
true; } }
* Returns if the value is Greater Than/Equal To the test case
* The value will check against the case value. The value can be
* three different value types:
* - case (numeric) : will compare values
* - case (numeric) : compare value string length to case value
* - case (string) : compare value string length to case string length
* - case (numeric) : compare value count of elements to case value
* - case (array) : compare value count of elements to case count of elements
* @throws Gumbo_Exception
if (is_numeric ($case)) { if ($val >=
$case) { $passed =
true; } }
* Returns if the value is Equal To the test case
* @throws Gumbo_Exception
$passed =
($val ==
$case);
* Returns if the value is Identical To the test case
* @throws Gumbo_Exception
$passed =
($val ===
$case);
* Returns if the value is Alpha characters only
* @throws Gumbo_Exception
* Returns if the value is AlphaNumeric characters only
* @throws Gumbo_Exception
* Returns if the value is Digits only
* @throws Gumbo_Exception
* Returns if the value is Hex characters only
* @throws Gumbo_Exception
public function isHex ($val) {
* Returns if the value is Lowercase
* @throws Gumbo_Exception
* Returns if the value is Uppercase
* @throws Gumbo_Exception
* Returns if the value is Printable
* @throws Gumbo_Exception
* Returns if the string value Begins With the test case
* @throws Gumbo_Exception
* Returns if the string value Ends With the test case
* @throws Gumbo_Exception
public function endsWith ($val, $case) {
* Returns if the string value Contains the test case
* @throws Gumbo_Exception
public function contains ($val, $case) {
$passed =
strstr ($val, $case);