Permalink
Browse files

Cleanup on Validator class (removed some old stuff that i don't even …

…know how ended up there), fixed findMessages and findRelated quirks
  • Loading branch information...
alganet committed Apr 27, 2011
1 parent d254d46 commit 63cc857801521082b5d8a0fc3abd340499ea7b8e
@@ -25,7 +25,7 @@ public function findMessages()
foreach (func_get_args() as $finder) {
$e = call_user_func_array(
array($this, 'findRelated'), explode('.', $finder)
array($this, 'findRelated'), explode('.', $finder)
);
$finder = str_replace('.', '_', $finder);
$messages[$finder] = $e ? $e->getMainMessage() : '';
@@ -39,7 +39,7 @@ public function findRelated()
$path = func_get_args();
while (!empty($path) && $target !== false)
$target = $this->getRelatedByName(array_shift($path));
$target = $target->getRelatedByName(array_shift($path));
return $target;
}
@@ -22,6 +22,8 @@ public function addRule($validator, $arguments=array())
$this->appendRule(Validator::buildRule($validator, $arguments));
else
$this->appendRule($validator);
return $this;
}
public function removeRules()
@@ -40,6 +42,8 @@ public function addRules(array $validators)
$this->addRule($key, $spec);
else
$this->addRule($spec);
return $this;
}
public function getRules()
@@ -11,25 +11,21 @@
class Validator extends AllOf
{
protected $arguments = array();
protected $ruleName;
public static function __callStatic($ruleName, $arguments)
{
if ('allOf' === $ruleName)
return static::buildRule($ruleName, $arguments);
$validator = new static;
$validator->setRuleName($ruleName);
$validator->setArguments($arguments);
$validator->checkForCompleteRule();
return $validator;
return $validator->__call($ruleName, $arguments);
}
public static function buildRule($ruleSpec, $arguments=array())
{
if ($ruleSpec instanceof Validatable)
return $ruleSpec;
try {
$validatorFqn = static::getRuleClassname($ruleSpec);
$validatorFqn = 'Respect\\Validation\\Rules\\' . ucfirst($ruleSpec);
$validatorClass = new ReflectionClass($validatorFqn);
$validatorInstance = $validatorClass->newInstanceArgs(
$arguments
@@ -43,8 +39,7 @@ public static function buildRule($ruleSpec, $arguments=array())
public function __call($method, $arguments)
{
array_unshift($arguments, $method);
$this->applyParts($arguments);
$this->addRule(static::buildRule($method, $arguments));
return $this;
}
@@ -60,48 +55,6 @@ public function reportError($input, array $extraParams=array())
return $exception;
}
protected static function getRuleClassname($ruleName)
{
return 'Respect\\Validation\\Rules\\' . ucfirst($ruleName);
}
protected function addArgument($argument)
{
$this->arguments[] = $argument;
}
protected function applyParts($parts)
{
foreach ($parts as $a) {
if (!isset($this->ruleName))
$this->setRuleName($a);
else
$this->addArgument($a);
}
$this->checkForCompleteRule();
}
protected function checkForCompleteRule()
{
if (!isset($this->ruleName))
return;
$this->addRule(
static::buildRule($this->ruleName, $this->arguments)
);
$this->ruleName = null;
$this->arguments = array();
}
protected function setArguments(array $arguments)
{
$this->arguments = $arguments;
}
protected function setRuleName($ruleName)
{
$this->ruleName = $ruleName;
}
}
/**
@@ -249,13 +249,13 @@ public function testReadme()
v::numeric()->validate($number); //true
//From 1 to 15 non-whitespace alphanumeric characters
$validUsername = v::alnum()
->noWhitespace()
->length(1, 15);
->noWhitespace()
->length(1, 15);
$validUsername->validate('alganet'); //true
$validUser = v::attribute('username', $validUsername) //reusing!
->attribute('birthdate', v::date('Y-m-d'))
->attribute('birthdat', v::date('Y-m-d'));
->attribute('birthdate', v::date('Y-m-d'))
->attribute('birthdat', v::date('Y-m-d'));
$user = new \stdClass;
$user->username = 'alganet';
@@ -276,12 +276,12 @@ public function testReadme()
}
$validBlogPost = v::allOf(v::allOf(v::object()
->attribute('title', v::string()->length(1, 32))
->attribute('author', $validUser) //reuse!
->attribute('date', v::date())
->attribute('text', v::string())))
->setName('Blog Post')
->setTemplate("Not nice {{name}}");
->attribute('title', v::string()->length(1, 32))
->attribute('author', $validUser) //reuse!
->attribute('date', v::date())
->attribute('text', v::string())))
->setName('Blog Post')
->setTemplate("Not nice {{name}}");
$blogPost = new \stdClass;
$blogPost->author = clone $user;

0 comments on commit 63cc857

Please sign in to comment.