/**
  * {@inheritDoc}
  */
 public function check($value = null, $schema = null, $path = null, $i = null)
 {
     $type = isset($schema->type) ? $schema->type : null;
     $isValid = true;
     if (is_array($type)) {
         // @TODO refactor
         $validatedOneType = false;
         $errors = array();
         foreach ($type as $tp) {
             $validator = new Type($this->checkMode);
             $subSchema = new \stdClass();
             $subSchema->type = $tp;
             $validator->check($value, $subSchema, $path, null);
             $error = $validator->getErrors();
             if (!count($error)) {
                 $validatedOneType = true;
                 break;
             }
             $errors = $error;
         }
         if (!$validatedOneType) {
             return $this->addErrors($errors);
         }
     } elseif (is_object($type)) {
         $this->checkUndefined($value, $type, $path);
     } else {
         $isValid = $this->validateType($value, $type);
     }
     if ($isValid === false) {
         if (!isset(self::$wording[$type])) {
             throw new StandardUnexpectedValueException(sprintf("No wording for %s available, expected wordings are: [%s]", var_export($type, true), implode(', ', array_filter(self::$wording))));
         }
         $this->addError($path, gettype($value) . " value found, but " . self::$wording[$type] . " is required");
     }
 }
Exemple #2
0
 /**
  * {@inheritDoc}
  */
 public function check($value = null, $schema = null, $path = null, $i = null)
 {
     $type = isset($schema->type) ? $schema->type : null;
     $isValid = true;
     if (is_array($type)) {
         // @TODO refactor
         $validatedOneType = false;
         $errors = array();
         foreach ($type as $tp) {
             $validator = new Type($this->checkMode);
             $subSchema = new \stdClass();
             $subSchema->type = $tp;
             $validator->check($value, $subSchema, $path, null);
             $error = $validator->getErrors();
             if (!count($error)) {
                 $validatedOneType = true;
                 break;
             }
             $errors = $error;
         }
         if (!$validatedOneType) {
             return $this->addErrors($errors);
         }
     } elseif (is_object($type)) {
         $this->checkUndefined($value, $type, $path);
     } else {
         $isValid = $this->validateType($value, $type);
     }
     if ($isValid === false) {
         $this->addError($path, gettype($value) . " value found, but a " . $type . " is required");
     }
 }
 /**
  * Helper to assert an error message
  *
  * @param string $expected
  * @param Type $actual
  */
 private function assertTypeConstraintError($expected, Type $actual)
 {
     $actualErrors = $actual->getErrors();
     $this->assertCount(1, $actualErrors, "Failed to assert that Type has exactly one error to assert the error message against.");
     $actualError = $actualErrors[0];
     $this->assertInternalType('array', $actualError, sprintf('Failed to assert that Type error is an array, %s given', gettype($actualError)));
     $messageKey = 'message';
     $this->assertArrayHasKey($messageKey, $actualError, sprintf('Failed to assert that Type error has a message key %s.', var_export($messageKey, true)));
     $actualMessage = $actualError[$messageKey];
     $this->assertEquals($expected, $actualMessage);
     // first equal for the diff
     $this->assertSame($expected, $actualMessage);
     // the same for the strictness
 }
 /**
  * Validates the type of a property
  *
  * @param mixed $value
  * @param mixed $schema
  * @param mixed $path
  * @param mixed $i
  */
 protected function checkType($value, $schema = null, $path = null, $i = null)
 {
     $validator = new Type($this->checkMode, $this->uriRetriever);
     $validator->check($value, $schema, $path, $i);
     $this->addErrors($validator->getErrors());
 }