diff --git a/src/PHPDraft/HTML/default.php b/src/PHPDraft/HTML/default.php index 6e588de5..d67dabd7 100644 --- a/src/PHPDraft/HTML/default.php +++ b/src/PHPDraft/HTML/default.php @@ -150,6 +150,13 @@ class="value"> + request->body)): ?> +
Body
+ request->body as $value): ?> + print_request('application/x-www-form-urlencoded');?> + + + url_variables !== []): ?> @@ -214,7 +221,7 @@ class="value"> structure !== []): ?>
Data Structure
- structure[0]['struct']->value as $value): ?> + structure[0]->value as $value): ?>
key; ?>
type; ?> diff --git a/src/PHPDraft/Model/DataStructureElement.php b/src/PHPDraft/Model/DataStructureElement.php index fc90450a..6a6cc765 100644 --- a/src/PHPDraft/Model/DataStructureElement.php +++ b/src/PHPDraft/Model/DataStructureElement.php @@ -139,7 +139,7 @@ function __toString() 'no example' : '
Example: ' . $this->value . '
' ; $return = '
' . - '' . $this->key . "" . + '' . $this->key . "" . "
\t" . '
' . $type . diff --git a/src/PHPDraft/Model/HTTPRequest.php b/src/PHPDraft/Model/HTTPRequest.php index 1e6ce21f..768c5f7a 100644 --- a/src/PHPDraft/Model/HTTPRequest.php +++ b/src/PHPDraft/Model/HTTPRequest.php @@ -3,7 +3,7 @@ * This file contains the HTTPRequest.php * * @package PHPDraft\Model - * @author Sean Molenaar + * @author Sean Molenaar */ namespace PHPDraft\Model; @@ -28,6 +28,12 @@ class HTTPRequest */ public $parent; + /** + * Body of the request (if POST or PUT) + * @var RequestBodyElement[] + */ + public $body; + public function __construct(&$parent) { $this->parent = &$parent; @@ -37,6 +43,19 @@ public function __construct(&$parent) public function parse($object) { $this->method = $object->attributes->method; + + if (($this->method === 'POST' || $this->method === 'PUT') && !empty($object->content)) + { + foreach ($object->content as $value) + { + if ($value->element === 'dataStructure') + { + $this->parse_structure($value->content); + continue; + } + } + } + if (isset($object->attributes->headers)) { foreach ($object->attributes->headers->content as $value) @@ -47,4 +66,17 @@ public function parse($object) return $this; } + + private function parse_structure($objects) + { + foreach ($objects as $object) + { + $deps = []; + $struct = new RequestBodyElement(); + $struct->parse($object, $deps); + $struct->deps = $deps; + + $this->body[] = $struct; + } + } } \ No newline at end of file diff --git a/src/PHPDraft/Model/HTTPResponse.php b/src/PHPDraft/Model/HTTPResponse.php index 6a218a2a..f1014578 100644 --- a/src/PHPDraft/Model/HTTPResponse.php +++ b/src/PHPDraft/Model/HTTPResponse.php @@ -3,7 +3,7 @@ * This file contains the HTTPResponse.php * * @package PHPDraft\Model - * @author Sean Molenaar + * @author Sean Molenaar */ namespace PHPDraft\Model; @@ -55,6 +55,7 @@ public function parse($object) } $this->parse_content($object); + return $this; } @@ -62,13 +63,17 @@ public function parse($object) * Parse request headers * * @param \stdClass $object An object to parse for headers + * * @return void */ protected function parse_headers($object) { foreach ($object->content as $value) { - if (isset($value->content)) $this->headers[$value->content->key->content] = $value->content->value->content; + if (isset($value->content)) + { + $this->headers[$value->content->key->content] = $value->content->value->content; + } } } @@ -76,6 +81,7 @@ protected function parse_headers($object) * Parse request content * * @param \stdClass $object An object to parse for content + * * @return void */ protected function parse_content($object) @@ -99,16 +105,19 @@ protected function parse_content($object) * Parse structure of the content * * @param \stdClass[] $objects Objects containing the structure + * * @return void */ protected function parse_structure($objects) { foreach ($objects as $object) { - $deps = []; - $struct = new DataStructureElement(); - $struct = $struct->parse($object, $deps); - $this->structure[] = ['struct' => $struct, 'deps' => $deps]; + $deps = []; + $struct = new DataStructureElement(); + $struct->parse($object, $deps); + $struct->deps = $deps; + + $this->structure[] = $struct; } } } \ No newline at end of file diff --git a/src/PHPDraft/Model/RequestBodyElement.php b/src/PHPDraft/Model/RequestBodyElement.php new file mode 100644 index 00000000..43bb9f42 --- /dev/null +++ b/src/PHPDraft/Model/RequestBodyElement.php @@ -0,0 +1,98 @@ + + */ + +namespace PHPDraft\Model; + + +class RequestBodyElement extends DataStructureElement +{ + /** + * Parse a JSON object to a data structure + * + * @param \stdClass $object An object to parse + * @param array $dependencies Dependencies of this object + * + * @return DataStructureElement self reference + */ + public function parse($object, &$dependencies) + { + if (empty($object) || !isset($object->content)) + { + return $this; + } + $this->element = $object->element; + if (isset($object->content) && is_array($object->content)) + { + foreach ($object->content as $value) + { + $struct = new RequestBodyElement(); + $this->value[] = $struct->parse($value, $dependencies); + } + + return $this; + } + + $this->key = $object->content->key->content; + $this->type = $object->content->value->element; + $this->description = isset($object->meta->description) ? $object->meta->description : NULL; + $this->status = + isset($object->attributes->typeAttributes[0]) ? $object->attributes->typeAttributes[0] : NULL; + + if (!in_array($this->type, $this->defaults)) + { + $dependencies[] = $this->type; + } + + if ($this->type === 'object') + { + $value = isset($object->content->value->content) ? $object->content->value : NULL; + $this->value = new RequestBodyElement(); + $this->value = $this->value->parse($value, $dependencies); + + return $this; + } + + $this->value = isset($object->content->value->content) ? $object->content->value->content : NULL; + + return $this; + } + + public function print_request($type = 'application/x-www-form-urlencoded') + { + if (is_array($this->value)) + { + $return = ''; + foreach ($this->value as $object) + { + if (get_class($object) === self::class) + { + $return .= $object->print_request($type); + } + } + + $return .= ''; + + return $return; + } + + $value = (empty($this->value)) ? '?' : $this->value; + + switch ($type) + { + case 'application/x-www-form-urlencoded': + return $this->key . "=" . $value . ''; + break; + default: + $object = []; + $object[$this->key] = $value; + + return json_encode($object) . PHP_EOL; + break; + } + } +} \ No newline at end of file