validate['Name'] as $rule) {
if (!isset($rule['rule'])) continue;
$spec = $rule['rule'];
if (is_array($spec)) {
$method = array_shift($spec);
$args = array_merge(array($value), $spec);
if (!call_user_func_array(array('Validation', $method), $args)) return false;
} else {
if (!call_user_func(array('Validation', $spec), $value)) return false;
}
}
return true;
}
$tests = 0;
$failures = 0;
function check($label, $cond) {
global $tests, $failures;
$tests++;
if ($cond) {
echo " ok - $label\n";
} else {
$failures++;
echo " NOT OK - $label\n";
}
}
$valid = array(
'Front Door',
'Camera-1 (north): lot/2.1',
'Caméra été',
'車庫',
'cam_01',
);
$invalid = array(
'
',
'">',
"a'b",
'a=b',
'x',
'',
);
foreach ($valid as $value) {
check('valid: '.$value, nameIsValid($value) === true);
}
foreach ($invalid as $value) {
$label = $value === '' ? '(blank)' : $value;
check('invalid: '.$label, nameIsValid($value) === false);
}
echo "\n$tests tests, $failures failures\n";
exit($failures ? 1 : 0);
?>