1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| /**
* @brief Generic validation function
*
* This function takes an input (either an INPUT_* integer or an associative array) and a required structure to filter
* against and computes error messages (if any).
* @c $structure parameter is described as follow
* @code
* $structure = array(
* 'a' => array(
* 'filter' => FILTER_VALIDATE_INT,
* 'missing' => 'A is missing',
* 'invalid' => 'A is invalid',
* ),
* 'b' => array(
* 'filter' => FILTER_SANITIZE_STRING,
* 'missing' => 'B is missing',
* 'invalid' => 'B is invalid',
* ),
* 'c' => array(
* 'filter' => FILTER_VALIDATE_EMAIL,
* 'missing' => 'C is missing',
* 'invalid' => 'C is invalid',
* ),
* );
* @endcode
*
* @param integer|array $input The input to validate, can be either INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER,
* INPUT_ENV or an associative array
* @param array $structure The filter structure, optionnaly padded with error messages
* @param array (out) $messages Error messages fetched from missing or invalid entries in $structure array
* @param boolean $silent @optional @default{false} If set to true, will not throw errors when the 'filter' parameter
* is missing
* @throws RuntimeException If used with PHP < 5.2
* @throws InvalidArgumentException If a parameter is invalid
*/
function validate ($input, array $structure, array & $messages = null, $silent = false) {
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 50200)
throw new RuntimeException(__FUNCTION__ . " requires PHP 5.2 or higher");
if (empty($structure))
throw new InvalidArgumentException("Structure paramter is empty");
if (is_int($input) && !in_array($input, array(INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, INPUT_ENV)))
throw new InvalidArgumentException(
"Input parameter must be one of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV");
elseif (is_int($input))
$validate = 'filter_input_array';
elseif (is_array($input))
$validate = 'filter_var_array';
else
throw new InvalidArgumentException(
"Input parameter is expected to be integer or array," . gettype($input) . "given");
$filter = array();
$messages_missing = array();
$messages_invalid = array();
foreach ($structure as $key => $value) {
if (isset($value['filter'])) {
$filter[$key] = $value['filter'];
}
else {
!$silent && trigger_error("No filter specified for $key, using FILTER_UNSAFE_RAW", E_USER_WARNING);
$filter[$key] = FILTER_UNSAFE_RAW;
}
if (isset($value['missing'])) $messages_missing[$key] = $value['missing'];
if (isset($value['invalid'])) $messages_invalid[$key] = $value['invalid'];
}
if (!$data = $validate($input, $filter))
return $data;
$messages = array();
$messages += array_intersect_key($messages_invalid, array_flip(array_keys($data, false, true)));
$messages += array_intersect_key($messages_missing, array_flip(array_keys($data, null, true)));
return $data;
} |
Partager