respect-validation/library/Respect/Validation/Validator.php

124 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace Respect\Validation;
use Respect\Validation\Rules\All;
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;
class Validator extends All
{
protected $ruleName;
protected $arguments = array();
2010-10-18 07:24:19 +02:00
protected function getRuleName()
{
return $this->ruleName;
}
2010-10-18 07:24:19 +02:00
protected function getArguments()
{
return $this->arguments;
}
2010-10-18 07:24:19 +02:00
protected function setRuleName($ruleName)
{
$this->ruleName = $ruleName;
}
2010-10-18 07:24:19 +02:00
protected function setArguments(array $arguments)
{
$this->arguments = $arguments;
}
2010-10-18 07:24:19 +02:00
protected function addArgument($argument)
{
$this->arguments[] = $argument;
}
public function __get($property)
{
$this->applyParts(func_get_args());
return $this;
}
protected function applyParts($parts)
{
foreach ($parts as $a) {
2010-09-26 08:18:40 +02:00
if (!isset($this->ruleName))
$this->setRuleName($a);
2010-09-26 08:18:40 +02:00
else
$this->addArgument($a);
}
$this->checkForCompleteRule();
}
public function __call($method, $arguments)
{
array_unshift($arguments, $method);
$this->applyParts($arguments);
return $this;
}
protected function checkForCompleteRule()
{
if (!isset($this->ruleName))
return;
$this->addRule(
static::buildRule($this->ruleName, $this->arguments)
);
$this->ruleName = null;
$this->arguments = array();
}
public static function __callStatic($ruleName, $arguments)
{
$validator = new static;
$validator->setRuleName($ruleName);
$validator->setArguments($arguments);
$validator->checkForCompleteRule();
return $validator;
}
public static function buildRule($ruleSpec, $arguments=array())
{
if ($ruleSpec instanceof Validatable) {
return $ruleSpec;
}
if (is_object($ruleSpec))
throw new ComponentException(
sprintf('%s does not implement the Respect\Validator\Validatable interface required for validators',
get_class($ruleSpec))
);
$validatorFqn = explode('\\', get_called_class());
array_pop($validatorFqn);
$validatorFqn[] = 'Rules';
$validatorFqn[] = $ruleSpec;
$validatorFqn = array_map('ucfirst', $validatorFqn);
$validatorFqn = implode('\\', $validatorFqn);
2010-10-18 10:40:44 +02:00
try {
$validatorClass = new ReflectionClass($validatorFqn);
} catch (ReflectionException $e) {
throw new ComponentException($e->getMessage());
}
$implementedInterface = $validatorClass->implementsInterface(
'Respect\Validation\Validatable'
);
if (!$implementedInterface)
throw new ComponentException(
sprintf('%s does not implement the Respect\Validator\Validatable interface required for validators',
$validatorFqn)
);
if ($validatorClass->hasMethod('__construct')) {
$validatorInstance = $validatorClass->newInstanceArgs(
$arguments
);
} else {
$validatorInstance = new $validatorFqn;
}
return $validatorInstance;
}
}