<?php
/**
* IsSetAnd Class File
*
* @author Corey Tisdale <hide@address.com>
* @copyright Copyright © 2011 Eye Want Media LLC
* @license MIT license
*/
/**
* IsSetAnd is a class for checking if array keys are set and compare them to something
* without rewriting many lines of code.
*
* If the value is there and meets the criteria, you may access it via IsSetAnd::$value
* as a shorthand way of not rewriting a portentially really long or complicated array
* referencestores
*
*
* Example Usage:
*
* $username = IsSetAnd::NotEmpty($_GET, 'username');
*
* OR
*
* if(null !== IsSetAnd::GreaterThanZero($_GET, 'years_old')) {
* echo "You are " . IsSetAnd::$value . " years old!";
* }
* In this example, please note that IsSetAnd::value will hold the value of the array key you tested
* if the test was true, otherwise it will be null
*/
class IsSetAnd {
/**
* @var mixed the value that you tested for, given that the test passed. Otherwise this
* value will be null.
*/
public static $value = null;
/**
* Does the work of checking the results of your test, and saving the value if
* necessary
* @param array $array the array that was tested
* @param mixed $key the key of the array that was tested
* @param bool $bool the result of the test
* @return mixed the value of the array key tested, or null if the test failed
*/
protected static function WorkTheMagic($array, $key, $bool) {
//This whole class is a shortcut class, so this is all it really does, just under different conditions
if($bool) {
IsSetAnd::$value = $array[$key]; //if we are good, store the value for retreival
} else {
IsSetAnd::$value = null; //else store null
}
return IsSetAnd::$value; //return null if no match, otherwise whatever was matched.
}
/**
* Checks to see if your $array[$key] exists, and if so, is it equal to $compval
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @param mixed $compval the value to compare $array[$key] to, if $array[$key] exists
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function Equals($array, $key, $compval) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && $array[$key] == $compval );
}
/**
* Checks to see if your $array[$key] exists, and if so, is it not empty
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function NotEmpty($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && !empty($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it numeric
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsNumeric($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && is_numeric($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it a ctype_alnum
* alphanumeric character
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsCtypeAlnum($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ctype_alnum($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it a ctype_alpha
* alpha character
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsCtypeAlpha($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ctype_alpha($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it a ctype_cntrl
* control character
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsCtypeCntrl($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ctype_cntrl($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it a ctype_digit
* digit character
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsCtypeDigit($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ctype_digit($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it a ctype_graph
* graph character, or any printable character except space
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsCtypeGraph($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ctype_graph($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it a ctype_lower
* lower case character
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsCtypeLower($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ctype_lower($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it a ctype_print
* printable character
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsCtypePrint($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ctype_print($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it a ctype_punct
* punctuation character, which is any non-space, non alphanumberic character
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsCtypePunct($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ctype_punct($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it a ctype_space
* whitespace character
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsCtypeSpace($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ctype_space($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it a ctype_upper
* uppercase character
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsCtypeUpper($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ctype_upper($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it a ctype_xdigit
* hexadecimal character
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsCtypeXdigit($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ctype_xdigit($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it an array
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsArray($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && is_array($array[$key]));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it an array that has at least one element
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function IsFilledArray($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && is_array($array[$key]) && count($array[$key]) > 0);
}
/**
* Checks to see if your $array[$key] exists, and if so, is it's value greater than zero
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function GreaterThanZero($array, $key) {
return IsSetAnd::GreaterThan($array, $key, 0);
}
/**
* Checks to see if your $array[$key] exists, and if so, is it greater than $compval
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @param mixed $compval the value to compare $array[$key] to, if $array[$key] exists
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function GreaterThan($array, $key, $compval) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ($array[$key] > $compval));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it less than to $compval
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @param mixed $compval the value to compare $array[$key] to, if $array[$key] exists
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function LessThan($array, $key, $compval) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ($array[$key] < $compval));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it greater than or equal to $compval
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @param mixed $compval the value to compare $array[$key] to, if $array[$key] exists
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function GreaterThanOrEqualTo($array, $key, $compval) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ($array[$key] >= $compval));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it less than or equal to $compval
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @param mixed $compval the value to compare $array[$key] to, if $array[$key] exists
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function LessThanOrEqualTo($array, $key, $compval) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ($array[$key] <= $compval));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it equal to boolean true
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function True($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ($array[$key] === true));
}
/**
* Checks to see if your $array[$key] exists, and if so, is it equal to boolean false
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function False($array, $key) {
return IsSetAnd::WorkTheMagic($array, $key, isset($array[$key]) && ($array[$key] === false));
}
/**
* Checks to see if your $array[$key] exists, and if so, run the user function passed.
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @param mixed $fucntion the function to be passed to call_user_func. This could take
* the folowing forms: a string that is the function name, a lambda function, an array
* in this format array($myObject, 'methodname') for instance class calls, and finally
* array('classname', 'methodname') for the method to be called statically
* @param array $params any additional params to pass to call_user_func_array. They
* will be passed after the main value. This parameter is optional.
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function UserFunctionIsTrue($array, $key, $function, $params = array()) {
//See if the array key even exists
$bool = isset($array[$key]);
if($bool) {
$params = array_merge(array($array[$key]), $params); //make our params all into one array so we can pass it
$bool = call_user_func_array($function, $params); //we don't need to and this because we know bool has to be true here
}
return IsSetAnd::WorkTheMagic($array, $key, $bool);
}
/**
* Checks to see if your $array[$key] exists, and if so, see if it matches a regex
* @param array $array the array to be tested
* @param mixed $key the key of the array to be tested
* @param string $regex the regex to match against
* @param integer $flags the flags you wish to pass wo preg_match. Optional
* @param integer $offset the offset you wish to pass to preg_match. Optional
* @return mixed the value of the array key tested, or null if the test failed
*/
public static function MatchesRegex($array, $key, $regex, $flags = null, $offset = null) {
//See if the array key even exists
$bool = isset($array[$key]);
$matches = array(); //have to have something to pass to preg_match so we can also pass flags and offset
if($bool) {
$bool = preg_match($regex, $array[$key], $matches, $flags, $offset); //we don't need to and this because we know bool has to be true here
}
return IsSetAnd::WorkTheMagic($array, $key, $bool);
}
}