echo 'compare_string("xyz", "XYZ"): ' . (compare_string('xyz', 'XYZ') ? 'true' : 'false') . '<br/>'; echo 'compare_string("xyz", "XYZ", true): ' . (compare_string('xyz', 'XYZ', true) ? 'true' : 'false') . '<br/>'; echo '<br/>Passed<br/><hr/>'; /* takes in a string list of items separated by the ':', it matches it against an array to check if the item exist in the array function is similar to array_contains/in_array however it allows multiple items to be validated at the same time @$greedy is a boolean value, when set to true, all items set to validate against the list must match if @$greedy is set to false, as long as any item matches, true is returned */ echo 'Signature: list_contains($items, $list, $greedy = true)<br/>'; echo '//greedy is used to compare two list, if true, all items in both list must match, if false, as long one match it is true <br/><br/>'; echo 'list_contains("xyz", array("abc", "def", "ghi")): ' . (list_contains("xyz", array("abc", "def", "ghi")) ? 'true' : 'false') . '<br/>'; echo 'list_contains("xyz", array("abc", "def", "xyz")): ' . (list_contains("xyz", array("abc", "def", "xyz")) ? 'true' : 'false') . '<br/>'; echo 'list_contains("xyz", array("abc", "def", "xyz"), true): ' . (list_contains("xyz", array("abc", "def", "xyz"), true) ? 'true' : 'false') . ' - no effect <br/>'; echo 'list_contains("xyz", array("abc", "def", "xyz"), false): ' . (list_contains("xyz", array("abc", "def", "xyz"), false) ? 'true' : 'false') . ' - no effect <br/>'; echo 'list_contains("abc:def:khl", array("abc", "def", "xyz"), true): ' . (list_contains("abc:def:khl", array("abc", "def", "xyz"), true) ? 'true' : 'false') . '<br/>'; echo 'list_contains("abc:def:khl", array("abc", "def", "xyz"), false): ' . (list_contains("abc:def:khl", array("abc", "def", "xyz"), false) ? 'true' : 'false') . '<br/>'; echo '<br/>Passed<br/><hr/>'; /* takes in a string list of items separated by the ':', it matches it against an array keys to check if the item exist in the array function is checks if the keys exists in the array, list given must be an associative array (else return false) @$greedy is a boolean value, when set to true, all items set to validate against the list must match if @$greedy is set to false, as long as any item matches, true is returned */ echo 'Signature: list_contains_keys($items, $list, $greedy = true)<br/>'; echo '//similar to list_contains, but validates keys instead. will not work with non-associative array, it will return false <br/><br/>'; echo 'list_contains_keys("0:1:2", array("abc", "def", "ghi")): ' . (list_contains_keys("0:1:2", array("abc", "def", "ghi"), false) ? 'true' : 'false') . ' - not associative<br/>'; echo 'list_contains_keys("one:two:three", array("one"=>"abc", "two"=>"def", "three"=>"ghi"), true): ' . (list_contains_keys("one:two:three", array("one" => "abc", "two" => "def", "three" => "ghi"), true) ? 'true' : 'false') . '<br/>'; echo 'list_contains_keys("one", array("one"=>"abc", "two"=>"def", "three"=>"ghi"), false): ' . (list_contains_keys("one", array("one" => "abc", "two" => "def", "three" => "ghi"), false) ? 'true' : 'false') . '<br/>'; echo 'list_contains_keys("one", array("one"=>"abc", "two"=>"def", "three"=>"ghi"), true): ' . (list_contains_keys("one", array("one" => "abc", "two" => "def", "three" => "ghi"), true) ? 'true' : 'false') . '<br/>'; echo '<br/>Passed<br/><hr/>'; echo '<h3>To Type Validation</h3>'; /* checks if the variable passed in is within the allow list of variable types (tools/constants/constants.php). List seperated by ":"
function enforce_inputs() { $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__); $message = ''; $variable = ''; $line = ''; //retrieve last item as return type if available $array = func_get_args(); $return_type = end($array); if (is_string($return_type) == false) { $return_type = Constants::get('default_return_type'); } else { $allowed_return_types = Constants::get('allowed_return_types'); if (array_contains($return_type, $allowed_return_types) == false) { $return_type = Constants::get('default_return_type'); } } //loop through every item to validate, sets a message to throw the error at the end of the method foreach ($array as $list) { if (is_ready($list)) { //every list must be ready if (is_array($list)) { //ensure that item is an array if (count($list) == 5) { //every list contains the required items $variable = set_default($list[0], null); $type = set_default($list[1], ':'); $min = set_default($list[2], null); $max = set_default($list[3], null); $nullable = set_default($list[4], false); $validation_list = null; if (isset($variable)) { //ensure that variable is set //ensures that valid variable list type is request if (list_contains($type, Constants::get('variable_list')) == false) { $message = 'Invalid variable validation requirement - type (' . $type . ') unrecognized.'; $line = __LINE__; } //if either one is not a numeric, check for array if (validate_type($min, 'numeric') == false || validate_type($max, 'numeric') == false) { if (validate_type($min, 'array') == true || validate_type($max, 'array') == true) { $validation_list = (is_null($min) == true || isset($min) == false) && validate_type($max, 'array') ? $max : $min; if (validate_type($validation_list, 'array') == false) { $message = 'Invalid variable validation requirement - an array to validate is required.'; $line = __LINE__; } } else { if (is_null($min) == false || is_null($max) == false) { $message = 'Invalid variable validation requirement - min/max must be numeric or an array of list in either one or both nulls.'; $line = __LINE__; } } //ensure than max is less than min } else { if ($max < $min) { $message = 'Invalid variable validation requirement - min is more than max.'; $line = __LINE__; } } //ensure that nullable is boolean if (validate_type($nullable, 'bool') == false) { $message = 'Invalid variable validation requirement - nullable must be boolean.'; $line = __LINE__; } //verify variable if not null if (!is_null($variable)) { if (validate_type($variable, $type) == false) { $message = 'Variable is not a ' . $type . '.'; $line = __LINE__; } if (is_null($validation_list) == false && isset($validation_list) == true) { if (array_contains($variable, $validation_list) == false) { $message = 'Variable is not found in the list provided.'; $line = __LINE__; } } else { if (validate_type($variable, 'string:numeric') == true) { if (is_null($min) == false || is_null($max) == false) { if (validate_range($variable, $min, $max) == false) { $message = 'Variable does not meet the min/max requirement.'; $line = __LINE__; } } } } } } else { if ($nullable == false) { $message = 'Variable is not set, unable to validate variable'; $line = __LINE__; } } } else { $message = 'Incomplete variable validation list. [variable, type, min, max, nullable*]'; $line = __LINE__; } } } else { $message = 'Invalid variable validation list, an array is required. [variable, type, min, max, nullable*]'; $line = __LINE__; } } if (compare_string($message, '') == false) { $variable_name = variable_name($variable); if (isset($variable_name) == true && $variable_name != '') { $variable_name = '$' . $variable_name; } else { $variable_name = $type != 'password' ? $variable : '*password*'; } if ($variable_name != '') { $variable_name = ' [' . $variable_name . ']'; } $error = Tool::prepare($message . $variable_name, '', $line, $return_type, Constants::get('default_error_code')); Tool::error($function, $error, false); } }