PHP Class GUMP

Author: Sean Nieuwoudt (http://twitter.com/SeanNieuwoudt)
Show file Open project: wixel/gump Class Usage Examples

Public Properties

Property Type Description
$basic_tags ** ------------------------- Validation Data ------------------------------- ** //
$en_noise_words

Protected Properties

Property Type Description
$errors Instance attribute containing errors from last run
$fieldCharsToRemove field characters below will be replaced with a space.
$fields Contain readable field names that have been set manually
$filter_methods Customer filter methods
$filter_rules Filter rules for execution
$instance Singleton instance of GUMP
$validation_methods Custom validation methods
$validation_rules Validation rules for execution

Public Methods

Method Description
__toString ( ) : string Magic method to generate the validation error messages.
add_filter ( string $rule, callable $callback ) : boolean Adds a custom filter using a callback function.
add_validator ( string $rule, callable $callback ) : boolean Adds a custom validation rule using a callback function.
errors ( ) : array Return the error array from the last validation run.
field ( mixed $key, array $array, mixed $default = null ) : mixed Helper method to extract an element from an array safely
filter ( array $input, array $filterset ) : mixed Filter the input data according to the specified filter set.
filter_input ( array $data, array $filters ) : mixed Shorthand method for running only the data filters.
filter_rules ( array $rules = [] ) : array Getter/Setter for the filter rules.
get_errors_array ( $convert_to_string = null ) : array | null Process the validation errors and return an array of errors with field names as keys.
get_instance ( ) : GUMP Function to create and return previously created instance
get_readable_errors ( boolean $convert_to_string = false, string $field_class = 'gump-field', string $error_class = 'gump-error-message' ) : string Process the validation errors and return human readable error messages.
is_valid ( array $data, array $validators ) : mixed Shorthand method for inline validation.
run ( array $data, boolean $check_fields = false ) : array Run the filtering and validation after each other.
sanitize ( array $input, array $fields = [], boolean $utf8_encode = true ) : array Sanitize the input data.
set_field_name ( string $field, string $readable_name ) Set a readable name for a specified field names.
set_field_names ( array $array ) Set readable name for specified fields in an array.
validate ( array $input, array $ruleset ) : mixed Perform data validation against the provided ruleset.
validation_rules ( array $rules = [] ) : array Getter/Setter for the validation rules.
xss_clean ( array $data ) : array Perform XSS clean to prevent cross site scripting.

Protected Methods

Method Description
filter_basic_tags ( string $value, array $params = null ) : string Filter out all HTML tags except the defined basic tags.
filter_htmlencode ( string $value, array $params = null ) : string Sanitize the string by converting HTML characters to their HTML entities.
filter_ms_word_characters ( string $value, array $params = null ) : string Convert MS Word special characters to web safe characters.
filter_noise_words ( string $value, array $params = null ) : string Replace noise words in a string (http://tax.cchgroup.com/help/Avoiding_noise_words_in_your_search.htm).
filter_rmpunctuation ( string $value, array $params = null ) : string Remove all known punctuation from a string.
filter_sanitize_email ( string $value, array $params = null ) : string Sanitize the string by removing illegal characters from emails.
filter_sanitize_floats ( string $value, array $params = null ) : string Sanitize the string by removing illegal characters from float numbers.
filter_sanitize_numbers ( string $value, array $params = null ) : string Sanitize the string by removing illegal characters from numbers.
filter_sanitize_string ( string $value, array $params = null ) : string Sanitize the string by removing any script tags.
filter_urlencode ( string $value, array $params = null ) : string Sanitize the string by urlencoding characters.
filter_whole_number ( string $value, array $params = null ) : string Convert the provided numeric value to a whole number.
shouldRunValidation ( array $input, $rules, $field ) : boolean Overloadable method to invoke validation.
validate_alpha ( string $field, array $input, null $param = null ) : mixed Determine if the provided value contains only alpha characters.
validate_alpha_dash ( string $field, array $input, null $param = null ) : mixed Determine if the provided value contains only alpha characters with dashed and underscores.
validate_alpha_numeric ( string $field, array $input, null $param = null ) : mixed Determine if the provided value contains only alpha-numeric characters.
validate_alpha_space ( string $field, array $input, null $param = null ) : mixed Determine if the provided value contains only alpha numeric characters with spaces.
validate_boolean ( string $field, array $input, null $param = null ) : mixed Determine if the provided value is a PHP accepted boolean.
validate_contains ( string $field, array $input, null $param = null ) : mixed Verify that a value is contained within the pre-defined value set.
validate_contains_list ( string $field, array $input, $param = null ) : mixed Verify that a value is contained within the pre-defined value set.
validate_date ( string $field, string $input, null $param = null ) : mixed Determine if the provided input is a valid date (ISO 8601).
validate_doesnt_contain_list ( string $field, array $input, $param = null ) : mixed Verify that a value is NOT contained within the pre-defined value set.
validate_equalsfield ( string $field, string $input, string $param = null ) : mixed Determine if the provided field value equals current field value.
validate_exact_len ( string $field, array $input, null $param = null ) : mixed Determine if the provided value length matches a specific value.
validate_extension ( string $field, array $input, $param = null ) : mixed check the uploaded file for extension for now checks onlt the ext should add mime type check.
validate_float ( string $field, array $input, null $param = null ) : mixed Determine if the provided value is a valid float.
validate_guidv4 ( string $field, string $input, string $param = null ) : mixed Determine if the provided field value is a valid GUID (v4)
validate_iban ( string $field, array $input, $param = null ) : mixed Determine if the provided value is a valid IBAN.
validate_integer ( string $field, array $input, null $param = null ) : mixed Determine if the provided value is a valid integer.
validate_max_len ( string $field, array $input, null $param = null ) : mixed Determine if the provided value length is less or equal to a specific value.
validate_max_numeric ( string $field, array $input, null $param = null ) : mixed Determine if the provided numeric value is lower or equal to a specific value.
validate_min_age ( string $field, string $input, string $param = null ) : mixed Determine if the provided input meets age requirement (ISO 8601).
validate_min_len ( string $field, array $input, null $param = null ) : mixed Determine if the provided value length is more or equal to a specific value.
validate_min_numeric ( string $field, array $input, null $param = null ) : mixed Determine if the provided numeric value is higher or equal to a specific value.
validate_numeric ( string $field, array $input, null $param = null ) : mixed Determine if the provided value is a valid number or numeric string.
validate_phone_number ( string $field, array $input, $param = null ) : mixed Determine if the provided value is a valid phone number.
validate_regex ( string $field, array $input, $param = null ) : mixed Custom regex validator.
validate_required ( string $field, array $input, null $param = null ) : mixed Check if the specified key is present and not empty.
validate_required_file ( string $field, array $input, $param = null ) : mixed checks if a file was uploaded.
validate_starts ( string $field, array $input, $param = null ) : mixed Determine if the provided value starts with param.
validate_street_address ( string $field, array $input, $param = null ) : mixed Determine if the provided input is likely to be a street address using weak detection.
validate_url_exists ( string $field, array $input, null $param = null ) : mixed Determine if a URL exists & is accessible.
validate_valid_cc ( string $field, array $input, $param = null ) : mixed Determine if the input is a valid credit card number.
validate_valid_email ( string $field, array $input, null $param = null ) : mixed Determine if the provided email is valid.
validate_valid_ip ( string $field, array $input, $param = null ) : mixed Determine if the provided value is a valid IP address.
validate_valid_ipv4 ( $field, $input, $param = null ) * What about private networks? http://en.wikipedia.org/wiki/Private_network What about loop-back address? 127.0.0.1
validate_valid_ipv6 ( string $field, array $input, $param = null ) : mixed Determine if the provided value is a valid IPv6 address.
validate_valid_json_string ( string $field, array $input, $param = null ) : mixed Json validatior.
validate_valid_name ( string $field, array $input, $param = null ) : mixed Determine if the input is a valid human name [Credits to http://github.com/ben-s].
validate_valid_url ( string $field, array $input, null $param = null ) : mixed Determine if the provided value is a valid URL.

Private Methods

Method Description
check_fields ( array $data ) Ensure that the field counts match the validation rule counts.
trimScalar ( mixed $value ) : mixed Trims whitespace only when the value is a scalar.

Method Details

__toString() public method

Magic method to generate the validation error messages.
public __toString ( ) : string
return string

add_filter() public static method

Adds a custom filter using a callback function.
public static add_filter ( string $rule, callable $callback ) : boolean
$rule string
$callback callable
return boolean

add_validator() public static method

Adds a custom validation rule using a callback function.
public static add_validator ( string $rule, callable $callback ) : boolean
$rule string
$callback callable
return boolean

errors() public method

Return the error array from the last validation run.
public errors ( ) : array
return array

field() public static method

Helper method to extract an element from an array safely
public static field ( mixed $key, array $array, mixed $default = null ) : mixed
$key mixed
$array array
$default mixed
return mixed

filter() public method

Filter the input data according to the specified filter set.
public filter ( array $input, array $filterset ) : mixed
$input array
$filterset array
return mixed

filter_basic_tags() protected method

Filter out all HTML tags except the defined basic tags.
protected filter_basic_tags ( string $value, array $params = null ) : string
$value string
$params array
return string

filter_htmlencode() protected method

Usage: '' => 'htmlencode'
protected filter_htmlencode ( string $value, array $params = null ) : string
$value string
$params array
return string

filter_input() public static method

Shorthand method for running only the data filters.
public static filter_input ( array $data, array $filters ) : mixed
$data array
$filters array
return mixed

filter_ms_word_characters() protected method

[“, ”, ‘, ’, –, …] => [", ", ', ', -, ...]
protected filter_ms_word_characters ( string $value, array $params = null ) : string
$value string
$params array
return string

filter_noise_words() protected method

Usage: '' => 'noise_words'
protected filter_noise_words ( string $value, array $params = null ) : string
$value string
$params array
return string

filter_rmpunctuation() protected method

Usage: '' => 'rmpunctuataion'
protected filter_rmpunctuation ( string $value, array $params = null ) : string
$value string
$params array
return string

filter_rules() public method

Getter/Setter for the filter rules.
public filter_rules ( array $rules = [] ) : array
$rules array
return array

filter_sanitize_email() protected method

Usage: '' => 'sanitize_email'
protected filter_sanitize_email ( string $value, array $params = null ) : string
$value string
$params array
return string

filter_sanitize_floats() protected method

Sanitize the string by removing illegal characters from float numbers.
protected filter_sanitize_floats ( string $value, array $params = null ) : string
$value string
$params array
return string

filter_sanitize_numbers() protected method

Sanitize the string by removing illegal characters from numbers.
protected filter_sanitize_numbers ( string $value, array $params = null ) : string
$value string
$params array
return string

filter_sanitize_string() protected method

Usage: '' => 'sanitize_string'
protected filter_sanitize_string ( string $value, array $params = null ) : string
$value string
$params array
return string

filter_urlencode() protected method

Usage: '' => 'urlencode'
protected filter_urlencode ( string $value, array $params = null ) : string
$value string
$params array
return string

filter_whole_number() protected method

Convert the provided numeric value to a whole number.
protected filter_whole_number ( string $value, array $params = null ) : string
$value string
$params array
return string

get_errors_array() public method

Process the validation errors and return an array of errors with field names as keys.
public get_errors_array ( $convert_to_string = null ) : array | null
$convert_to_string
return array | null | null (if empty)

get_instance() public static method

Function to create and return previously created instance
public static get_instance ( ) : GUMP
return GUMP

get_readable_errors() public method

Process the validation errors and return human readable error messages.
public get_readable_errors ( boolean $convert_to_string = false, string $field_class = 'gump-field', string $error_class = 'gump-error-message' ) : string
$convert_to_string boolean = false
$field_class string
$error_class string
return string

is_valid() public static method

Shorthand method for inline validation.
public static is_valid ( array $data, array $validators ) : mixed
$data array The data to be validated
$validators array The GUMP validators
return mixed True(boolean) or the array of error messages

run() public method

Run the filtering and validation after each other.
public run ( array $data, boolean $check_fields = false ) : array
$data array
$check_fields boolean
return array

sanitize() public method

Sanitize the input data.
public sanitize ( array $input, array $fields = [], boolean $utf8_encode = true ) : array
$input array
$fields array
$utf8_encode boolean
return array

set_field_name() public static method

Set a readable name for a specified field names.
public static set_field_name ( string $field, string $readable_name )
$field string
$readable_name string

set_field_names() public static method

Usage: GUMP::set_field_names(array( "name" => "My Lovely Name", "username" => "My Beloved Username", ));
public static set_field_names ( array $array )
$array array

shouldRunValidation() protected method

Overloadable method to invoke validation.
protected shouldRunValidation ( array $input, $rules, $field ) : boolean
$input array
$rules
$field
return boolean

validate() public method

Perform data validation against the provided ruleset.
public validate ( array $input, array $ruleset ) : mixed
$input array
$ruleset array
return mixed

validate_alpha() protected method

Usage: '' => 'alpha'
protected validate_alpha ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_alpha_dash() protected method

Usage: '' => 'alpha_dash'
protected validate_alpha_dash ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_alpha_numeric() protected method

Usage: '' => 'alpha_numeric'
protected validate_alpha_numeric ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_alpha_space() protected method

Usage: '' => 'alpha_space'
protected validate_alpha_space ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_boolean() protected method

Usage: '' => 'boolean'
protected validate_boolean ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_contains() protected method

Usage: '' => 'contains,value value value'
protected validate_contains ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_contains_list() protected method

OUTPUT: will NOT show the list of values. Usage: '' => 'contains_list,value;value;value'
protected validate_contains_list ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_date() protected method

Usage: '' => 'date'
protected validate_date ( string $field, string $input, null $param = null ) : mixed
$field string
$input string date ('Y-m-d') or datetime ('Y-m-d H:i:s')
$param null
return mixed

validate_doesnt_contain_list() protected method

OUTPUT: will NOT show the list of values. Usage: '' => 'doesnt_contain_list,value;value;value'
protected validate_doesnt_contain_list ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_equalsfield() protected method

Usage: '' => 'equalsfield,Z'
protected validate_equalsfield ( string $field, string $input, string $param = null ) : mixed
$field string
$input string
$param string field to compare with
return mixed

validate_exact_len() protected method

Usage: '' => 'exact_len,5'
protected validate_exact_len ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_extension() protected method

Usage: '' => 'starts,Z'
protected validate_extension ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_float() protected method

Usage: '' => 'float'
protected validate_float ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_guidv4() protected method

Usage: '' => 'guidv4'
protected validate_guidv4 ( string $field, string $input, string $param = null ) : mixed
$field string
$input string
$param string field to compare with
return mixed

validate_iban() protected method

Usage: '' => 'iban'
protected validate_iban ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_integer() protected method

Usage: '' => 'integer'
protected validate_integer ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_max_len() protected method

Usage: '' => 'max_len,240'
protected validate_max_len ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_max_numeric() protected method

Usage: '' => 'max_numeric,50'
protected validate_max_numeric ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_min_age() protected method

Usage: '' => 'min_age,13'
protected validate_min_age ( string $field, string $input, string $param = null ) : mixed
$field string
$input string date ('Y-m-d') or datetime ('Y-m-d H:i:s')
$param string int
return mixed

validate_min_len() protected method

Usage: '' => 'min_len,4'
protected validate_min_len ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_min_numeric() protected method

Usage: '' => 'min_numeric,1'
protected validate_min_numeric ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_numeric() protected method

Usage: '' => 'numeric'
protected validate_numeric ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_phone_number() protected method

Usage: '' => 'phone_number'
protected validate_phone_number ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed Examples: 555-555-5555: valid 5555425555: valid 555 555 5555: valid 1(519) 555-4444: valid 1 (519) 555-4422: valid 1-555-555-5555: valid 1-(555)-555-5555: valid

validate_regex() protected method

Usage: '' => 'regex,/your-regex-expression/'
protected validate_regex ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_required() protected method

Usage: '' => 'required'
protected validate_required ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_required_file() protected method

Usage: '' => 'required_file'
protected validate_required_file ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_starts() protected method

Usage: '' => 'starts,Z'
protected validate_starts ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_street_address() protected method

Usage: '' => 'street_address'
protected validate_street_address ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_url_exists() protected method

Usage: '' => 'url_exists'
protected validate_url_exists ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_valid_cc() protected method

See: http://stackoverflow.com/questions/174730/what-is-the-best-way-to-validate-a-credit-card-in-php Usage: '' => 'valid_cc'
protected validate_valid_cc ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_valid_email() protected method

Usage: '' => 'valid_email'
protected validate_valid_email ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validate_valid_ip() protected method

Usage: '' => 'valid_ip'
protected validate_valid_ip ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_valid_ipv4() protected method

* What about private networks? http://en.wikipedia.org/wiki/Private_network What about loop-back address? 127.0.0.1
protected validate_valid_ipv4 ( $field, $input, $param = null )

validate_valid_ipv6() protected method

Usage: '' => 'valid_ipv6'
protected validate_valid_ipv6 ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_valid_json_string() protected method

Usage: '' => 'valid_json_string'
protected validate_valid_json_string ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_valid_name() protected method

See: https://github.com/Wixel/GUMP/issues/5 Usage: '' => 'valid_name'
protected validate_valid_name ( string $field, array $input, $param = null ) : mixed
$field string
$input array
return mixed

validate_valid_url() protected method

Usage: '' => 'valid_url'
protected validate_valid_url ( string $field, array $input, null $param = null ) : mixed
$field string
$input array
$param null
return mixed

validation_rules() public method

Getter/Setter for the validation rules.
public validation_rules ( array $rules = [] ) : array
$rules array
return array

xss_clean() public static method

Perform XSS clean to prevent cross site scripting.
public static xss_clean ( array $data ) : array
$data array
return array

Property Details

$basic_tags public static property

** ------------------------- Validation Data ------------------------------- ** //
public static $basic_tags

$en_noise_words public static property

public static $en_noise_words

$errors protected property

Instance attribute containing errors from last run
protected $errors

$fieldCharsToRemove protected property

field characters below will be replaced with a space.
protected $fieldCharsToRemove

$fields protected static property

Contain readable field names that have been set manually
protected static $fields

$filter_methods protected static property

Customer filter methods
protected static $filter_methods

$filter_rules protected property

Filter rules for execution
protected $filter_rules

$instance protected static property

Singleton instance of GUMP
protected static $instance

$validation_methods protected static property

Custom validation methods
protected static $validation_methods

$validation_rules protected property

Validation rules for execution
protected $validation_rules