Gumbo_Valid



Validation Class

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 as follows.

  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
  4. Return 'passed'
 $passed = false;
 // perform check on data
 ...
 if (!$passed && $this->isThrowing ()) {
 	throw new Gumbo_Exception ("Validation Failed: ", $this->getLevel());
 }
 return $passed;

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 thrown.

 public function isName ($val) {
 	$passed = false;

 	$this->turnOn ();
 	try {
 		// verify precondition
 		$this->isString ($val);
 		$this->isLessThanEqualTo ($val, 150);
 		$this->isMatch ($val, "^([A-Za-z0-9[:space:]\.,_-]){0,}$");
 		$passed = true;
 	} catch (Exception $e) {
 		unset ($e);
 	}
 	$this->turnOff ();

 	if (!$passed && $this->isThrowing ()) {
 		throw new Gumbo_Exception ("Validation Failed: Name", $this->getLevel ());
 	}
 	return $passed;
 }

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.

Author(s): Michael Luster <mluster79@yahoo.com>
License:New BSD License
Copyright:Copyright (c) 2007, iBayou, Michael Luster
Link:http://sourceforge.net/projects/phpgumbo
Version:0.0.1

Implements interfaces:

Child classes:

Class Variables

int $_level =  ERROR_DEBUG [line 108]
bool $_on =  false [line 106]
bool $_throw =  false [line 104]

Class Methods

public Gumbo_Valid __construct ( [bool $throw], [int $level] ) [line 117]

Constructor

Parameter(s):

  • (bool) $throw :: throw Exceptions
  • (int) $level :: Error Level thrown
Overridden in child classes as:
Gumbo_Valid_Address::__construct()
Constructor
Gumbo_Valid_CreditCard::__construct()
Constructor
Gumbo_Valid_Phone::__construct()
Constructor
Gumbo_Valid_User::__construct()
Constructor
Gumbo_Valid_Web::__construct()
Constructor


[ Top ]
public bool beginsWith ( string $val, string $case ) [line 936]

Returns if the string value Begins With the test case

Parameter(s):

  • (string) $val
  • (string) $case
  • throws:  Gumbo_Exception

[ Top ]
public bool contains ( string $val, string $case ) [line 986]

Returns if the string value Contains the test case

Parameter(s):

  • (string) $val
  • (string) $case
  • throws:  Gumbo_Exception

[ Top ]
public bool endsWith ( string $val, string $case ) [line 961]

Returns if the string value Ends With the test case

Parameter(s):

  • (string) $val
  • (string) $case
  • throws:  Gumbo_Exception

[ Top ]
public int getLevel ( ) [line 219]

Returns the Error Level for Exceptions thrown by the class

Implementation of:
Gumbo_Interface_Valid::getLevel()

[ Top ]
public bool isAlpha ( string $val ) [line 795]

Returns if the value is Alpha characters only

Parameter(s):

  • (string) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isAlphaNumeric ( string $val ) [line 815]

Returns if the value is AlphaNumeric characters only

Parameter(s):

  • (string) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isArray ( array $val ) [line 324]

Returns if the given value is an array

Parameter(s):

  • (array) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isBool ( bool $val ) [line 339]

Returns if the value is a boolean

Parameter(s):

  • (bool) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isDigit ( mix $val ) [line 835]

Returns if the value is Digits only

Parameter(s):

  • (mix) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isEqualTo ( mix $val, mix $case ) [line 762]

Returns if the value is Equal To the test case

Parameter(s):

  • (mix) $val
  • (mix) $case
  • throws:  Gumbo_Exception

[ Top ]
public bool isFalse ( bool $val ) [line 530]

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.

Parameter(s):

  • (bool) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isFloat ( num $val ) [line 354]

Returns if the value is a Float

Parameter(s):

  • (num) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isGreaterThan ( mix $val, mix $case ) [line 673]

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:

numeric (int,float)

  • case (numeric) : will compare values
string
  • case (numeric) : compare value string length to case value
  • case (string) : compare value string length to case string length
array
  • case (numeric) : compare value count of elements to case value
  • case (array) : compare value count of elements to case count of elements

Parameter(s):

  • (mix) $val
  • (mix) $case
  • throws:  Gumbo_Exception

[ Top ]
public bool isGreaterThanEqualTo ( mix $val, mix $case ) [line 725]

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:

numeric (int,float)

  • case (numeric) : will compare values
string
  • case (numeric) : compare value string length to case value
  • case (string) : compare value string length to case string length
array
  • case (numeric) : compare value count of elements to case value
  • case (array) : compare value count of elements to case count of elements

Parameter(s):

  • (mix) $val
  • (mix) $case
  • throws:  Gumbo_Exception

