コード例 #1
0
ファイル: Validator.php プロジェクト: Nycto/Round-Eights
 /**
  * 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;
 }
コード例 #2
0
ファイル: Validator.php プロジェクト: Nycto/Round-Eights
 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());
 }