Joli\Jane\OpenApi\SchemaParser\SchemaParser::parseSchema PHP Method

parseSchema() public method

Parse an file into a OpenAPI Schema model
public parseSchema ( string $openApiSpec ) : Joli\Jane\OpenApi\Model\OpenApi
$openApiSpec string
return Joli\Jane\OpenApi\Model\OpenApi
    public function parseSchema($openApiSpec)
    {
        $openApiSpecContents = file_get_contents($openApiSpec);
        $schemaClass = self::OPEN_API_MODEL;
        $schema = null;
        $jsonException = null;
        $yamlException = null;
        try {
            $schema = $this->serializer->deserialize($openApiSpecContents, $schemaClass, self::CONTENT_TYPE_JSON);
        } catch (\Exception $exception) {
            $jsonException = $exception;
        }
        if (!$schema) {
            try {
                $schema = $this->serializer->deserialize($openApiSpecContents, $schemaClass, self::CONTENT_TYPE_YAML);
            } catch (\Exception $exception) {
                $yamlException = $exception;
            }
            if (!$schema) {
                throw new ParseFailureException(sprintf(self::EXCEPTION_MESSAGE, $openApiSpec), 1, $jsonException, $yamlException);
            }
        }
        return $schema;
    }

Usage Example

Example #1
0
 /**
  * Return a list of class guessed
  *
  * @param string $openApiSpec
  * @param string $name
  * @param string $namespace
  * @param string $directory
  *
  * @return Context
  */
 public function createContext($openApiSpec, $name, $namespace, $directory)
 {
     $schema = $this->schemaParser->parseSchema($openApiSpec);
     $classes = $this->chainGuesser->guessClass($schema, $name);
     foreach ($classes as $class) {
         $properties = $this->chainGuesser->guessProperties($class->getObject(), $name, $classes);
         foreach ($properties as $property) {
             $property->setType($this->chainGuesser->guessType($property->getObject(), $property->getName(), $classes));
         }
         $class->setProperties($properties);
     }
     return new Context($schema, $namespace, $directory, $classes);
 }