Ejemplo n.º 1
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.º 2
0
 /**
  * {@inheritdoc}
  */
 public function apply($instance, stdClass $schema, Context $context, Walker $walker)
 {
     if (is_string($schema->type)) {
         if (!Types::isA($instance, $schema->type)) {
             $context->addViolation('instance must be of type %s', [$schema->type]);
         }
     } else {
         if (!Types::isOneOf($instance, $schema->type)) {
             $context->addViolation('instance does not match any of the expected types');
         }
     }
 }
Ejemplo n.º 3
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.º 4
0
 /**
  * Validates an instance against a given schema, populating a context
  * with encountered violations.
  *
  * @param mixed    $instance
  * @param stdClass $schema
  * @param Context  $context
  */
 public function applyConstraints($instance, stdClass $schema, Context $context)
 {
     $cacheKey = gettype($instance) . spl_object_hash($schema);
     $constraints =& $this->constraintsCache[$cacheKey];
     if ($constraints === null) {
         $version = $this->getVersion($schema);
         $instanceType = Types::getPrimitiveTypeOf($instance);
         $constraints = $this->registry->getConstraintsForType($version, $instanceType);
         $constraints = $this->filterConstraintsForSchema($constraints, $schema);
     }
     foreach ($constraints as $constraint) {
         $constraint->apply($instance, $schema, $context, $this);
     }
 }