2010-09-23 21:46:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Respect\Validation;
|
|
|
|
|
|
|
|
use ReflectionClass;
|
2010-10-18 10:40:44 +02:00
|
|
|
use ReflectionException;
|
2011-02-07 11:52:18 +01:00
|
|
|
use Respect\Validation\Exceptions\AllOfException;
|
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
use Respect\Validation\Rules\AllOf;
|
2010-09-23 21:46:46 +02:00
|
|
|
|
2011-05-25 12:34:08 +02:00
|
|
|
/**
|
|
|
|
* @method \Respect\Validation\Validator allOf()
|
|
|
|
* @method \Respect\Validation\Validator alnum(string $additionalChars = null)
|
|
|
|
* @method \Respect\Validation\Validator alpha(string $additionalChars = null)
|
|
|
|
* @method \Respect\Validation\Validator arr()
|
|
|
|
* @method \Respect\Validation\Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true)
|
|
|
|
* @method \Respect\Validation\Validator between(int $min = null, int $max = null, bool $inclusive = false)
|
2011-09-17 16:23:15 +02:00
|
|
|
* @method \Respect\Validation\Validator bool()
|
2011-05-25 12:34:08 +02:00
|
|
|
* @method \Respect\Validation\Validator call()
|
|
|
|
* @method \Respect\Validation\Validator callback(mixed $callback)
|
|
|
|
* @method \Respect\Validation\Validator contains(mixed $containsValue, bool $identical = false)
|
2011-09-17 16:23:15 +02:00
|
|
|
* @method \Respect\Validation\Validator cpf()
|
2011-05-25 12:34:08 +02:00
|
|
|
* @method \Respect\Validation\Validator date(string $format = null)
|
|
|
|
* @method \Respect\Validation\Validator digits(string $additionalChars = null)
|
|
|
|
* @method \Respect\Validation\Validator domain()
|
|
|
|
* @method \Respect\Validation\Validator each(Validatable $itemValidator = null, Validatable $keyValidator = null)
|
|
|
|
* @method \Respect\Validation\Validator email()
|
|
|
|
* @method \Respect\Validation\Validator endsWith(mixed $endValue, bool $identical = false)
|
|
|
|
* @method \Respect\Validation\Validator equals(mixed $compareTo, bool $compareIdentical=false)
|
|
|
|
* @method \Respect\Validation\Validator float()
|
|
|
|
* @method \Respect\Validation\Validator hexa()
|
|
|
|
* @method \Respect\Validation\Validator in(array $haystack, bool $compareIdentical = false)
|
|
|
|
* @method \Respect\Validation\Validator instance(string $instanceName)
|
|
|
|
* @method \Respect\Validation\Validator int()
|
|
|
|
* @method \Respect\Validation\Validator ip(array $ipOptions = null)
|
2011-09-17 16:23:15 +02:00
|
|
|
* @method \Respect\Validation\Validator json()
|
2011-05-25 12:34:08 +02:00
|
|
|
* @method \Respect\Validation\Validator key(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
|
|
|
|
* @method \Respect\Validation\Validator length(int $min=null, int $max=null, bool $inclusive = true)
|
|
|
|
* @method \Respect\Validation\Validator max(int $maxValue, bool $inclusive = false)
|
|
|
|
* @method \Respect\Validation\Validator min(int $minValue, bool $inclusive = false)
|
2011-09-14 04:47:59 +02:00
|
|
|
* @method \Respect\Validation\Validator multiple(int $multipleOf)
|
2011-05-25 12:34:08 +02:00
|
|
|
* @method \Respect\Validation\Validator negative()
|
|
|
|
* @method \Respect\Validation\Validator noneOf()
|
|
|
|
* @method \Respect\Validation\Validator not(Validatable $rule)
|
|
|
|
* @method \Respect\Validation\Validator notEmpty()
|
|
|
|
* @method \Respect\Validation\Validator noWhitespace()
|
|
|
|
* @method \Respect\Validation\Validator nullValue()
|
|
|
|
* @method \Respect\Validation\Validator numeric()
|
|
|
|
* @method \Respect\Validation\Validator object()
|
|
|
|
* @method \Respect\Validation\Validator oneOf()
|
|
|
|
* @method \Respect\Validation\Validator positive()
|
|
|
|
* @method \Respect\Validation\Validator regex($regex)
|
|
|
|
* @method \Respect\Validation\Validator sf(string $name, array $params = null)
|
|
|
|
* @method \Respect\Validation\Validator startsWith(mixed $startValue, bool $identical = false)
|
|
|
|
* @method \Respect\Validation\Validator string()
|
|
|
|
* @method \Respect\Validation\Validator tld()
|
2012-04-07 19:49:18 +02:00
|
|
|
* @method \Respect\Validation\Validator version()
|
2011-05-25 12:34:08 +02:00
|
|
|
* @method \Respect\Validation\Validator zend(mixed $validator, array $params = null)
|
|
|
|
*/
|
2010-11-09 04:35:19 +01:00
|
|
|
class Validator extends AllOf
|
2010-09-23 21:46:46 +02:00
|
|
|
{
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
public static function __callStatic($ruleName, $arguments)
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2011-04-27 19:35:12 +02:00
|
|
|
if ('allOf' === $ruleName)
|
|
|
|
return static::buildRule($ruleName, $arguments);
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
$validator = new static;
|
2011-04-27 19:35:12 +02:00
|
|
|
return $validator->__call($ruleName, $arguments);
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
public static function buildRule($ruleSpec, $arguments=array())
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2011-04-04 19:37:40 +02:00
|
|
|
if ($ruleSpec instanceof Validatable)
|
2011-02-07 11:52:18 +01:00
|
|
|
return $ruleSpec;
|
2011-04-04 19:37:40 +02:00
|
|
|
try {
|
2011-04-27 19:35:12 +02:00
|
|
|
$validatorFqn = 'Respect\\Validation\\Rules\\' . ucfirst($ruleSpec);
|
2011-04-04 19:37:40 +02:00
|
|
|
$validatorClass = new ReflectionClass($validatorFqn);
|
|
|
|
$validatorInstance = $validatorClass->newInstanceArgs(
|
|
|
|
$arguments
|
|
|
|
);
|
2011-04-04 18:40:47 +02:00
|
|
|
|
2011-04-04 19:37:40 +02:00
|
|
|
return $validatorInstance;
|
|
|
|
} catch (ReflectionException $e) {
|
|
|
|
throw new ComponentException($e->getMessage());
|
|
|
|
}
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
public function __call($method, $arguments)
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2012-05-17 21:16:45 +02:00
|
|
|
if ('not' === $method)
|
|
|
|
return $arguments ? static::buildRule($method, $arguments) : new Rules\Not($this);
|
2012-07-22 15:17:31 +02:00
|
|
|
|
|
|
|
if (isset($method{4}) &&
|
|
|
|
substr($method, 0, 4) == 'base' && preg_match('@^base([0-9]{1,2})$@', $method, $match))
|
|
|
|
return $this->addRule(static::buildRule('base', array($match[1])));
|
|
|
|
|
|
|
|
|
|
|
|
return $this->addRule(static::buildRule($method, $arguments));
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2011-02-18 04:08:46 +01:00
|
|
|
public function reportError($input, array $extraParams=array())
|
2011-02-07 11:52:18 +01:00
|
|
|
{
|
2011-02-18 04:08:46 +01:00
|
|
|
$exception = new AllOfException;
|
|
|
|
$input = AllOfException::stringify($input);
|
|
|
|
$name = $this->getName() ? : "\"$input\"";
|
2011-12-27 03:43:22 +01:00
|
|
|
$params = array_merge(
|
|
|
|
$extraParams, get_object_vars($this), get_class_vars(__CLASS__)
|
|
|
|
);
|
2011-02-18 04:08:46 +01:00
|
|
|
$exception->configure($name, $params);
|
2011-04-17 17:29:48 +02:00
|
|
|
if (!is_null($this->template))
|
|
|
|
$exception->setTemplate($this->template);
|
2011-02-18 04:08:46 +01:00
|
|
|
return $exception;
|
2011-02-07 11:52:18 +01:00
|
|
|
}
|
|
|
|
|
2011-05-25 12:34:08 +02:00
|
|
|
/**
|
|
|
|
* Create instance validator
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @return \Respect\Validation\Validator
|
|
|
|
*/
|
|
|
|
public static function create() {
|
2011-05-25 12:56:22 +02:00
|
|
|
$ref = new ReflectionClass(__CLASS__);
|
2011-05-25 12:34:08 +02:00
|
|
|
return $ref->newInstanceArgs(func_get_args());
|
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|