2010-09-23 21:46:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Respect\Validation;
|
|
|
|
|
|
|
|
use ReflectionClass;
|
|
|
|
|
2010-09-24 05:32:00 +02:00
|
|
|
abstract class Validator
|
2010-09-23 21:46:46 +02:00
|
|
|
{
|
|
|
|
|
2010-09-24 05:32:00 +02:00
|
|
|
public static function validate($input, $validatorName)
|
|
|
|
{
|
|
|
|
$arguments = func_get_args();
|
|
|
|
$arguments = array_slice($arguments, 2);
|
|
|
|
return static::buildValidator($validatorName, $arguments)->validate($input);
|
|
|
|
}
|
2010-09-23 21:46:46 +02:00
|
|
|
|
2010-09-24 05:32:00 +02:00
|
|
|
public static function is($input, $validatorName)
|
2010-09-23 21:46:46 +02:00
|
|
|
{
|
2010-09-24 05:32:00 +02:00
|
|
|
$arguments = func_get_args();
|
|
|
|
$arguments = array_slice($arguments, 2);
|
|
|
|
return static::buildValidator($validatorName, $arguments)->isValid($input);
|
2010-09-23 21:46:46 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 05:32:00 +02:00
|
|
|
public static function __callStatic($method, $arguments)
|
|
|
|
{
|
|
|
|
if (2 > count($arguments))
|
|
|
|
return false;
|
|
|
|
if (0 === strpos($method, 'valid')) {
|
|
|
|
$arguments[1] = substr($method, 5) . '\\' . $arguments[1];
|
|
|
|
$arguments[1] = trim($arguments[1], '\\');
|
|
|
|
return call_user_func_array(
|
|
|
|
array(get_called_class(), 'validate'), $arguments
|
|
|
|
);
|
|
|
|
} elseif (0 === strpos($method, 'is')) {
|
|
|
|
$arguments[1] = substr($method, 2) . '\\' . $arguments[1];
|
|
|
|
$arguments[1] = trim($arguments[1], '\\');
|
|
|
|
return call_user_func_array(
|
|
|
|
array(get_called_class(), 'is'), $arguments
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function buildValidator($validator, $arguments=array())
|
2010-09-23 21:46:46 +02:00
|
|
|
{
|
|
|
|
if ($validator instanceof Validatable) {
|
2010-09-24 05:32:00 +02:00
|
|
|
return $validator;
|
2010-09-23 21:46:46 +02:00
|
|
|
}
|
|
|
|
if (is_object($validator))
|
2010-09-24 03:56:17 +02:00
|
|
|
throw new ComponentException(
|
2010-09-24 04:30:16 +02:00
|
|
|
sprintf('%s does not implement the Respect\Validator\Validatable interface required for validators',
|
|
|
|
get_class($validator))
|
2010-09-23 21:46:46 +02:00
|
|
|
);
|
|
|
|
$validatorFqn = explode('\\', get_called_class());
|
|
|
|
array_pop($validatorFqn);
|
|
|
|
$validatorFqn = array_merge($validatorFqn, explode('\\', $validator));
|
2010-09-24 05:32:00 +02:00
|
|
|
$validatorFqn = array_map('ucfirst', $validatorFqn);
|
2010-09-23 21:46:46 +02:00
|
|
|
$validatorFqn = implode('\\', $validatorFqn);
|
|
|
|
$validatorClass = new ReflectionClass($validatorFqn);
|
|
|
|
$implementedInterface = $validatorClass->implementsInterface(
|
|
|
|
'Respect\Validation\Validatable'
|
|
|
|
);
|
|
|
|
if (!$implementedInterface)
|
2010-09-24 03:56:17 +02:00
|
|
|
throw new ComponentException(
|
2010-09-24 04:30:16 +02:00
|
|
|
sprintf('%s does not implement the Respect\Validator\Validatable interface required for validators',
|
|
|
|
$validatorFqn)
|
2010-09-23 21:46:46 +02:00
|
|
|
);
|
|
|
|
if ($validatorClass->hasMethod('__construct')) {
|
|
|
|
$validatorInstance = $validatorClass->newInstanceArgs(
|
|
|
|
$arguments
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$validatorInstance = new $validatorFqn;
|
|
|
|
}
|
2010-09-24 05:32:00 +02:00
|
|
|
return $validatorInstance;
|
2010-09-23 21:46:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|