Ejemplo n.º 1
0
 /**
  * Performs the validation and returns the result
  *
  * @param Mixed $value The value to validate
  * @return \r8\Validator\Result
  */
 public function validate($value)
 {
     // Invoke the internal validator
     $result = $this->process($value);
     if ($result instanceof \Traversable) {
         $result = \iterator_to_array($result);
     }
     // Normalize the results if it is an array
     if (\is_array($result)) {
         $result = \r8\ary\flatten($result);
         $result = \r8\ary\stringize($result);
         $result = \r8\ary\compact($result);
     } elseif ($result instanceof \r8\Validator\Result) {
         $result = $result->getErrors();
     } elseif (is_null($result) || is_bool($result) || $result === 0 || $result === 0.0) {
         $result = null;
     } else {
         $result = (string) $result;
     }
     // Boot up the results of the validation process
     $output = new \r8\Validator\Result($value);
     // If the internal validator returned a non-empty value
     // (either an array with values or a non-blank string)
     if (!\r8\isEmpty($result)) {
         // If this validator is hooked up with a set of custom error messages,
         // use them instead of what the result returned
         if ($this->hasErrors()) {
             $output->addErrors($this->getErrors());
         } else {
             $output->addErrors($result);
         }
     }
     return $output;
 }
Ejemplo n.º 2
0
 public function testIsValid()
 {
     $result = new \r8\Validator\Result("Wakka");
     $this->assertTrue($result->isValid());
     $result->addError("Test Error");
     $this->assertFalse($result->isValid());
     $result->clearErrors();
     $this->assertTrue($result->isValid());
 }
Ejemplo n.º 3
0
 public function testResultError()
 {
     $return = new \r8\Validator\Result("To Validate");
     $return->addErrors("First Error", "Second Error");
     $mock = $this->getMockValidator($return);
     $result = $mock->validate("To Validate");
     $this->assertThat($result, $this->isInstanceOf("r8\\Validator\\Result"));
     $this->assertFalse($result->isValid());
     $this->assertEquals(array("First Error", "Second Error"), $result->getErrors());
 }
Ejemplo n.º 4
0
 public function testDuplicateErrors()
 {
     $result1 = new \r8\Validator\Result("example value");
     $result1->addError("This is an Error");
     $valid1 = $this->getMock("r8\\iface\\Validator");
     $valid1->expects($this->once())->method("validate")->with($this->equalTo("example value"))->will($this->returnValue($result1));
     $result2 = new \r8\Validator\Result("example value");
     $result2->addError("This is an Error");
     $valid2 = $this->getMock("r8\\iface\\Validator");
     $valid2->expects($this->once())->method("validate")->with($this->equalTo("example value"))->will($this->returnValue($result2));
     $any = new \r8\Validator\Any($valid1, $valid2);
     $result = $any->validate("example value");
     $this->assertThat($result, $this->isInstanceOf("r8\\Validator\\Result"));
     $this->assertFalse($result->isValid());
     $this->assertEquals(array("This is an Error"), $result->getErrors());
 }
Ejemplo n.º 5
0
 public function testResultObjectResult()
 {
     $valid = new \r8\Validator\Callback(function ($value) {
         $result = new \r8\Validator\Result($value);
         if ($value > 10) {
             $result->addError("Error one")->addError("error two");
         }
         return $result;
     });
     $this->assertTrue($valid->isValid(5));
     $result = $valid->validate(20);
     $this->assertFalse($result->isValid());
     $this->assertEquals(array("Error one", "error two"), $result->getErrors());
 }