Skip to content

Commit 10034bd

Browse files
committed
feat: generate OpenAPI JSON from APIB
1 parent d5adafa commit 10034bd

11 files changed

+88
-285
lines changed

.gitignore

-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

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

-231
This file was deleted.

0 commit comments

Comments
 (0)