11# php-openapi
22
3- READ [ OpenAPI] ( https://www.openapis.org/ ) 3.0.x YAML and JSON files and make the content accessible in PHP objects.
3+ Read and write [ OpenAPI] ( https://www.openapis.org/ ) 3.0.x YAML and JSON files and make the content accessible in PHP objects.
4+
5+ It also provides a CLI tool for validating and converting OpenAPI 3.0.x YAML and JSON files.
46
57[ ![ Latest Stable Version] ( https://poser.pugx.org/cebe/php-openapi/v/stable )] ( https://packagist.org/packages/cebe/php-openapi )
68[ ![ Build Status] ( https://travis-ci.org/cebe/php-openapi.svg?branch=master )] ( https://travis-ci.org/cebe/php-openapi )
@@ -17,31 +19,77 @@ READ [OpenAPI](https://www.openapis.org/) 3.0.x YAML and JSON files and make the
1719
1820## Used by
1921
20- This library provides a low level API for reading OpenAPI files. It is used by higher level tools to
22+ This library provides a low level API for reading and writing OpenAPI files. It is used by higher level tools to
2123do awesome work:
2224
23- - https://github.com/cebe/yii2-openapi Code Generator for REST API from OpenAPI spec
25+ - https://github.com/cebe/yii2-openapi Code Generator for REST API from OpenAPI spec, includes fake data generator.
2426- https://github.com/cebe/yii2-app-api Yii framework application template for developing API-first applications
2527- ... ([ add yours] ( https://github.com/cebe/php-openapi/edit/master/README.md#L24 ) )
2628
2729## Usage
2830
31+ ### CLI tool
32+
33+ $ vendor/bin/php-openapi help
34+ PHP OpenAPI 3 tool
35+ ------------------
36+ by Carsten Brandt <[email protected] > 37+
38+ Usage:
39+ php-openapi <command> [<options>] [input.yml|input.json] [output.yml|output.json]
40+
41+ The following commands are available:
42+
43+ validate Validate the API description in the specified input file against the OpenAPI v3.0 schema.
44+ Note: the validation is performed in two steps. The results is composed of
45+ (1) structural errors found while reading the API description file, and
46+ (2) violations of the OpenAPI v3.0 schema.
47+
48+ If no input file is specified input will be read from STDIN.
49+ The tool will try to auto-detect the content type of the input, but may fail
50+ to do so, you may specify --read-yaml or --read-json to force the file type.
51+
52+ Exits with code 2 on validation errors, 1 on other errors and 0 on success.
53+
54+ convert Convert a JSON or YAML input file to JSON or YAML output file.
55+ References are being resolved so the output will be a single specification file.
56+
57+ If no input file is specified input will be read from STDIN.
58+ If no output file is specified output will be written to STDOUT.
59+ The tool will try to auto-detect the content type of the input and output file, but may fail
60+ to do so, you may specify --read-yaml or --read-json to force the input file type.
61+ and --write-yaml or --write-json to force the output file type.
62+
63+ help Shows this usage information.
64+
65+ Options:
66+
67+ --read-json force reading input as JSON. Auto-detect if not specified.
68+ --read-yaml force reading input as YAML. Auto-detect if not specified.
69+ --write-json force writing output as JSON. Auto-detect if not specified.
70+ --write-yaml force writing output as YAML. Auto-detect if not specified.
71+
72+
2973### Reading Specification information
3074
31- Read OpenAPI spec from JSON:
75+ Read OpenAPI spec from JSON file :
3276
3377``` php
3478use cebe\openapi\Reader;
3579
36- $openapi = Reader::readFromJson(file_get_contents('openapi.json'));
80+ // realpath is needed for resolving references with relative Paths or URLs
81+ $openapi = Reader::readFromJsonFile(realpath('openapi.json'));
3782```
3883
3984Read OpenAPI spec from YAML:
4085
4186``` php
4287use cebe\openapi\Reader;
4388
44- $openapi = Reader::readFromYaml(file_get_contents('openapi.yaml'));
89+ // realpath is needed for resolving references with relative Paths or URLs
90+ $openapi = Reader::readFromYamlFile(realpath('openapi.json'));
91+ // you may also specify the URL to your description file
92+ $openapi = Reader::readFromYamlFile('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/3.0.2/examples/v3.0/petstore-expanded.yaml');
4593```
4694
4795Access specification data:
@@ -57,6 +105,44 @@ foreach($openapi->paths as $path => $definition) {
57105Object properties are exactly like in the [ OpenAPI specification] ( https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#openapi-specification ) .
58106You may also access additional properties added by specification extensions.
59107
108+ ### Writing Specification files
109+
110+ ``` php
111+ // create base description
112+ $openapi = new \cebe\openapi\spec\OpenApi([
113+ 'openapi' => '3.0.2',
114+ 'info' => [
115+ 'title' => 'Test API',
116+ 'version' => '1.0.0',
117+ ],
118+ 'paths' => [],
119+ ]);
120+ // manipulate description as needed
121+ $openapi->paths['/test'] = new \cebe\openapi\spec\PathItem([
122+ 'description' => 'something'
123+ ]);
124+ // ...
125+
126+ $json = \cebe\openapi\Writer::writeToJson($openapi);
127+ ```
128+
129+ results in the following JSON data:
130+
131+ ``` json
132+ {
133+ "openapi" : " 3.0.0" ,
134+ "info" : {
135+ "title" : " Test API" ,
136+ "version" : " 1.0.0"
137+ },
138+ "paths" : {
139+ "/test" : {
140+ "description" : " something"
141+ }
142+ }
143+ }
144+ ```
145+
60146### Reading Specification Files and Resolving References
61147
62148In the above we have passed the raw JSON or YAML data to the Reader. In order to be able to resolve
@@ -82,6 +168,8 @@ $openapi->resolveReferences(
82168### Validation
83169
84170The library provides simple validation operations, that check basic OpenAPI spec requirements.
171+ This is the same as "structural errors found while reading the API description file" from the CLI tool.
172+ This validation does not include checking against the OpenAPI v3.0 JSON schema.
85173
86174```
87175// return `true` in case no errors have been found, `false` in case of errors.
0 commit comments