[ Top ]
public bool isHex ( string $val ) [line 855]

Returns if the value is Hex characters only

Parameter(s):

  • (string) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isIdenticalTo ( mix $val, mix $case ) [line 778]

Returns if the value is Identical To the test case

Parameter(s):

  • (mix) $val
  • (mix) $case
  • throws:  Gumbo_Exception

[ Top ]
public bool isInt ( int $val ) [line 464]

Returns if the value is an Integer

Parameter(s):

  • (int) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isLessThan ( mix $val, mix $case ) [line 569]

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:

numeric (int,float)

  • case (numeric) : will compare values
string
  • case (int) : compare value string length to case value
  • case (string) : compare value string length to case string length
array
  • case (int) : compare value count of elements to case value
  • case (array) : compare value count of elements to case count of elements

Parameter(s):

  • (mix) $val
  • (mix) $case
  • throws:  Gumbo_Exception

[ Top ]
public bool isLessThanEqualTo ( mix $val, mix $case ) [line 621]

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:

numeric (int,float)

  • case (numeric) : will compare values
string
  • case (numeric) : compare value string length to case value
  • case (string) : compare value string length to case string length
array
  • case (numeric) : compare value count of elements to case value
  • case (array) : compare value count of elements to case count of elements

Parameter(s):

  • (mix) $val
  • (mix) $case
  • throws:  Gumbo_Exception

[ Top ]
public bool isLong ( num $val ) [line 369]

Returns if the value is a Long

Parameter(s):

  • (num) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isLowercase ( string $val ) [line 875]

Returns if the value is Lowercase

Parameter(s):

  • (string) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isMatch ( string $val, [string $pattern] ) [line 480]

Returns if the value is a Match to the pattern

Parameter(s):

  • (string) $val
  • (string) $pattern
  • throws:  Gumbo_Exception

[ Top ]
public bool isNull ( null $val ) [line 384]

Returns if the value is Null

Parameter(s):

  • (null) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isNumeric ( num $val ) [line 399]

Returns if the value is Numeric

Parameter(s):

  • (num) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isObject ( StdClass $val, [string $cls] ) [line 430]

Returns if the value is an Object

Parameter(s):

  • (StdClass) $val
  • (string) $cls :: instance of class
  • throws:  Gumbo_Exception

[ Top ]
protected bool isOn ( ) [line 211]

Returns if Exceptions was turned on (temporarily)


[ Top ]
public bool isPrimitive ( mixed $val ) [line 309]

Returns if the given value is a primitive data type

Parameter(s):

  • (mixed) $val
  • precondition:  is a boolean, number, string, null
  • throws:  Gumbo_Exception

[ Top ]
public bool isPrintable ( string $val ) [line 915]

Returns if the value is Printable

Parameter(s):

  • (string) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isResource ( res $val ) [line 414]

Returns if the value is a Resource

Parameter(s):

  • (res) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isString ( string $val ) [line 449]

Returns if the value is a String

Parameter(s):

  • (string) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isThrowing ( ) [line 200]

Returns if Exceptions should be thrown from this class

Implementation of:
Gumbo_Interface_Valid::isThrowing()

[ Top ]
public bool isTrue ( bool $val ) [line 503]

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.

Parameter(s):

  • (bool) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool isUppercase ( string $val ) [line 895]

Returns if the value is Uppercase

Parameter(s):

  • (string) $val
  • throws:  Gumbo_Exception

[ Top ]
public bool precondition ( mixed $val ) [line 250]

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 or an array, simply: Gumbo_Valid::precondition ($val, "str", "arr");

The following are the strings to reference types:

  • 'bool' => boolean
  • 'int' => integer
  • 'num' => numeric, float, double
  • 'str' => string
  • 'arr' => array
  • 'obj' => object (any type of object)
  • 'res' => resource
  • 'null' => null

Parameter(s):

  • (mixed) $val
  • throws:  Gumbo_Exception

[ Top ]
public void setLevel ( int $level ) [line 167]

Sets the Error Level

Parameter(s):

  • (int) $level
  • throws:  Gumbo_Exception
Implementation of:
Gumbo_Interface_Valid::setLevel()

[ Top ]
public void setThrow ( bool $yes ) [line 149]

Sets if the class should throw Exceptions

Parameter(s):

  • (bool) $yes
  • throws:  Gumbo_Exception
Implementation of:
Gumbo_Interface_Valid::setThrow()

[ Top ]
protected void turnOff ( ) [line 137]

Turns throwing Exception OFF

  • postcondition:  !isOn()

[ Top ]
protected void turnOn ( ) [line 129]

Turns throwing Exceptions ON (temporarily)

  • postcondition:  isOn()

[ Top ]