2010-09-23 21:46:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Respect\Validation;
|
|
|
|
|
2010-09-26 07:16:02 +02:00
|
|
|
use Respect\Validation\Rules\All;
|
2010-09-23 21:46:46 +02:00
|
|
|
use ReflectionClass;
|
2010-09-27 23:02:30 +02:00
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
2010-10-18 10:40:44 +02:00
|
|
|
use ReflectionException;
|
2010-09-23 21:46:46 +02:00
|
|
|
|
2010-09-25 06:48:49 +02:00
|
|
|
class Validator extends All
|
2010-09-23 21:46:46 +02:00
|
|
|
{
|
|
|
|
|
2010-09-25 06:48:49 +02:00
|
|
|
protected $ruleName;
|
2010-09-25 06:13:26 +02:00
|
|
|
protected $arguments = array();
|
2010-09-23 21:46:46 +02:00
|
|
|
|
2010-10-18 07:24:19 +02:00
|
|
|
protected function getRuleName()
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2010-09-25 06:48:49 +02:00
|
|
|
return $this->ruleName;
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2010-10-18 07:24:19 +02:00
|
|
|
protected function getArguments()
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
|
|
|
return $this->arguments;
|
|
|
|
}
|
|
|
|
|
2010-10-18 07:24:19 +02:00
|
|
|
protected function setRuleName($ruleName)
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2010-09-25 06:48:49 +02:00
|
|
|
$this->ruleName = $ruleName;
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2010-10-18 07:24:19 +02:00
|
|
|
protected function setArguments(array $arguments)
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
|
|
|
$this->arguments = $arguments;
|
|
|
|
}
|
|
|
|
|
2010-10-18 07:24:19 +02:00
|
|
|
protected function addArgument($argument)
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
|
|
|
$this->arguments[] = $argument;
|
|
|
|
}
|
|
|
|
|
2010-09-25 06:48:49 +02:00
|
|
|
public function __get($property)
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2010-09-25 06:48:49 +02:00
|
|
|
$this->applyParts(func_get_args());
|
|
|
|
return $this;
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2010-09-25 06:48:49 +02:00
|
|
|
protected function applyParts($parts)
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2010-09-25 06:48:49 +02:00
|
|
|
foreach ($parts as $a) {
|
2010-09-26 08:18:40 +02:00
|
|
|
if (!isset($this->ruleName))
|
2010-09-25 06:48:49 +02:00
|
|
|
$this->setRuleName($a);
|
2010-09-26 08:18:40 +02:00
|
|
|
else
|
|
|
|
$this->addArgument($a);
|
2010-09-24 05:32:00 +02:00
|
|
|
}
|
2010-09-25 06:13:26 +02:00
|
|
|
$this->checkForCompleteRule();
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2010-09-25 06:48:49 +02:00
|
|
|
public function __call($method, $arguments)
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2010-09-25 06:48:49 +02:00
|
|
|
array_unshift($arguments, $method);
|
|
|
|
$this->applyParts($arguments);
|
|
|
|
return $this;
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2010-09-25 06:48:49 +02:00
|
|
|
protected function checkForCompleteRule()
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2010-09-26 07:16:02 +02:00
|
|
|
if (!isset($this->ruleName))
|
2010-09-25 06:13:26 +02:00
|
|
|
return;
|
2010-09-25 06:48:49 +02:00
|
|
|
$this->addRule(
|
2010-09-26 07:16:02 +02:00
|
|
|
static::buildRule($this->ruleName, $this->arguments)
|
2010-09-25 06:13:26 +02:00
|
|
|
);
|
2010-09-25 06:48:49 +02:00
|
|
|
$this->ruleName = null;
|
2010-09-25 06:13:26 +02:00
|
|
|
$this->arguments = array();
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2010-09-26 07:16:02 +02:00
|
|
|
public static function __callStatic($ruleName, $arguments)
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2010-09-25 06:13:26 +02:00
|
|
|
$validator = new static;
|
2010-09-25 06:48:49 +02:00
|
|
|
$validator->setRuleName($ruleName);
|
2010-09-25 06:13:26 +02:00
|
|
|
$validator->setArguments($arguments);
|
|
|
|
$validator->checkForCompleteRule();
|
|
|
|
return $validator;
|
2010-09-24 05:32:00 +02:00
|
|
|
}
|
|
|
|
|
2010-09-25 06:13:26 +02:00
|
|
|
public static function buildRule($ruleSpec, $arguments=array())
|
2010-09-23 21:46:46 +02:00
|
|
|
{
|
2010-09-25 06:13:26 +02:00
|
|
|
if ($ruleSpec instanceof Validatable) {
|
|
|
|
return $ruleSpec;
|
2010-09-23 21:46:46 +02:00
|
|
|
}
|
2010-09-25 06:13:26 +02:00
|
|
|
if (is_object($ruleSpec))
|
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',
|
2010-09-25 06:13:26 +02:00
|
|
|
get_class($ruleSpec))
|
2010-09-23 21:46:46 +02:00
|
|
|
);
|
|
|
|
$validatorFqn = explode('\\', get_called_class());
|
|
|
|
array_pop($validatorFqn);
|
2010-09-26 07:16:02 +02:00
|
|
|
$validatorFqn[] = 'Rules';
|
|
|
|
$validatorFqn[] = $ruleSpec;
|
2010-09-24 05:32:00 +02:00
|
|
|
$validatorFqn = array_map('ucfirst', $validatorFqn);
|
2010-09-23 21:46:46 +02:00
|
|
|
$validatorFqn = implode('\\', $validatorFqn);
|
2010-10-18 10:40:44 +02:00
|
|
|
try {
|
|
|
|
$validatorClass = new ReflectionClass($validatorFqn);
|
|
|
|
} catch (ReflectionException $e) {
|
|
|
|
throw new ComponentException($e->getMessage());
|
|
|
|
}
|
2010-09-23 21:46:46 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
}
|