Skip to content

Commit d40d31d

Browse files
committed
Fix some more drafter 4.0 issues
Issue GH-75
1 parent 479cc8e commit d40d31d

File tree

6 files changed

+76
-34
lines changed

6 files changed

+76
-34
lines changed

Diff for: src/PHPDraft/Model/Category.php

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class Category extends HierarchyElement
2424
*/
2525
public $structures = [];
2626

27+
/**
28+
* Category type.
29+
*
30+
* @var ?string
31+
*/
32+
public $type = NULL;
33+
2734
/**
2835
* Fill class values based on JSON object.
2936
*
@@ -34,6 +41,9 @@ class Category extends HierarchyElement
3441
public function parse(stdClass $object)
3542
{
3643
parent::parse($object);
44+
45+
$this->type = $object->meta->classes->content ?? NULL;
46+
3747
foreach ($object->content as $item) {
3848
switch ($item->element) {
3949
case 'resource':
@@ -48,6 +58,8 @@ public function parse(stdClass $object)
4858

4959
if (is_array($item->content) && isset($item->content[0]->meta->id)) {
5060
$this->structures[$item->content[0]->meta->id] = $struct;
61+
} elseif (isset($item->content->meta->id->content)) {
62+
$this->structures[$item->content->meta->id->content] = $struct;
5163
} else {
5264
$this->structures[] = $struct;
5365
}

Diff for: src/PHPDraft/Model/Elements/BasicStructureElement.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ abstract protected function new_instance(): StructureElement;
8888
*
8989
* @return void
9090
*/
91-
protected function parse_common($object, array &$dependencies): void
91+
protected function parse_common(stdClass $object, array &$dependencies): void
9292
{
93-
$this->key = (isset($object->content->key->content)) ? $object->content->key->content : NULL;
94-
$this->type = (isset($object->content->value->element)) ? $object->content->value->element : NULL;
93+
$this->key = $object->content->key->content ?? NULL;
94+
$this->type = $object->content->value->element ?? NULL;
9595
$this->description = NULL;
9696
if (isset($object->meta->description->content)){
9797
$this->description = htmlentities($object->meta->description->content);

Diff for: src/PHPDraft/Model/HTTPRequest.php

+46-20
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,26 @@ class HTTPRequest implements Comparable
5757
*
5858
* @var mixed
5959
*/
60-
public $body = NULL;
60+
public $body = null;
6161

6262
/**
63-
* Identifier for the request.
63+
* Schema of the body of the request (if POST or PUT).
6464
*
65-
* @var string
65+
* @var mixed
6666
*/
67-
protected $id;
68-
67+
public $body_schema = null;
6968
/**
7069
* Structure of the request (if POST or PUT).
7170
*
7271
* @var RequestBodyElement
7372
*/
7473
public $struct = [];
74+
/**
75+
* Identifier for the request.
76+
*
77+
* @var string
78+
*/
79+
protected $id;
7580

7681
/**
7782
* HTTPRequest constructor.
@@ -94,21 +99,38 @@ public function __construct(Transition &$parent)
9499
public function parse(stdClass $object): self
95100
{
96101
$this->method = $object->attributes->method->content ?? $object->attributes->method;
97-
$this->title = isset($object->meta->title) ? $object->meta->title : NULL;
102+
$this->title = isset($object->meta->title) ? $object->meta->title : null;
98103

99104
if (($this->method === 'POST' || $this->method === 'PUT') && !empty($object->content)) {
100105
foreach ($object->content as $value) {
101106
if ($value->element === 'dataStructure') {
102107
$this->parse_structure($value);
103108
continue;
104-
} elseif ($value->element === 'copy') {
109+
}
110+
111+
if ($value->element === 'copy') {
105112
$this->description = MarkdownExtra::defaultTransform(htmlentities($value->content));
106-
} elseif ($value->element === 'asset') {
107-
if (in_array('messageBody', $value->meta->classes)) {
108-
$this->body[] = (isset($value->content)) ? $value->content : NULL;
109-
$this->headers['Content-Type'] =
110-
(isset($value->attributes->contentType)) ? $value->attributes->contentType : '';
111-
}
113+
continue;
114+
}
115+
116+
if ($value->element !== 'asset') {
117+
continue;
118+
}
119+
if (is_array($value->meta->classes) && in_array('messageBody', $value->meta->classes)) {
120+
$this->body[] = (isset($value->content)) ? $value->content : null;
121+
$this->headers['Content-Type'] = (isset($value->attributes->contentType)) ? $value->attributes->contentType : '';
122+
continue;
123+
}
124+
125+
if (isset($value->meta->classes->content)
126+
&& is_array($value->meta->classes->content)
127+
&& $value->meta->classes->content[0]->content === 'messageBody') {
128+
$this->body[] = (isset($value->content)) ? $value->content : null;
129+
$this->headers['Content-Type'] = (isset($value->attributes->contentType->content)) ? $value->attributes->contentType->content : '';
130+
} elseif (isset($value->meta->classes->content)
131+
&& is_array($value->meta->classes->content)
132+
&& $value->meta->classes->content[0]->content === 'messageBodySchema') {
133+
$this->body_schema = (isset($value->content)) ? $value->content : null;
112134
}
113135
}
114136
}
@@ -119,7 +141,7 @@ public function parse(stdClass $object): self
119141
}
120142
}
121143

122-
if ($this->body === NULL) {
144+
if ($this->body === null) {
123145
$this->body = &$this->struct;
124146
}
125147

@@ -152,15 +174,15 @@ public function get_id(): string
152174
* @param string $base_url URL to the base server
153175
* @param array $additional Extra options to pass to cURL
154176
*
177+
* @return string An executable cURL command
155178
* @throws Exception
156179
*
157-
* @return string An executable cURL command
158180
*/
159181
public function get_curl_command(string $base_url, array $additional = []): string
160182
{
161183
$options = [];
162184

163-
$type = $this->headers['Content-Type'] ?? NULL;
185+
$type = $this->headers['Content-Type'] ?? null;
164186

165187
$options[] = '-X' . $this->method;
166188
if (empty($this->body)) {
@@ -171,6 +193,9 @@ public function get_curl_command(string $base_url, array $additional = []): stri
171193
$options[] = '--data-binary ' . escapeshellarg(join('', $this->body));
172194
} elseif (is_subclass_of($this->struct, StructureElement::class)) {
173195
foreach ($this->struct->value as $body) {
196+
if (empty($body)) {
197+
continue;
198+
}
174199
$options[] = '--data-binary ' . escapeshellarg(strip_tags($body->print_request($type)));
175200
}
176201
}
@@ -179,7 +204,8 @@ public function get_curl_command(string $base_url, array $additional = []): stri
179204
}
180205
$options = array_merge($options, $additional);
181206

182-
return htmlspecialchars('curl ' . join(' ', $options) . ' ' . escapeshellarg($this->parent->build_url($base_url, TRUE)));
207+
return htmlspecialchars('curl ' . join(' ', $options) . ' ' . escapeshellarg($this->parent->build_url($base_url,
208+
true)));
183209
}
184210

185211
/**
@@ -200,17 +226,17 @@ public function is_equal_to($b): bool
200226
* @param string $base_url URL to the base server
201227
* @param array $additional Extra options to pass to the service
202228
*
229+
* @return string
203230
* @throws Exception
204231
*
205-
* @return string
206232
*/
207233
public function get_hurl_link(string $base_url, array $additional = []): string
208234
{
209235
$options = [];
210236

211-
$type = (isset($this->headers['Content-Type'])) ? $this->headers['Content-Type'] : NULL;
237+
$type = (isset($this->headers['Content-Type'])) ? $this->headers['Content-Type'] : null;
212238

213-
$url = $this->parent->build_url($base_url, TRUE);
239+
$url = $this->parent->build_url($base_url, true);
214240
$url = explode('?', $url);
215241
if (isset($url[1])) {
216242
$params = [];

Diff for: src/PHPDraft/Model/HTTPResponse.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,17 @@ protected function parse_headers(stdClass $object): void
124124
protected function parse_content(stdClass $object): void
125125
{
126126
foreach ($object->content as $value) {
127-
if ($value->element === 'dataStructure') {
128-
$this->parse_structure($value->content);
129-
continue;
130-
} elseif ($value->element === 'copy') {
127+
if ($value->element === 'copy') {
131128
$this->description = MarkdownExtra::defaultTransform(htmlentities($value->content));
132129
continue;
133130
}
134131

132+
if ($value->element === 'dataStructure') {
133+
$content = is_array($value->content) ? $value->content : [$value->content];
134+
$this->parse_structure($content);
135+
continue;
136+
}
137+
135138
if (isset($value->attributes->contentType->content)) {
136139
$this->content[$value->attributes->contentType->content] = $value->content;
137140
} elseif (isset($value->attributes->contentType)) {

Diff for: src/PHPDraft/Out/HTML/default.phtml

+4-4
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,21 @@ use PHPDraft\Out\Minifier;
235235
<div class="row">
236236
<?php foreach ($response->structure as $value): ?>
237237
<?= $value; ?>
238-
<?php endforeach; ?>
238+
<?php endforeach; ?>
239239
</div>
240240
<?php endif; ?>
241-
<?php foreach ($response->content as $key => $value): ?>
241+
<?php foreach ($response->content as $response_key => $value): ?>
242242
<div>
243243
<?php $href =
244244
$response->get_id() . '-' . $response->statuscode . '-' . str_replace([
245245
'/',
246246
'+',
247-
], '-', $key); ?>
247+
], '-', $response_key); ?>
248248
<h5 class="response-body"
249249
data-toggle="collapse"
250250
data-target="#response-<?= $href ?>">
251251
<i class="fas indicator fa-angle-up"></i>
252-
<?= $key; ?>
252+
<?= $response_key; ?>
253253

254254
</h5>
255255
<pre class="collapse response-body"

Diff for: src/PHPDraft/Parse/BaseParser.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ public function parseToJson(): stdClass
9191
foreach ($this->json->content as $item) {
9292
if ($item->element === 'annotation') {
9393
$warnings = TRUE;
94-
$prefix = strtoupper($item->meta->classes[0]);
94+
$line = $item->attributes->sourceMap->content[0]->content[0]->content[0]->attributes->line->content ?? 'UNKNOWN';
95+
$prefix = (is_array($item->meta->classes)) ? strtoupper($item->meta->classes[0]) : strtoupper($item->meta->classes->content[0]->content);
9596
$error = $item->content;
96-
file_put_contents('php://stderr', "$prefix: $error\n");
97-
file_put_contents('php://stdout', "<pre>$prefix: $error</pre>\n");
97+
file_put_contents('php://stderr', "$prefix: $error (line $line)\n");
98+
file_put_contents('php://stdout', "<pre>$prefix: $error (line $line)</pre>\n");
9899
}
99100
}
100101

0 commit comments

Comments
 (0)