-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathObjectStructureElement.php
More file actions
221 lines (186 loc) · 6.85 KB
/
ObjectStructureElement.php
File metadata and controls
221 lines (186 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
declare(strict_types=1);
/**
* This file contains the ObjectStructureElement.php.
*
* @package PHPDraft\Model\Elements
*
* @author Sean Molenaar<sean@seanmolenaar.eu>
*/
namespace PHPDraft\Model\Elements;
use Michelf\MarkdownExtra;
/**
* Class ObjectStructureElement.
*/
class ObjectStructureElement extends BasicStructureElement
{
/**
* Object representation before parsing
* @var \stdClass|null
* @phpstan-ignore-next-line
*/
private $object;
/**
* Unset object function.
* @internal Only for tests
*/
public function __clearForTest(): void
{
$this->object = null;
}
/**
* Parse a JSON object to a data structure.
*
* @param object|null $object An object to parse
* @param array $dependencies Dependencies of this object
*
* @return ObjectStructureElement self reference
*/
public function parse(?object $object, array &$dependencies): StructureElement
{
$this->object = $object;
if (is_null($object) || !isset($object->element) || !(isset($object->content) || isset($object->meta) )) {
return $this;
}
$this->element = $object->element;
$this->parse_common($object, $dependencies);
if (isset($object->content) && is_array($object->content)) {
$this->parse_array_content($object, $dependencies);
return $this;
}
if (in_array($this->type, ['object', 'array', 'enum'], true) || !in_array($this->type, self::DEFAULTS, true)) {
$this->parse_value_structure($object, $dependencies);
return $this;
}
if (isset($object->content->value->content)) {
$this->value = $object->content->value->content;
} elseif (isset($object->content->value->attributes->samples)) {
$this->value = array_reduce($object->content->value->attributes->samples->content, function ($carry, $item) {
if ($carry === null) {
return "$item->content ($item->element)";
}
return "$carry | $item->content ($item->element)";
});
} else {
$this->value = null;
}
return $this;
}
/**
* Parse $this->value as a structure based on given content.
*
* @param object $object APIB content
* @param array $dependencies Object dependencies
*
* @return void
*/
protected function parse_value_structure(object $object, array &$dependencies)
{
if (isset($object->content->content) || in_array($this->element, ['boolean', 'string', 'number', 'ref'])) {
return;
}
$value = $object->content->value ?? $object;
$type = in_array($this->element, ['member']) ? $this->type : $this->element;
$struct = $this->get_class($type);
$this->value = $struct->parse($value, $dependencies);
unset($struct);
unset($value);
}
/**
* Get a new instance of a class.
*
* @return ObjectStructureElement
*/
protected function new_instance(): StructureElement
{
return new self();
}
/**
* Parse content formed as an array.
*
* @param object $object APIB content
* @param array $dependencies Object dependencies
*
* @return void
*/
protected function parse_array_content(object $object, array &$dependencies): void
{
foreach ($object->content as $value) {
$type = $this->element === 'member' ? $this->type : $this->element;
$struct = $this->get_class($type);
$this->value[] = $struct->parse($value, $dependencies);
unset($struct);
}
unset($value);
}
/**
* Print a string representation.
*
* @return string
*/
public function __toString(): string
{
if (is_array($this->value)) {
$return = '';
foreach ($this->value as $object) {
if (is_string($object) || is_subclass_of(get_class($object), StructureElement::class)) {
$return .= $object;
}
}
return "<table class=\"table table-striped mdl-data-table mdl-js-data-table \">$return</table>";
}
if ($this->value === null && $this->key === null && $this->description !== null) {
return '';
}
if ($this->value === null && $this->key === null && $this->description === null) {
return '<span class="example-value pull-right">{ }</span>';
}
if (is_null($this->value)) {
return $this->construct_string_return('');
}
if (is_object($this->value) && (self::class === get_class($this->value) || RequestBodyElement::class === get_class($this->value))) {
return $this->construct_string_return('<div class="sub-struct">' . $this->value . '</div>');
}
if (is_object($this->value) && (ArrayStructureElement::class === get_class($this->value))) {
return $this->construct_string_return('<div class="array-struct">' . $this->value . '</div>');
}
if (is_object($this->value) && (EnumStructureElement::class === get_class($this->value))) {
return $this->construct_string_return('<div class="enum-struct">' . $this->value . '</div>');
}
$value = '<span class="example-value pull-right">';
if (is_bool($this->value)) {
$value .= ($this->value) ? 'true' : 'false';
} else {
$value .= $this->value;
}
$value .= '</span>';
return $this->construct_string_return($value);
}
/**
* Create an HTML return.
*
* @param string $value value to display
*
* @return string
*/
protected function construct_string_return(string $value): string
{
if ($this->type === null) {
return $value;
}
$type = $this->get_element_as_html($this->type);
$variable = '';
if ($this->is_variable === true) {
$link_name = str_replace(' ', '-', strtolower($this->key->type));
$tooltip = 'This is a variable key of type "' . $this->key->type . '"';
$variable = '<a class="variable-key" title="' . $this->key->type . '" href="#object-' . $link_name . '"><span class="fas fa-info variable-info" data-toggle="tooltip" data-placement="top" data-tooltip="' . $tooltip . '" title="' . $tooltip . '"></span></a>';
}
return '<tr>' .
'<td>' . '<span>' . $this->key->value . '</span>' . $variable . '</td>' .
'<td>' . $type . '</td>' .
'<td> <span class="status">' . $this->status . '</span></td>' .
'<td>' . MarkdownExtra::defaultTransform($this->description) . '</td>' .
'<td>' . $value . '</td>' .
'</tr>';
}
}