Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     $context->enterNode('type');
     if (is_string($schema->type)) {
         if (!Types::isPrimitive($schema->type)) {
             throw new NotPrimitiveTypeException($context);
         }
     } elseif (is_array($schema->type)) {
         foreach ($schema->type as $index => $type) {
             $context->enterNode($index);
             if (!is_string($type)) {
                 throw new InvalidTypeException($context, Types::TYPE_STRING);
             }
             if (!Types::isPrimitive($type)) {
                 throw new NotPrimitiveTypeException($context);
             }
             $context->leaveNode();
         }
         if (count(array_unique($schema->type)) !== count($schema->type)) {
             throw new NotUniqueException($context);
         }
     } else {
         throw new InvalidTypeException($context, [Types::TYPE_STRING, Types::TYPE_ARRAY]);
     }
     $context->leaveNode();
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     $context->enterNode('dependencies');
     if (!is_object($schema->dependencies)) {
         throw new InvalidTypeException($context, Types::TYPE_OBJECT);
     }
     foreach ($schema->dependencies as $property => $value) {
         $context->enterNode($property);
         if (is_object($value)) {
             $walker->parseSchema($value, $context);
         } elseif (is_array($value)) {
             if (0 === ($propertyCount = count($value))) {
                 throw new EmptyArrayException($context);
             }
             foreach ($value as $index => $subProperty) {
                 if (!is_string($subProperty)) {
                     $context->enterNode($index);
                     throw new InvalidTypeException($context, Types::TYPE_STRING);
                 }
             }
             if (count(array_unique($value)) !== $propertyCount) {
                 throw new NotUniqueException($context);
             }
         } else {
             throw new InvalidTypeException($context, [Types::TYPE_OBJECT, Types::TYPE_ARRAY]);
         }
         $context->leaveNode();
     }
     $context->leaveNode();
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function apply($instance, stdClass $schema, Context $context, Walker $walker)
 {
     if (is_object($schema->items)) {
         // 8.2.3.1. If items is a schema, then the child instance must be
         // valid against this schema, regardless of its index, and
         // regardless of the value of "additionalItems".
         foreach ($instance as $index => $item) {
             $context->enterNode($index);
             $walker->applyConstraints($item, $schema->items, $context);
             $context->leaveNode();
         }
     } else {
         // "items" is an array
         $itemSize = count($schema->items);
         foreach ($instance as $index => $item) {
             $context->enterNode($index);
             // 8.2.3.2.  If the index is less than, or equal to, the size of
             // "items", the child instance must be valid against the
             // corresponding schema in the "items" array; otherwise, it must
             // be valid against the schema defined by "additionalItems".
             //
             // NOTE: this is adapted for 0-based indexation.
             if ($index < $itemSize) {
                 $walker->applyConstraints($item, $schema->items[$index], $context);
             } elseif ($schema->additionalItems === false) {
                 $context->addViolation('additional items are not allowed');
             } else {
                 $walker->applyConstraints($item, $schema->additionalItems, $context);
             }
             $context->leaveNode();
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     if (!is_bool($schema->uniqueItems)) {
         $context->enterNode('uniqueItems');
         throw new InvalidTypeException($context, Types::TYPE_BOOLEAN);
     }
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     if (!is_string($schema->format)) {
         $context->enterNode('format');
         throw new InvalidTypeException($context, Types::TYPE_STRING);
     }
     // TODO: add option to treat unknown format as a schema error
 }
Ejemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     $context->enterNode('not');
     if (!is_object($schema->not)) {
         throw new InvalidTypeException($context, Types::TYPE_OBJECT);
     }
     $walker->parseSchema($schema->not, $context);
     $context->leaveNode();
 }
Ejemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     $property = $this->keywords()[0];
     $secondaryProperty = $this->keywords()[1];
     if (!property_exists($schema, $property)) {
         throw new MissingKeywordException($context, $property);
     }
     if (!property_exists($schema, $secondaryProperty)) {
         $schema->{$secondaryProperty} = false;
     }
     if (!Types::isA($schema->{$property}, Types::TYPE_NUMBER)) {
         $context->enterNode($property);
         throw new InvalidTypeException($context, Types::TYPE_NUMBER);
     }
     if (!is_bool($schema->{$secondaryProperty})) {
         $context->enterNode($secondaryProperty);
         throw new InvalidTypeException($context, Types::TYPE_BOOLEAN);
     }
 }
Ejemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     $context->enterNode('required');
     if (!is_array($schema->required)) {
         throw new InvalidTypeException($context, Types::TYPE_ARRAY);
     }
     if (0 === ($requiredCount = count($schema->required))) {
         throw new EmptyArrayException($context);
     }
     foreach ($schema->required as $index => $property) {
         if (!is_string($property)) {
             $context->enterNode($index);
             throw new InvalidTypeException($context, Types::TYPE_STRING);
         }
     }
     if ($requiredCount !== count(array_unique($schema->required))) {
         throw new NotUniqueException($context);
     }
     $context->leaveNode();
 }
Ejemplo n.º 9
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     $context->enterNode('pattern');
     if (!is_string($schema->pattern)) {
         throw new InvalidTypeException($context, Types::TYPE_STRING);
     }
     if (!Utils::isValidRegex($schema->pattern)) {
         throw new InvalidRegexException($context);
     }
     $context->leaveNode();
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     $context->enterNode('multipleOf');
     if (!Types::isA($schema->multipleOf, Types::TYPE_NUMBER)) {
         throw new InvalidTypeException($context, Types::TYPE_NUMBER);
     }
     if ($schema->multipleOf <= 0) {
         throw new NotStrictlyPositiveException($context);
     }
     $context->leaveNode();
 }
Ejemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     $keyword = $this->keywords()[0];
     $context->enterNode($keyword);
     if (!is_array($schema->{$keyword})) {
         throw new InvalidTypeException($context, Types::TYPE_ARRAY);
     }
     if (count($schema->{$keyword}) === 0) {
         throw new EmptyArrayException($context);
     }
     foreach ($schema->{$keyword} as $index => $subSchema) {
         $context->enterNode($index);
         if (!is_object($subSchema)) {
             throw new InvalidTypeException($context, Types::TYPE_OBJECT);
         }
         $walker->parseSchema($subSchema, $context);
         $context->leaveNode();
     }
     $context->leaveNode();
 }
Ejemplo n.º 12
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     $keyword = $this->keywords()[0];
     $context->enterNode($keyword);
     if (!is_int($schema->{$keyword})) {
         throw new InvalidTypeException($context, Types::TYPE_INTEGER);
     }
     if ($schema->{$keyword} < 0) {
         throw new LessThanZeroException($context);
     }
     $context->leaveNode();
 }
Ejemplo n.º 13
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     $context->enterNode('enum');
     if (!is_array($schema->enum)) {
         throw new InvalidTypeException($context, Types::TYPE_ARRAY);
     }
     if (count($schema->enum) === 0) {
         throw new EmptyArrayException($context);
     }
     foreach ($schema->enum as $i => $aItem) {
         foreach ($schema->enum as $j => $bItem) {
             if ($i !== $j && Utils::areEqual($aItem, $bItem)) {
                 throw new NotUniqueException($context);
             }
         }
     }
     $context->leaveNode();
 }
Ejemplo n.º 14
0
 private function parsePatternPropertiesProperty(stdClass $schema, Context $context, Walker $walker)
 {
     if (!is_object($schema->patternProperties)) {
         throw new InvalidTypeException($context, Types::TYPE_OBJECT);
     }
     foreach ($schema->patternProperties as $regex => $value) {
         $context->enterNode($regex);
         if (!Utils::isValidRegex($regex)) {
             throw new InvalidRegexException($context);
         }
         if (!is_object($value)) {
             throw new InvalidTypeException($context, Types::TYPE_OBJECT);
         }
         $walker->parseSchema($value, $context);
         $context->leaveNode();
     }
 }