Skip to content

Commit 1820daf

Browse files
committed
added some docs and a reader class
1 parent e557043 commit 1820daf

File tree

4 files changed

+163
-11
lines changed

4 files changed

+163
-11
lines changed

README.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,81 @@
11
# php-openapi
2-
READ OpenAPI yaml files and make the content accessable in PHP objects.
2+
3+
READ OpenAPI yaml and json files and make the content accessable in PHP objects.
4+
5+
## Install
6+
7+
composer require cebe/php-openapi
8+
9+
## Requirements
10+
11+
- PHP 7.0 or higher
12+
13+
## Usage
14+
15+
Read OpenAPI spec from JSON:
16+
17+
```php
18+
use cebe\openapi\Reader;
19+
20+
$openapi = Reader::readFromJson(file_get_contents('openapi.json'));
21+
```
22+
23+
Read OpenAPI spec from YAML:
24+
25+
```php
26+
use cebe\openapi\Reader;
27+
28+
$openapi = Reader::readFromYaml(file_get_contents('openapi.yaml'));
29+
```
30+
31+
Access specification data:
32+
33+
```php
34+
echo $openapi->openapi; // openAPI version, e.g. 3.0.0
35+
echo $openapi->info->title; // API title
36+
foreach($openapi->paths as $path => $definition) {
37+
// iterate path definitions
38+
}
39+
```
40+
41+
Object properties are exactly like in the [OpenAPI specification](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#openapi-specification).
42+
You may also access additional properties added by specification extensions.
43+
44+
45+
## Completeness
46+
47+
This library is currently work in progress, the following list tracks completeness:
48+
49+
- [ ] read OpenAPI 3.0 JSON
50+
- [ ] read OpenAPI 3.0 YAML
51+
- [ ] OpenAPI 3.0 Schema
52+
- [x] OpenAPI Object
53+
- [x] Info Object
54+
- [x] Contact Object
55+
- [x] License Object
56+
- [x] Server Object
57+
- [x] Server Variable Object
58+
- [ ] Components Object
59+
- [ ] Paths Object
60+
- [ ] Path Item Object
61+
- [ ] Operation Object
62+
- [x] External Documentation Object
63+
- [ ] Parameter Object
64+
- [ ] Request Body Object
65+
- [ ] Media Type Object
66+
- [ ] Encoding Object
67+
- [ ] Responses Object
68+
- [ ] Response Object
69+
- [ ] Callback Object
70+
- [ ] Example Object
71+
- [ ] Link Object
72+
- [ ] Header Object
73+
- [ ] Tag Object
74+
- [ ] Reference Object
75+
- [ ] Schema Object
76+
- [ ] Discriminator Object
77+
- [ ] XML Object
78+
- [ ] Security Scheme Object
79+
- [ ] OAuth Flows Object
80+
- [ ] OAuth Flow Object
81+
- [ ] Security Requirement Object

src/Reader.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace cebe\openapi;
4+
5+
use cebe\openapi\spec\OpenApi;
6+
use cebe\openapi\spec\SpecBaseObject;
7+
use Symfony\Component\Yaml\Yaml;
8+
9+
/**
10+
*
11+
*
12+
* @author Carsten Brandt <mail@cebe.cc>
13+
*/
14+
class Reader
15+
{
16+
public static function readFromJson(string $json, string $baseType = OpenApi::class): SpecBaseObject
17+
{
18+
return new $baseType(json_decode($json, true));
19+
}
20+
21+
public static function readFromYaml(string $yaml, string $baseType = OpenApi::class): SpecBaseObject
22+
{
23+
return new $baseType(Yaml::parse($yaml));
24+
}
25+
}

tests/ReaderTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
*
5+
*
6+
* @author Carsten Brandt <mail@cebe.cc>
7+
*/
8+
class ReaderTest extends \PHPUnit\Framework\TestCase
9+
{
10+
public function testReadJson()
11+
{
12+
$openapi = \cebe\openapi\Reader::readFromJson(<<<JSON
13+
{
14+
"openapi": "3.0.0",
15+
"info": {
16+
"title": "Test API",
17+
"version": "1.0.0"
18+
},
19+
"paths": {
20+
21+
}
22+
}
23+
JSON
24+
);
25+
26+
$this->assertApiContent($openapi);
27+
}
28+
29+
public function testReadYaml()
30+
{
31+
$openapi = \cebe\openapi\Reader::readFromYaml(<<<YAML
32+
openapi: 3.0.0
33+
info:
34+
title: "Test API"
35+
version: "1.0.0"
36+
paths:
37+
/somepath:
38+
YAML
39+
);
40+
41+
$this->assertApiContent($openapi);
42+
}
43+
44+
private function assertApiContent(\cebe\openapi\spec\OpenApi $openapi)
45+
{
46+
$result = $openapi->validate();
47+
$this->assertEquals([], $openapi->getErrors());
48+
$this->assertTrue($result);
49+
50+
51+
$this->assertEquals("3.0.0", $openapi->openapi);
52+
$this->assertEquals("Test API", $openapi->info->title);
53+
$this->assertEquals("1.0.0", $openapi->info->version);
54+
}
55+
56+
}

tests/spec/OpenApiTest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function testReadPetStore()
4141
$this->assertEquals("Swagger Petstore", $openapi->info->title);
4242
// info.license
4343
$this->assertInstanceOf(\cebe\openapi\spec\License::class, $openapi->info->license);
44+
$this->assertEquals("MIT", $openapi->info->license->name);
4445
// info.contact
4546
$this->assertNull($openapi->info->contact);
4647

@@ -50,17 +51,8 @@ public function testReadPetStore()
5051
$this->assertCount(1, $openapi->servers);
5152
foreach ($openapi->servers as $server) {
5253
$this->assertInstanceOf(\cebe\openapi\spec\Server::class, $server);
54+
$this->assertEquals("http://petstore.swagger.io/v1", $server->url);
5355

5456
}
55-
56-
/*
57-
openapi: "3.0.0"
58-
info:
59-
version: 1.0.0
60-
title: Swagger Petstore
61-
license:
62-
name: MIT
63-
servers:
64-
- url: http://petstore.swagger.io/v1*/
6557
}
6658
}

0 commit comments

Comments
 (0)