Skip to content

Commit 98a2a68

Browse files
committed
Adding request body parsing
1 parent 69d7da8 commit 98a2a68

File tree

5 files changed

+155
-9
lines changed

5 files changed

+155
-9
lines changed

src/PHPDraft/HTML/default.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ class="value"><?= $value; ?></span>
150150
<?php endforeach; ?>
151151
</ul>
152152
<?php endif; ?>
153+
<?php if (!empty($transition->request->body)): ?>
154+
<h5>Body</h5>
155+
<?php foreach ($transition->request->body as $value): ?>
156+
<?= $value->print_request('application/x-www-form-urlencoded');?>
157+
<?= $value ?>
158+
<?php endforeach; ?>
159+
<?php endif; ?>
153160
<?php endif; ?>
154161

155162
<?php if ($transition->url_variables !== []): ?>
@@ -214,7 +221,7 @@ class="value"><?= $value; ?></span>
214221
<?php if ($response->structure !== []): ?>
215222
<h5>Data Structure</h5>
216223
<dl class="dl-horizontal">
217-
<?php foreach ($response->structure[0]['struct']->value as $value): ?>
224+
<?php foreach ($response->structure[0]->value as $value): ?>
218225
<dt><?= $value->key; ?></dt>
219226
<dd>
220227
<a href="#object-<?= $value->type; ?>"><?= $value->type; ?></a>

src/PHPDraft/Model/DataStructureElement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function __toString()
139139
'<s class="pull-right">no example</s>' : '<blockquote>Example: ' . $this->value . '</blockquote>' ;
140140
$return =
141141
'<dt>' .
142-
'<a name="object-' . $this->key . '">' . $this->key . "</a>" .
142+
'<span>' . $this->key . "</span>" .
143143
"</dt>\t" .
144144
'<dd>' .
145145
$type .

src/PHPDraft/Model/HTTPRequest.php

+33-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* This file contains the HTTPRequest.php
44
*
55
* @package PHPDraft\Model
6-
* @author Sean Molenaar<[email protected]>
6+
* @author Sean Molenaar<[email protected]>
77
*/
88

99
namespace PHPDraft\Model;
@@ -28,6 +28,12 @@ class HTTPRequest
2828
*/
2929
public $parent;
3030

31+
/**
32+
* Body of the request (if POST or PUT)
33+
* @var RequestBodyElement[]
34+
*/
35+
public $body;
36+
3137
public function __construct(&$parent)
3238
{
3339
$this->parent = &$parent;
@@ -37,6 +43,19 @@ public function __construct(&$parent)
3743
public function parse($object)
3844
{
3945
$this->method = $object->attributes->method;
46+
47+
if (($this->method === 'POST' || $this->method === 'PUT') && !empty($object->content))
48+
{
49+
foreach ($object->content as $value)
50+
{
51+
if ($value->element === 'dataStructure')
52+
{
53+
$this->parse_structure($value->content);
54+
continue;
55+
}
56+
}
57+
}
58+
4059
if (isset($object->attributes->headers))
4160
{
4261
foreach ($object->attributes->headers->content as $value)
@@ -47,4 +66,17 @@ public function parse($object)
4766

4867
return $this;
4968
}
69+
70+
private function parse_structure($objects)
71+
{
72+
foreach ($objects as $object)
73+
{
74+
$deps = [];
75+
$struct = new RequestBodyElement();
76+
$struct->parse($object, $deps);
77+
$struct->deps = $deps;
78+
79+
$this->body[] = $struct;
80+
}
81+
}
5082
}

src/PHPDraft/Model/HTTPResponse.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* This file contains the HTTPResponse.php
44
*
55
* @package PHPDraft\Model
6-
* @author Sean Molenaar<[email protected]>
6+
* @author Sean Molenaar<[email protected]>
77
*/
88

99
namespace PHPDraft\Model;
@@ -55,27 +55,33 @@ public function parse($object)
5555
}
5656

5757
$this->parse_content($object);
58+
5859
return $this;
5960
}
6061

