Esempio n. 1
0
 /**
  * Resolve JSON Schema for the reference
  *
  * @param Reference $reference
  * @param JsonSchema $currentSchema
  *
  * @throws UnsupportedException
  *
  * @return JsonSchema Return the json schema referenced
  */
 protected function resolveSchema(Reference $reference, $currentSchema)
 {
     if ($reference->isInCurrentDocument() && $reference->hasFragment()) {
         return $currentSchema;
     }
     if ($reference->isRelative() && !$currentSchema->getId()) {
         throw new UnsupportedException(sprintf("Reference is relative and no id found in current schema, cannot resolve reference %s", $reference->getReference()));
     }
     // Build url
     $schemaUrl = sprintf('%s://%s:%s', $reference->getScheme() ?: 'http', $reference->getHost(), $reference->getPort() ?: '80');
     if ($reference->isRelative()) {
         $parsedUrl = parse_url($currentSchema->getId());
         $schemaUrl = sprintf('%s://%s:%s', $parsedUrl['scheme'] ?: 'http', $parsedUrl['host'], $parsedUrl['port'] ?: '80');
     }
     if ($reference->getPath()) {
         $schemaUrl = sprintf("%s/%s", $schemaUrl, $reference->getPath());
     }
     if ($reference->getQuery()) {
         $schemaUrl = sprintf("%s?%s", $schemaUrl, $reference->getQuery());
     }
     if (!isset($this->schemaCache[$schemaUrl])) {
         $schema = $this->serializer->deserialize($this->getJsonSchemaContent($schemaUrl), 'Joli\\Jane\\Model\\JsonSchema', 'json');
         $this->schemaCache[$schemaUrl] = $schema;
     }
     return $this->schemaCache[$schemaUrl];
 }
Esempio n. 2
0
 /**
  * Create a new JsonSchema based on two merged schema
  *
  * @param JsonSchema $left
  * @param JsonSchema $right
  *
  * @TODO Handle more fields
  *
  * @return JsonSchema
  */
 public function merge(JsonSchema $left, JsonSchema $right)
 {
     $merged = clone $right;
     if ($left->getType() !== null && $right->getType() !== null && $left->getType() !== $right->getType()) {
         throw new \RuntimeException("Both types are defined and different, merge is not possible");
     }
     if ($right->getType() === null && $left->getType() !== null) {
         $merged->setType($left->getType());
     }
     $merged->setProperties($this->arrayMerge($left->getProperties(), $right->getProperties()));
     $merged->setRequired($this->arrayUnique($this->arrayMerge($left->getRequired(), $right->getRequired())));
     return $merged;
 }