* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class JsonSchema implements GeneratorInterface { /** @var string */ private $refBase; public function __construct(string $refBase = '#/definitions/') { $this->refBase = $refBase; } public function generate(SchemaInterface $schema) { $data = $this->toArray( $schema->getType(), $schema->getDefinitions() ); return Parser::encode($data, JSON_PRETTY_PRINT); } /** * @param \PSX\Schema\TypeInterface $type * @param \PSX\Schema\DefinitionsInterface $definitions * @return array */ public function toArray(TypeInterface $type, DefinitionsInterface $definitions) { $object = $this->generateType($type); $result = [ 'definitions' => $this->generateDefinitions($definitions), ]; $result = array_merge($result, $object); return $result; } protected function generateDefinitions(DefinitionsInterface $definitions) { $result = []; $types = $definitions->getAllTypes(); ksort($types); foreach ($types as $ref => $type) { [$ns, $name] = TypeUtil::split($ref); $result[$name] = $this->generateType($type); } return $result; } protected function generateType(TypeInterface $type) { TypeUtil::normalize($type); if ($type instanceof StructType) { $data = $type->toArray(); if (isset($data['properties'])) { $data['properties'] = array_map(function ($property) { return $this->generateType($property); }, $data['properties']); } if (isset($data['$extends'])) { $extends = $data['$extends']; unset($data['$extends']); [$ns, $name] = TypeUtil::split($extends); return [ 'allOf' => [ ['$ref' => $this->refBase . $name], $data, ] ]; } else { return $data; } } elseif ($type instanceof MapType) { $data = $type->toArray(); if (isset($data['additionalProperties']) && $data['additionalProperties'] instanceof TypeInterface) { $data['additionalProperties'] = $this->generateType($data['additionalProperties']); } return $data; } elseif ($type instanceof ArrayType) { $data = $type->toArray(); if (isset($data['items']) && $data['items'] instanceof TypeInterface) { $data['items'] = $this->generateType($data['items']); } return $data; } elseif ($type instanceof UnionType) { $data = $type->toArray(); if (isset($data['oneOf'])) { $data['oneOf'] = array_map(function($type){ return $this->generateType($type); }, $data['oneOf']); } return $data; } elseif ($type instanceof IntersectionType) { $data = $type->toArray(); if (isset($data['allOf'])) { $data['allOf'] = array_map(function($type){ return $this->generateType($type); }, $data['allOf']); } return $data; } elseif ($type instanceof ReferenceType) { [$ns, $name] = TypeUtil::split($type->getRef()); return [ '$ref' => $this->refBase . $name ]; } elseif ($type instanceof AnyType) { return []; } else { return $type->toArray(); } } } __halt_compiler();----SIGNATURE:----oBizCUCKGEqddcj+d1gwj+KQQBNcDqklONGERSbz9Hk7MsHzTpTpbyTcF3xcLR8lumE6VmtDmzAHLifwiX60k9VWIpPVIJMCHwk/v7c5n0QQf3cwt+xKvPbSrZBpkdzKWjdD6ZqfswE1tjpmfFScM9OtVCaZ5bqJYxLlhE/tu/Vg6gebYp9TiZllLxjVS5JCP8fQU5zqb0zeYl4Gv2C6CJMJm28eVBRU6nkLWdFOpScpq2l+1SajIoaQSqleT/HZrHjM/S6XkfJ5mefqGNDUtm0gwvEG6mhI0ySmogRnA/TyNqEriLu8+l5a7TyM8x6pMu7M/MVZqy8qXLaOg2exXdWrflNnzuDaweJLaL2LRiCVw1eRnzAXiKJRdIPlitbJJwZRRxil3UlJQ25JzXxBULBChHqYwWlxjp72RpM21jhKmfgj2DT/UH8GYEiZ8dde4w23rqJXedWx7tJ0OQkwp7zdE0qZC1Y3xg4x2GZe+loqHAaykJuQL+j4CZ3DOz2fIpHmjIPvJtycPVOS032KiG9SCYn+1lE9s+MNuaMeHLAd0LFtEjkOySl7wpWByHxNZTRFqVHZZMVT3OEzgPIOoHUNUaM2yKuUZ10oi30dUe9SJcATWct/tmmW9gjuR+rEFvDOVDvV0wuAt8YyBgHttq/xcoE+AtQWF4OIXayse7E=----ATTACHMENT:----NDY2Mzg5MzE0NTM3MDcxMSA1NzY5Mjk5NzA2NzQ4NjMyIDQ3ODAzMTg5OTQ0NzIyMDY=