Skip to content

Commit 19a6438

Browse files
committed
Fix array and enum parsing
1 parent 697de00 commit 19a6438

21 files changed

+621
-640
lines changed

phpdraft

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use PHPDraft\Parse\ParserFactory;
1919
use PHPDraft\Parse\ResourceException;
2020

2121
define('VERSION', '0');
22+
#define('ID_STATIC', 'SOME_ID');
2223
try
2324
{
2425
// Define the cli options.

src/PHPDraft/Model/Category.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace PHPDraft\Model;
1313

14+
use PHPDraft\Model\Elements\BasicStructureElement;
1415
use PHPDraft\Model\Elements\ObjectStructureElement;
1516
use stdClass;
1617

@@ -54,7 +55,7 @@ public function parse(stdClass $object)
5455
break;
5556
case 'dataStructure':
5657
$deps = [];
57-
$struct = new ObjectStructureElement();
58+
$struct = BasicStructureElement::get_class($item->content);
5859
$struct->deps = $deps;
5960
$struct->parse($item->content, $deps);
6061

src/PHPDraft/Model/Elements/ArrayStructureElement.php

+8-14
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public function parse(?object $object, array &$dependencies): StructureElement
3030

3131
$this->parse_common($object, $dependencies);
3232

33-
if (!isset($object->content->value->content)) {
33+
if (!isset($object->content)) {
3434
$this->value = [];
3535

3636
return $this;
3737
}
3838

39-
foreach ($object->content->value->content as $sub_item) {
39+
foreach ($object->content as $sub_item) {
4040
if (!in_array($sub_item->element, self::DEFAULTS)) {
4141
$dependencies[] = $sub_item->element;
4242
}
@@ -58,28 +58,22 @@ public function parse(?object $object, array &$dependencies): StructureElement
5858
*/
5959
public function __toString(): string
6060
{
61-
$return = '<ul class="list-group mdl-list">';
61+
if (is_string($this->value)) {
62+
$type = $this->get_element_as_html($this->element);
6263

63-
if (!is_array($this->value)) {
64-
return '<span class="example-value pull-right">[ ]</span>';
64+
return '<tr><td>' . $this->key . '</td><td>' . $type . '</td><td>' . $this->description . '</td></tr>';
6565
}
6666

67+
$return = '';
6768
foreach ($this->value as $item) {
6869
$value = key($item);
69-
$key = $item[$value];
70-
$type = (in_array($key, self::DEFAULTS)) ? "<code>$key</code>" : '<a href="#object-' . str_replace(
71-
' ',
72-
'-',
73-
strtolower($key)
74-
) . '">' . $key . '</a>';
70+
$type = $this->get_element_as_html($item[$value]);
7571

7672
$value = empty($value) ? '' : " - <span class=\"example-value pull-right\">$value</span>";
7773
$return .= '<li class="list-group-item mdl-list__item">' . $type . $value . '</li>';
7874
}
7975

80-
$return .= '</ul>';
81-
82-
return $return;
76+
return '<ul class="list-group mdl-list">' . $return . '</ul>';
8377
}
8478

8579
/**

src/PHPDraft/Model/Elements/BasicStructureElement.php

+47-5
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected function parse_common(object $object, array &$dependencies): void
130130

131131
$this->description_as_html();
132132

133-
if (!in_array($this->type, self::DEFAULTS)) {
133+
if (!in_array($this->type, self::DEFAULTS) && $this->type !== NULL) {
134134
$dependencies[] = $this->type;
135135
}
136136
}
@@ -145,6 +145,23 @@ public function description_as_html(): void
145145
$this->description = MarkdownExtra::defaultTransform($this->description);
146146
}
147147

148+
/**
149+
* Represent the element in HTML.
150+
*
151+
* @param string $element Element name
152+
*
153+
* @return string HTML string
154+
*/
155+
protected function get_element_as_html($element): string
156+
{
157+
if (in_array($element, self::DEFAULTS)) {
158+
return '<code>' . $element . '</code>';
159+
}
160+
161+
$link_name = str_replace(' ', '-', strtolower($element));
162+
return '<a class="code" title="' . $element . '" href="#object-' . $link_name . '">' . $element . '</a>';
163+
}
164+
148165
/**
149166
* Get a string representation of the value.
150167
*
@@ -155,17 +172,42 @@ public function description_as_html(): void
155172
public function string_value($flat = FALSE)
156173
{
157174
if (is_array($this->value)) {
158-
$key = rand(0, count($this->value));
159-
if (is_subclass_of($this->value[$key], StructureElement::class) && $flat === FALSE) {
160-
return $this->value[$key]->string_value($flat);
175+
$value_key = rand(0, count($this->value));
176+
if (is_subclass_of($this->value[$value_key], StructureElement::class) && $flat === FALSE) {
177+
return $this->value[$value_key]->string_value($flat);
161178
}
162179

163-
return $this->value[$key];
180+
return $this->value[$value_key];
164181
}
165182

166183
if (is_subclass_of($this->value, BasicStructureElement::class) && $flat === TRUE) {
167184
return is_array($this->value->value) ? array_keys($this->value->value)[0] : $this->value->value;
168185
}
169186
return $this->value;
170187
}
188+
189+
/**
190+
* Get what element to parse with.
191+
*
192+
* @param object $object The object to parse.
193+
*
194+
* @return BasicStructureElement The element to parse to
195+
*/
196+
public static function get_class(object $object): BasicStructureElement
197+
{
198+
switch ($object->element) {
199+
default:
200+
case 'object':
201+
$struct = new ObjectStructureElement();
202+
break;
203+
case 'array':
204+
$struct = new ArrayStructureElement();
205+
break;
206+
case 'enum':
207+
$struct = new EnumStructureElement();
208+
break;
209+
}
210+
211+
return $struct;
212+
}
171213
}

src/PHPDraft/Model/Elements/EnumStructureElement.php

+30-26
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,40 @@ class EnumStructureElement extends BasicStructureElement
2323
*/
2424
public function parse(?object $object, array &$dependencies): StructureElement
2525
{
26-
$this->element = (isset($object->element)) ? $object->element : 'enum';
26+
$this->element = $object->element;
2727

2828
$this->parse_common($object, $dependencies);
2929

30-
$this->key = $this->key ?? $object->content ?? 'UNKNOWN';
31-
$this->type = $this->type ?? $object->element;
30+
$this->key = $this->key ?? $object->content->content ?? NULL;
31+
$this->type = $this->type ?? $object->content->element ?? NULL;
3232

33-
if (!isset($object->content->value->content)) {
33+
if (!isset($object->content) && !isset($object->attributes)) {
3434
$this->value = $this->key;
3535

3636
return $this;
3737
}
3838

39-
$enumerations = $object->content->value->attributes->enumerations->content ?? $object->content->value->content;
40-
foreach ($enumerations as $sub_item) {
39+
if (isset($object->attributes->default)) {
40+
if (!in_array($object->attributes->default->content->element ?? '', self::DEFAULTS)) {
41+
$dependencies[] = $object->attributes->default->content->element;
42+
}
43+
$this->value = $object->attributes->default->content->content;
44+
$this->deps = $dependencies;
45+
46+
return $this;
47+
}
48+
49+
if (isset($object->content)) {
50+
if (!in_array($object->content->element, self::DEFAULTS)) {
51+
$dependencies[] = $object->content->element;
52+
}
53+
$this->value = $object->content->content;
54+
$this->deps = $dependencies;
55+
56+
return $this;
57+
}
58+
59+
foreach ($object->attributes->enumerations->content as $sub_item) {
4160
if (!in_array($sub_item->element, self::DEFAULTS)) {
4261
$dependencies[] = $sub_item->element;
4362
}
@@ -57,36 +76,21 @@ public function parse(?object $object, array &$dependencies): StructureElement
5776
*/
5877
public function __toString(): string
5978
{
60-
$return = '<ul class="list-group mdl-list">';
61-
6279
if (is_string($this->value)) {
63-
$type = (in_array($this->element, self::DEFAULTS)) ? $this->element : '<a href="#object-' . str_replace(
64-
' ',
65-
'-',
66-
strtolower($this->element)
67-
) . '">' . $this->element . '</a>';
80+
$type = $this->get_element_as_html($this->element);
6881

69-
return '<tr><td>' . $this->key . '</td><td><code>' . $type . '</code></td><td>' . $this->description . '</td></tr>';
70-
}
71-
72-
if (!is_array($this->value)) {
73-
return '<span class="example-value pull-right">//list of options</span>';
82+
return '<tr><td>' . $this->key . '</td><td>' . $type . '</td><td>' . $this->description . '</td></tr>';
7483
}
7584

85+
$return = '';
7686
foreach ($this->value as $value => $key) {
77-
$type = (in_array($key, self::DEFAULTS)) ? "<code>$key</code>" : '<a href="#object-' . str_replace(
78-
' ',
79-
'-',
80-
strtolower($key)
81-
) . '">' . $key . '</a>';
87+
$type = $type = $this->get_element_as_html($key);
8288

8389
$item = empty($value) ? '' : " - <span class=\"example-value pull-right\">$value</span>";
8490
$return .= '<li class="list-group-item mdl-list__item">' . $type . $item . '</li>';
8591
}
8692

87-
$return .= '</ul>';
88-
89-
return $return;
93+
return '<ul class="list-group mdl-list">' . $return . '</ul>';
9094
}
9195

9296
/**

src/PHPDraft/Model/Elements/ObjectStructureElement.php

+30-36
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ public function parse(?object $object, array &$dependencies): StructureElement
4242

4343
$this->element = $object->element;
4444
$this->parse_common($object, $dependencies);
45-
if (!isset($object->content) && isset($object->meta)) {
46-
return $this;
47-
}
4845

4946
if (isset($object->content) && is_array($object->content)) {
5047
$this->parse_array_content($object, $dependencies);
@@ -74,25 +71,30 @@ public function parse(?object $object, array &$dependencies): StructureElement
7471
/**
7572
* Parse $this->value as a structure based on given content.
7673
*
77-
* @param mixed $value APIB content
78-
* @param array $dependencies Object dependencies
74+
* @param object $object APIB content
75+
* @param array $dependencies Object dependencies
7976
*
8077
* @return void
8178
*/
82-
protected function parse_value_structure($value, array &$dependencies)
79+
protected function parse_value_structure(object $object, array &$dependencies)
8380
{
84-
switch ($this->type) {
81+
$type = $this->element === 'member' ? $this->type : $this->element;
82+
if (!isset($object->content->value) && !isset($object->attributes->enumerations)) {
83+
return;
84+
}
85+
86+
$value = $object->content->value ?? $object;
87+
switch ($type) {
88+
default:
89+
case 'object':
90+
$struct = $this->new_instance();
91+
break;
8592
case 'array':
8693
$struct = new ArrayStructureElement();
8794
break;
8895
case 'enum':
8996
$struct = new EnumStructureElement();
9097
break;
91-
default:
92-
case 'object':
93-
$value = $value->content->value ?? null;
94-
$struct = $this->new_instance();
95-
break;
9698
}
9799
$this->value = $struct->parse($value, $dependencies);
98100

@@ -113,25 +115,26 @@ protected function new_instance(): StructureElement
113115
/**
114116
* Parse content formed as an array.
115117
*
116-
* @param mixed $object APIB content
117-
* @param array $dependencies Object dependencies
118+
* @param object|null $object APIB content
119+
* @param array $dependencies Object dependencies
118120
*
119121
* @return void
120122
*/
121-
protected function parse_array_content($object, array &$dependencies): void
123+
protected function parse_array_content(?object $object, array &$dependencies): void
122124
{
123125
foreach ($object->content as $value) {
124-
switch ($this->type){
126+
$type = $this->element === 'member' ? $this->type : $this->element;
127+
switch ($type){
128+
default:
129+
case 'object':
130+
$struct = $this->new_instance();
131+
break;
125132
case 'enum':
126133
$struct = new EnumStructureElement();
127134
break;
128135
case 'array':
129136
$struct = new ArrayStructureElement();
130137
break;
131-
default:
132-
case 'object':
133-
$struct = $this->new_instance();
134-
break;
135138
}
136139

137140
$this->value[] = $struct->parse($value, $dependencies);
@@ -154,24 +157,22 @@ public function __toString(): string
154157
}
155158

156159
if (is_array($this->value)) {
157-
$return = '<table class="table table-striped mdl-data-table mdl-js-data-table ">';
160+
$return = '';
158161
foreach ($this->value as $object) {
159-
if (is_string($object) || is_subclass_of(get_class($object), BasicStructureElement::class)) {
162+
if (is_string($object) || is_subclass_of(get_class($object), StructureElement::class)) {
160163
$return .= $object;
161164
}
162165
}
163166

164-
$return .= '</table>';
165-
166-
return $this->description . $return;
167+
return "<table class=\"table table-striped mdl-data-table mdl-js-data-table \">$return</table>";
167168
}
168169

169170
if ($this->ref !== null) {
170-
return '<p>Inherits from <a href="#object-' . strtolower($this->ref) . '">' . $this->ref . '</a></p>' . $this->description;
171+
return '<p>Inherits from <a href="#object-' . strtolower($this->ref) . '">' . $this->ref . '</a></p>';
171172
}
172173

173174
if ($this->value === null && $this->key === null && $this->description !== null) {
174-
return "<div class='description clearfix'>$this->description</div>";
175+
return '';
175176
}
176177

177178
if ($this->value === null && $this->key === null && $this->description === null) {
@@ -218,15 +219,8 @@ protected function construct_string_return(string $value): string
218219
if ($this->type === NULL) {
219220
return $value;
220221
}
221-
if (!in_array($this->type, self::DEFAULTS)) {
222-
$type = '<a class="code" href="#object-' . str_replace(
223-
' ',
224-
'-',
225-
strtolower($this->type)
226-
) . '">' . $this->type . '</a>';
227-
} else {
228-
$type = '<code>' . $this->type . '</code>';
229-
}
222+
223+
$type = $this->get_element_as_html($this->type);
230224
$variable = '';
231225
if ($this->is_variable) {
232226
$variable = '<span class="fas fa-info variable-info" data-toggle="tooltip" data-placement="top" title="This is a variable key"></span>';

0 commit comments

Comments
 (0)