Ejemplo n.º 1
0
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 ":" 
   takes in additional parameter to ensure that password is string
   variable list can be as individual eg. 'string' or 'string:numeric', if any falls into the allowed list, it would be accepted. if greedy all must match
*/
echo 'Signature: validate_type($variable, $allowed_list, $greedy = false)';
echo '//check if the variable belongs to type. can pass in multiple eg string:numeric validation (either one match, returns true) <br/>';
echo 'validate_type("x", "string"): ' . (validate_type('x', 'string') ? 'true' : 'false') . '<br/>';
echo 'validate_type("x", "numeric"): ' . (validate_type('x', 'numeric') ? 'true' : 'false') . '<br/>';
echo 'validate_type("x", "string:numeric"): ' . (validate_type('x', 'string:numeric') ? 'true' : 'false') . '<br/>';
echo 'validate_type("x", "string:numeric", true): ' . (validate_type('x', 'string:numeric', true) ? 'true' : 'false') . ' //greedy all must match <br/>';
echo 'validate_type("x", "ajoke", true): ' . (validate_type('x', 'ajoke', true) ? 'true' : 'false') . ' //incorrect match will return false <br/>';
echo '<br/>Passed<br/><hr/>';
/* checks if the variable length/value falls within the minimum/maximum range provided 
   method validates both string and numeric values 
   if string is provided, string's length is matched
   if numeric is provided, the value of the numeric is matched
   true is returned whenever the matched value is equals to the min/max, more than equals to min, or lesser than equals to max
   if any other cases false is returned.
*/
echo 'Signature: validate_range($variable, $min, $max)<br/>';
echo '//can pass in string (calculates length), numeric (check number range) - min/max inclusive<br/>';
echo 'validate_range($variable_range("xyz", 0, 3): ' . (validate_range('xyz', 0, 3) ? 'true' : 'false') . '<br/>';
echo 'validate_range($variable_range("", 1, 3): ' . (validate_range('', 1, 3) ? 'true' : 'false') . '<br/>';
echo 'validate_range($variable_range("xyz", 0, 2): ' . (validate_range('xyz', 0, 2) ? 'true' : 'false') . '<br/>';
echo 'validate_range($variable_range(-1, 0, 2): ' . (validate_range(-1, 0, 2) ? 'true' : 'false') . '<br/>';
echo 'validate_range($variable_range(3, 0, 2): ' . (validate_range(3, 0, 2) ? 'true' : 'false') . '<br/>';
Ejemplo n.º 2
0
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);
    }
}
Ejemplo n.º 3
0
function validate_range($variable, $min, $max)
{
    $results = false;
    if (is_null($variable) == false && isset($variable) == true) {
        if (validate_type($min, 'numeric:string:null') == true && validate_type($max, 'numeric:string:null') == true) {
            if (is_numeric($variable)) {
                if (is_null($min) == false && isset($min) == true && is_null($max) == false && isset($max) == true) {
                    if ($min == $max) {
                        if ($variable == $min) {
                            $results = true;
                        }
                    }
                    if ($variable >= $min && $variable <= $max) {
                        $results = true;
                    }
                } else {
                    if (is_null($min) == false && isset($min) == true && (is_null($max) == true || isset($max) == false)) {
                        if ($variable >= $min) {
                            return true;
                        }
                    } else {
                        if (is_null($max) == false && isset($max) == true && (is_null($min) == true || isset($min) == false)) {
                            if ($variable <= $max) {
                                return true;
                            }
                        }
                    }
                }
            } else {
                if (is_string($variable)) {
                    if (is_null($min) == false && isset($min) == true && is_null($max) == false && isset($max) == true) {
                        if ($min == $max) {
                            if (strlen($variable) == $min) {
                                $results = true;
                            }
                        }
                        if (strlen($variable) >= $min && strlen($variable) <= $max) {
                            $results = true;
                        }
                    } else {
                        if (is_null($min) == false && isset($min) == true && (is_null($max) == true || isset($max) == false)) {
                            if (strlen($variable) >= $min) {
                                $results = true;
                            }
                        } else {
                            if (is_null($max) == false && isset($max) == true && (is_null($min) == true || isset($min) == false)) {
                                if (strlen($variable) <= $max) {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $results;
}
Ejemplo n.º 4
0
function main()
{
    $type = validate_type();
    validate_type($type);
    validate_score();
    $score = validate_score();
    grading($type, $score);
}