mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 17:50:13 +02:00
Better Rules\Not() implementation. Works on composite rules now.
This commit is contained in:
parent
cc278caffd
commit
9b6da3e3e2
|
@ -10,7 +10,7 @@ class PositiveException extends ValidationException
|
|||
self::STANDARD => '{{name}} must be positive',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not MODE_be positive',
|
||||
self::STANDARD => '{{name}} must not be positive',
|
||||
)
|
||||
);
|
||||
|
||||
|
|
|
@ -24,6 +24,11 @@ abstract class AbstractComposite extends AbstractRule implements Validatable
|
|||
$this->appendRule($validator);
|
||||
}
|
||||
|
||||
public function removeRules()
|
||||
{
|
||||
$this->rules = array();
|
||||
}
|
||||
|
||||
public function addRules(array $validators)
|
||||
{
|
||||
foreach ($validators as $key => $spec)
|
||||
|
@ -46,10 +51,10 @@ abstract class AbstractComposite extends AbstractRule implements Validatable
|
|||
{
|
||||
if (empty($this->rules))
|
||||
return false;
|
||||
|
||||
|
||||
if ($validator instanceof Valitatable)
|
||||
return isset($this->rules[spl_object_hash($validator)]);
|
||||
|
||||
|
||||
foreach ($this->rules as $rule)
|
||||
if ($rule instanceof $validator)
|
||||
return true;
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
|
||||
class Not extends AbstractRule
|
||||
|
@ -12,26 +13,50 @@ class Not extends AbstractRule
|
|||
|
||||
public function __construct(Validatable $rule)
|
||||
{
|
||||
if ($rule instanceof AbstractComposite)
|
||||
$rule = $this->absorbComposite($rule);
|
||||
|
||||
$this->rule = $rule;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($rule instanceof AbstractComposite)
|
||||
return $this->rule->validate($input);
|
||||
|
||||
return!$this->rule->validate($input);
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if ($this->rule instanceof AbstractComposite)
|
||||
return $this->rule->assert($input);
|
||||
|
||||
try {
|
||||
$this->rule->assert($input);
|
||||
} catch (ValidationException $e) {
|
||||
return true;
|
||||
}
|
||||
|
||||
throw $this->rule
|
||||
->reportError($input)
|
||||
->setMode(ValidationException::MODE_NEGATIVE);
|
||||
}
|
||||
|
||||
protected function absorbComposite(AbstractComposite $rule)
|
||||
{
|
||||
$rules = $rule->getRules();
|
||||
$rule->removeRules();
|
||||
|
||||
foreach ($rules as &$r)
|
||||
if ($r instanceof AbstractComposite)
|
||||
$rule->addRule($this->absorbComposite($r));
|
||||
else
|
||||
$rule->addRule(new static($r));
|
||||
|
||||
return $rule;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -137,15 +137,6 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
v::notEmpty()->assert('aaa');
|
||||
}
|
||||
|
||||
public function testNot()
|
||||
{
|
||||
try {
|
||||
v::not(v::ip())->assert('192.168.0.1');
|
||||
} catch (\Exception $e) {
|
||||
echo $e->getFullMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function testNullValue()
|
||||
{
|
||||
v::nullValue()->assert(null);
|
||||
|
|
Loading…
Reference in a new issue