Skip to content

Commit

Permalink
Adding request body parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Aug 22, 2016
1 parent 69d7da8 commit 98a2a68
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 9 deletions.
9 changes: 8 additions & 1 deletion src/PHPDraft/HTML/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ class="value"><?= $value; ?></span>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php if (!empty($transition->request->body)): ?>
<h5>Body</h5>
<?php foreach ($transition->request->body as $value): ?>
<?= $value->print_request('application/x-www-form-urlencoded');?>
<?= $value ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endif; ?>

<?php if ($transition->url_variables !== []): ?>
Expand Down Expand Up @@ -214,7 +221,7 @@ class="value"><?= $value; ?></span>
<?php if ($response->structure !== []): ?>
<h5>Data Structure</h5>
<dl class="dl-horizontal">
<?php foreach ($response->structure[0]['struct']->value as $value): ?>
<?php foreach ($response->structure[0]->value as $value): ?>
<dt><?= $value->key; ?></dt>
<dd>
<a href="#object-<?= $value->type; ?>"><?= $value->type; ?></a>
Expand Down
2 changes: 1 addition & 1 deletion src/PHPDraft/Model/DataStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function __toString()
'<s class="pull-right">no example</s>' : '<blockquote>Example: ' . $this->value . '</blockquote>' ;
$return =
'<dt>' .
'<a name="object-' . $this->key . '">' . $this->key . "</a>" .
'<span>' . $this->key . "</span>" .
"</dt>\t" .
'<dd>' .
$type .
Expand Down
34 changes: 33 additions & 1 deletion src/PHPDraft/Model/HTTPRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This file contains the HTTPRequest.php
*
* @package PHPDraft\Model
* @author Sean Molenaar<[email protected]>
* @author Sean Molenaar<[email protected]>
*/

namespace PHPDraft\Model;
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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;
}
}
}
21 changes: 15 additions & 6 deletions src/PHPDraft/Model/HTTPResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This file contains the HTTPResponse.php
*
* @package PHPDraft\Model
* @author Sean Molenaar<[email protected]>
* @author Sean Molenaar<[email protected]>
*/

namespace PHPDraft\Model;
Expand Down Expand Up @@ -55,27 +55,33 @@ public function parse($object)
}

$this->parse_content($object);

return $this;
}

/**
* 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;
}
}
}

/**
* Parse request content
*
* @param \stdClass $object An object to parse for content
*
* @return void
*/
protected function parse_content($object)
Expand All @@ -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;
}
}
}
98 changes: 98 additions & 0 deletions src/PHPDraft/Model/RequestBodyElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* This file contains the RequestBodyElement
*
* @package PHPDraft\Model
* @author Sean Molenaar<[email protected]>
*/

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 = '<code>';
foreach ($this->value as $object)
{
if (get_class($object) === self::class)
{
$return .= $object->print_request($type);
}
}

$return .= '</code>';

return $return;
}

$value = (empty($this->value)) ? '?' : $this->value;

switch ($type)
{
case 'application/x-www-form-urlencoded':
return $this->key . "=<kbd>" . $value . '</kbd>';
break;
default:
$object = [];
$object[$this->key] = $value;

return json_encode($object) . PHP_EOL;
break;
}
}
}

0 comments on commit 98a2a68

Please sign in to comment.