Esempio n. 1
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 TypeConstraint($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");
     }
 }
Esempio n. 2
0
 /**
  * Helper to assert an error message
  *
  * @param string $expected
  * @param TypeConstraint $actual
  */
 private function assertTypeConstraintError($expected, TypeConstraint $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
 }
 protected function validateType($value, $type)
 {
     if ('array' === $type) {
         return is_array($value) || $value instanceof \ArrayAccess;
     }
     return parent::validateType($value, $type);
 }
Esempio n. 4
0
 /**
  * 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 TypeConstraint($this->checkMode, $this->uriRetriever, $this->uriResolver, $this->schemaId);
     $validator->check($value, $schema, $path, $i);
     $this->addErrors($validator->getErrors());
 }