Skip to content

Commit 0298420

Browse files
committed
feat: generate OpenAPI JSON from APIB
1 parent c95725b commit 0298420

12 files changed

+348
-90
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
/build/out
1212
/build/*.phar
1313
/tests/statics/index.*
14-
src/Michelf/*
1514
src/.gitignore
1615
vendor/**
1716

@@ -24,3 +23,4 @@ atlassian-ide-plugin.xml
2423

2524
/coverage.xml
2625
/event.json
26+
!/src/PHPDraft/Out/

phpdraft

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ try
2828
->opt('help:h', 'This help text', false)
2929
->opt('version:v', 'Print the version for PHPDraft.', false)
3030
->opt('file:f', 'Specifies the file to parse.', false)
31+
->opt('openapi:a', 'Output location for an OpenAPI file.', false)
3132
->opt('yes:y', 'Always accept using the online mode.', false, 'bool')
3233
->opt('online:o', 'Always use the online mode.', false, 'bool')
3334
->opt('template:t', 'Specifies the template to use. (defaults to \'default\').', false)
@@ -90,6 +91,11 @@ try
9091
$data = json_decode($json_string);
9192
}
9293

94+
if (isset($args['openapi'])) {
95+
$openapi = ParserFactory::getOpenAPI()->init($data);
96+
$openapi->write($args['openapi']);
97+
}
98+
9399
$html = ParserFactory::getJson()->init($data);
94100
$name = 'PHPD_SORT_' . strtoupper($args->getOpt('sort', ''));
95101
$html->sorting = Sorting::${$name} ?? Sorting::PHPD_SORT_NONE->value;

src/PHPDraft/Model/HTTPRequest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class HTTPRequest implements Comparable
4343
*
4444
* @var string
4545
*/
46-
public string $description;
46+
public string $description = '';
4747

4848
/**
4949
* Parent class.

src/PHPDraft/Model/Transition.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class Transition extends HierarchyElement
2323
/**
2424
* HTTP method used.
2525
*
26-
* @var string
26+
* @var string|null
2727
*/
28-
public string $method;
28+
public ?string $method = NULL;
2929

3030
/**
3131
* URI.

src/PHPDraft/Out/BaseTemplateRenderer.php

+42-22
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,21 @@ abstract class BaseTemplateRenderer
2424
* @var int
2525
*/
2626
public int $sorting;
27+
2728
/**
28-
* CSS Files to load.
29-
*
30-
* @var string[]
31-
*/
32-
public array $css = [];
33-
/**
34-
* JS Files to load.
35-
*
36-
* @var string[]
37-
*/
38-
public array $js = [];
39-
/**
40-
* The image to use as a logo.
41-
*
42-
* @var string|null
43-
*/
44-
protected ?string $image = null;
45-
/**
46-
* The template file to load.
29+
* JSON representation of an API Blueprint.
4730
*
48-
* @var string
31+
* @var object
4932
*/
50-
protected string $template;
33+
protected object $object;
34+
5135
/**
5236
* The base data of the API.
5337
*
5438
* @var array<string, mixed>
5539
*/
56-
protected array $base_data;
40+
protected array $base_data = [];
41+
5742
/**
5843
* JSON object of the API blueprint.
5944
*
@@ -66,4 +51,39 @@ abstract class BaseTemplateRenderer
6651
* @var ObjectStructureElement[]
6752
*/
6853
protected array $base_structures = [];
54+
55+
/**
56+
* Parse base data
57+
*
58+
* @param object $object
59+
*/
60+
protected function parse_base_data(object $object): void
61+
{
62+
//Prepare base data
63+
if (!is_array($object->content[0]->content)) {
64+
return;
65+
}
66+
67+
$this->base_data['TITLE'] = $object->content[0]->meta->title->content ?? '';
68+
69+
foreach ($object->content[0]->attributes->metadata->content as $meta) {
70+
$this->base_data[$meta->content->key->content] = $meta->content->value->content;
71+
}
72+
73+
foreach ($object->content[0]->content as $value) {
74+
if ($value->element === 'copy') {
75+
$this->base_data['DESC'] = $value->content;
76+
continue;
77+
}
78+
79+
$cat = new Category();
80+
$cat = $cat->parse($value);
81+
82+
if (($value->meta->classes->content[0]->content ?? null) === 'dataStructures') {
83+
$this->base_structures = array_merge($this->base_structures, $cat->structures);
84+
} else {
85+
$this->categories[] = $cat;
86+
}
87+
}
88+
}
6989
}

src/PHPDraft/Out/TemplateRenderer.php src/PHPDraft/Out/HtmlTemplateRenderer.php

+29-36
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,36 @@
2828
use Twig\TwigFilter;
2929
use Twig\TwigTest;
3030

31-
class TemplateRenderer extends BaseTemplateRenderer
31+
class HtmlTemplateRenderer extends BaseTemplateRenderer
3232
{
33+
34+
35+
/**
36+
* CSS Files to load.
37+
*
38+
* @var string[]
39+
*/
40+
public array $css = [];
41+
42+
/**
43+
* JS Files to load.
44+
*
45+
* @var string[]
46+
*/
47+
public array $js = [];
48+
/**
49+
* The image to use as a logo.
50+
*
51+
* @var string|null
52+
*/
53+
protected ?string $image = null;
54+
/**
55+
* The template file to load.
56+
*
57+
* @var string
58+
*/
59+
protected string $template;
60+
3361
/**
3462
* TemplateGenerator constructor.
3563
*
@@ -106,41 +134,6 @@ public function get(object $object): string
106134
]);
107135
}
108136

109-
/**
110-
* Parse base data
111-
*
112-
* @param object $object
113-
*/
114-
private function parse_base_data(object $object): void
115-
{
116-
//Prepare base data
117-
if (!is_array($object->content[0]->content)) {
118-
return;
119-
}
120-
121-
$this->base_data['TITLE'] = $object->content[0]->meta->title->content ?? '';
122-
123-
foreach ($object->content[0]->attributes->metadata->content as $meta) {
124-
$this->base_data[$meta->content->key->content] = $meta->content->value->content;
125-
}
126-
127-
foreach ($object->content[0]->content as $value) {
128-
if ($value->element === 'copy') {
129-
$this->base_data['DESC'] = $value->content;
130-
continue;
131-
}
132-
133-
$cat = new Category();
134-
$cat = $cat->parse($value);
135-
136-
if (($value->meta->classes->content[0]->content ?? null) === 'dataStructures') {
137-
$this->base_structures = array_merge($this->base_structures, $cat->structures);
138-
} else {
139-
$this->categories[] = $cat;
140-
}
141-
}
142-
}
143-
144137
/**
145138
* Get the path to a file to include.
146139
*

0 commit comments

Comments
 (0)