/** * {@inheritdoc} */ public function apply($instance, stdClass $schema, Context $context, Walker $walker) { $hasMatch = false; foreach ($schema->enum as $value) { if (Utils::areEqual($instance, $value)) { $hasMatch = true; break; } } if (!$hasMatch) { $context->addViolation('should match one element in enum'); } }
/** * {@inheritdoc} */ public function apply($instance, stdClass $schema, Context $context, Walker $walker) { if ($schema->uniqueItems === true) { foreach ($instance as $i => $aItem) { foreach ($instance as $j => $bItem) { if ($i !== $j && Utils::areEqual($aItem, $bItem)) { $context->addViolation('elements must be unique'); break 2; } } } } }
/** * Fetches a remote schema and ensures it is valid. * * @param string $uri * * @throws InvalidRemoteSchemaException * @throws JsonDecodeException * * @return stdClass */ private function fetchSchemaAt($uri) { if ($hook = $this->preFetchHook) { $uri = $hook($uri); } set_error_handler(function ($severity, $error) use($uri) { restore_error_handler(); throw new UnfetchableUriException([$uri, $error, $severity]); }); $content = file_get_contents($uri); restore_error_handler(); $schema = json_decode($content); if (json_last_error() !== JSON_ERROR_NONE) { throw new JsonDecodeException(sprintf('Cannot decode JSON from URI "%s" (error: %s)', $uri, Utils::lastJsonErrorMessage())); } if (!is_object($schema)) { throw new InvalidRemoteSchemaException([$uri]); } return $schema; }
/** * {@inheritdoc} */ public function apply($instance, stdClass $schema, Context $context, Walker $walker) { if (!Utils::matchesRegex($instance, $schema->pattern)) { $context->addViolation('should match regex "%s"', [$schema->pattern]); } }
/** * Returns the JSON-decoded content of a file. * * @param string $file * * @return mixed */ protected function loadJsonFromFile($file) { return Utils::loadJsonFromFile($file); }
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(); } }
private function getSchema($uri) { return Utils::loadJsonFromFile($this->uriToFile($uri)); }