6162
/**
6263
* Parse request headers
6364
*
6465
* @param \stdClass $object An object to parse for headers
66+
*
6567
* @return void
6668
*/
6769
protected function parse_headers($object)
6870
{
6971
foreach ($object->content as $value)
7072
{
71-
if (isset($value->content)) $this->headers[$value->content->key->content] = $value->content->value->content;
73+
if (isset($value->content))
74+
{
75+
$this->headers[$value->content->key->content] = $value->content->value->content;
76+
}
7277
}
7378
}
7479

7580
/**
7681
* Parse request content
7782
*
7883
* @param \stdClass $object An object to parse for content
84+
*
7985
* @return void
8086
*/
8187
protected function parse_content($object)
@@ -99,16 +105,19 @@ protected function parse_content($object)
99105
* Parse structure of the content
100106
*
101107
* @param \stdClass[] $objects Objects containing the structure
108+
*
102109
* @return void
103110
*/
104111
protected function parse_structure($objects)
105112
{
106113
foreach ($objects as $object)
107114
{
108-
$deps = [];
109-
$struct = new DataStructureElement();
110-
$struct = $struct->parse($object, $deps);
111-
$this->structure[] = ['struct' => $struct, 'deps' => $deps];
115+
$deps = [];
116+
$struct = new DataStructureElement();
117+
$struct->parse($object, $deps);
118+
$struct->deps = $deps;
119+
120+
$this->structure[] = $struct;
112121
}
113122
}
114123
}
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* This file contains the RequestBodyElement
4+
*
5+
* @package PHPDraft\Model
6+
* @author Sean Molenaar<[email protected]>
7+
*/
8+
9+
namespace PHPDraft\Model;
10+
11+
12+
class RequestBodyElement extends DataStructureElement
13+
{
14+
/**
15+
* Parse a JSON object to a data structure
16+
*
17+
* @param \stdClass $object An object to parse
18+
* @param array $dependencies Dependencies of this object
19+
*
20+
* @return DataStructureElement self reference
21+
*/
22+
public function parse($object, &$dependencies)
23+
{
24+
if (empty($object) || !isset($object->content))
25+
{
26+
return $this;
27+
}
28+
$this->element = $object->element;
29+
if (isset($object->content) && is_array($object->content))
30+
{
31+
foreach ($object->content as $value)
32+
{
33+
$struct = new RequestBodyElement();
34+
$this->value[] = $struct->parse($value, $dependencies);
35+
}
36+
37+
return $this;
38+
}
39+
40+
$this->key = $object->content->key->content;
41+
$this->type = $object->content->value->element;
42+
$this->description = isset($object->meta->description) ? $object->meta->description : NULL;
43+
$this->status =
44+
isset($object->attributes->typeAttributes[0]) ? $object->attributes->typeAttributes[0] : NULL;
45+
46+
if (!in_array($this->type, $this->defaults))
47+
{
48+
$dependencies[] = $this->type;
49+
}
50+
51+
if ($this->type === 'object')
52+
{
53+
$value = isset($object->content->value->content) ? $object->content->value : NULL;
54+
$this->value = new RequestBodyElement();
55+
$this->value = $this->value->parse($value, $dependencies);
56+
57+
return $this;
58+
}
59+
60+
$this->value = isset($object->content->value->content) ? $object->content->value->content : NULL;
61+
62+
return $this;
63+
}
64+
65+
public function print_request($type = 'application/x-www-form-urlencoded')
66+
{
67+
if (is_array($this->value))
68+
{
69+
$return = '<code>';
70+
foreach ($this->value as $object)
71+
{
72+
if (get_class($object) === self::class)
73+
{
74+
$return .= $object->print_request($type);
75+
}
76+
}
77+
78+
$return .= '</code>';
79+
80+
return $return;
81+
}
82+
83+
$value = (empty($this->value)) ? '?' : $this->value;
84+
85+
switch ($type)
86+
{
87+
case 'application/x-www-form-urlencoded':
88+
return $this->key . "=<kbd>" . $value . '</kbd>';
89+
break;
90+
default:
91+
$object = [];
92+
$object[$this->key] = $value;
93+
94+
return json_encode($object) . PHP_EOL;
95+
break;
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)