-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBasicStructureElement.php
More file actions
211 lines (191 loc) · 5.49 KB
/
BasicStructureElement.php
File metadata and controls
211 lines (191 loc) · 5.49 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
<?php
declare(strict_types=1);
/**
* Basic structure element
*/
namespace PHPDraft\Model\Elements;
abstract class BasicStructureElement implements StructureElement
{
/**
* Object key.
*
* @var ElementStructureElement|null
*/
public $key;
/**
* Object JSON type.
*
* @var string|null
*/
public $type;
/**
* Object description.
*
* @var string|null
*/
public $description;
/**
* Type of element.
*
* @var string|null
*/
public $element = null;
/**
* Object value.
*
* @var mixed
*/
public $value = null;
/**
* Object status (required|optional).
*
* @var string|null
*/
public $status = '';
/**
* Parent structure.
*
* @var string|null
*/
public $ref;
/**
* Is variable.
*
* @var bool
*/
public $is_variable;
/**
* List of object dependencies.
*
* @var string[]|null
*/
public $deps;
/**
* Parse a JSON object to a structure.
*
* @param object|null $object An object to parse
* @param array $dependencies Dependencies of this object
*
* @return StructureElement self reference
*/
abstract public function parse(?object $object, array &$dependencies): StructureElement;
/**
* Print a string representation.
*
* @return string
*/
abstract public function __toString(): string;
/**
* Get a new instance of a class.
*
* @return self
*/
abstract protected function new_instance(): StructureElement;
/**
* Parse common fields to give context.
*
* @param object $object APIB object
* @param array $dependencies Object dependencies
*
* @return void
*/
protected function parse_common(object $object, array &$dependencies): void
{
$this->key = null;
if (isset($object->content->key)) {
$key = new ElementStructureElement();
$key->parse($object->content->key, $dependencies);
$this->key = $key;
}
$this->type = $object->content->value->element
?? $object->meta->title->content
?? $object->meta->id->content
?? null;
$this->description = null;
if (isset($object->meta->description->content)) {
$this->description = htmlentities($object->meta->description->content);
} elseif (isset($object->meta->description)) {
$this->description = htmlentities($object->meta->description);
}
if ($this->description !== null) {
$encoded = htmlentities($this->description, ENT_COMPAT, 'ISO-8859-1', false);
$this->description = $encoded;
}
$this->ref = null;
if ($this->element === 'ref') {
$this->ref = $object->content;
}
$this->is_variable = $object->attributes->variable->content ?? false;
$this->status = null;
if (isset($object->attributes->typeAttributes->content)) {
$data = array_map(function ($item) {
return $item->content;
}, $object->attributes->typeAttributes->content);
$this->status = join(', ', $data);
} elseif (isset($object->attributes->typeAttributes)) {
$this->status = join(', ', $object->attributes->typeAttributes);
}
if (!in_array($this->type, self::DEFAULTS) && $this->type !== null) {
$dependencies[] = $this->type;
}
}
/**
* Represent the element in HTML.
*
* @param string $element Element name
*
* @return string HTML string
*/
protected function get_element_as_html(string $element): string
{
if (in_array($element, self::DEFAULTS)) {
return '<code>' . $element . '</code>';
}
$link_name = str_replace(' ', '-', strtolower($element));
return '<a class="code" title="' . $element . '" href="#object-' . $link_name . '">' . $element . '</a>';
}
/**
* Get a string representation of the value.
*
* @param bool $flat get a flat representation of the item.
*
* @return string
*/
public function string_value(bool $flat = false)
{
if (is_array($this->value)) {
$value_key = rand(0, count($this->value));
if (is_subclass_of($this->value[$value_key], StructureElement::class) && $flat === false) {
return $this->value[$value_key]->string_value($flat);
}
return $this->value[$value_key];
}
if (is_subclass_of($this->value, BasicStructureElement::class) && $flat === true) {
return is_array($this->value->value) ? array_keys($this->value->value)[0] : $this->value->value;
}
return $this->value;
}
/**
* Get what element to parse with.
*
* @param string $element The string to parse.
*
* @return BasicStructureElement The element to parse to
*/
public function get_class(string $element): BasicStructureElement
{
switch ($element) {
default:
case 'object':
$struct = $this->new_instance();
break;
case 'array':
$struct = new ArrayStructureElement();
break;
case 'enum':
$struct = new EnumStructureElement();
break;
}
return $struct;
}
}