Skip to content

Commit

Permalink
Fix arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Sep 5, 2016
1 parent ad1bf76 commit bba60e3
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 102 deletions.
5 changes: 3 additions & 2 deletions src/PHPDraft/Model/APIBlueprintElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ function parse($object)
*/
public function get_href()
{
$prep = ($this->parent !== NULL) ? $this->parent->get_href() . '-' : '';
$seperator = '-';
$prep = ($this->parent !== NULL) ? $this->parent->get_href() . $seperator : '';

return $prep . str_replace(' ', '-', strtolower($this->title));
return $prep . str_replace(' ', $seperator, strtolower($this->title));
}
}
83 changes: 46 additions & 37 deletions src/PHPDraft/Model/DataStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,51 @@

namespace PHPDraft\Model;

use PHPDraft\Model\Elements\ArrayStructureElement;

class DataStructureElement
{
/**
* Default datatypes
* @var array
*/
const DEFAULTS = ['boolean', 'string', 'number', 'object', 'array'];
/**
* Object key
* @var string
*/
public $key;

/**
* Object JSON type
* @var string
* @var mixed
*/
public $type;

/**
* Object description
* @var string
*/
public $description;

/**
* Type of element
* @var string
*/
public $element = NULL;

/**
* Object value
* @var mixed|DataStructureElement[]
*/
public $value = NULL;

/**
* Object status (required|optional)
* @var string
*/
public $status = '';

/**
* List of object dependencies
* @var string[]
*/
public $deps;

/**
* Default datatypes
* @var array
*/
protected $defaults = ['boolean', 'string', 'number', 'object', 'array'];

/**
* Parse a JSON object to a data structure
*
Expand Down Expand Up @@ -88,17 +83,24 @@ public function parse($object, &$dependencies)
$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;
isset($object->attributes->typeAttributes) ? join(', ', $object->attributes->typeAttributes) : NULL;

if (!in_array($this->type, $this->defaults))
if (!in_array($this->type, self::DEFAULTS))
{
$dependencies[] = $this->type;
}

if ($this->type === 'object')
if ($this->type === 'object' || $this->type === 'array')
{
$value = isset($object->content->value->content) ? $object->content->value : NULL;
$this->value = new DataStructureElement();
$value = isset($object->content->value->content) ? $object->content->value : NULL;
if ($this->type === 'array')
{
$this->value = new ArrayStructureElement();
}
else
{
$this->value = new DataStructureElement();
}
$this->value = $this->value->parse($value, $dependencies);

return $this;
Expand All @@ -123,44 +125,51 @@ function __toString()

if (is_array($this->value))
{
$return = '<dl class="dl-horizontal">';
$return = '<table class="table table-striped">';
foreach ($this->value as $object)
{
if (get_class($object) === get_class($this))
if (get_class($object) === self::class || get_class($object) === ArrayStructureElement::class)
{
$return .= $object;
}
}

$return .= '</dl>';
$return .= '</table>';

return $return;
}

$type = (!in_array($this->type, $this->defaults)) ?
'<a class="code" href="#object-' . $this->type . '">' . $this->type . '</a>' : '<code>'.$this->type.'</code>';
$type = (!in_array($this->type, self::DEFAULTS)) ?
'<a class="code" href="#object-' . $this->type . '">' . $this->type . '</a>' : '<code>' . $this->type . '</code>';

if (empty($this->value))
{
$value = '<s class="example-value pull-right">no example</s>';
$value = '';
}
else if (is_object($this->value) && self::class === get_class($this->value))
else
{
$value = '<div class="sub-struct">'.$this->value.'</div>';
}
else{
$value = '<span class="example-value pull-right">Example: ' . $this->value . '</span>';
if (is_object($this->value) && self::class === get_class($this->value))
{
$value = '<div class="sub-struct">' . $this->value . '</div>';
}
elseif (is_object($this->value) && (ArrayStructureElement::class === get_class($this->value)))
{
$value = '<div class="array-struct">' . $this->value . '</div>';
}
else
{
$value = '<span class="example-value pull-right">' . $this->value . '</span>';
}
}

$return =
'<dt>' .
'<span>' . $this->key . "</span>" .
"</dt>\t" .
'<dd>' .
$type .
'<span>' . $this->description . '</span>' .
$value .
'</dd>';
'<tr>' .
'<td>' . '<span>' . $this->key . "</span>" . '</td>' .
'<td>' . $type . '</td>' .
'<td> <span class="status">' . $this->status . '</span></td>' .
'<td><span>' . $this->description . '</span></td>' .
'<td>' . $value . '</td>' .
'</tr>';

return $return;
}
Expand Down
71 changes: 71 additions & 0 deletions src/PHPDraft/Model/Elements/ArrayStructureElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Created by PhpStorm.
* User: smillernl
* Date: 2-9-16
* Time: 15:21
*/

namespace PHPDraft\Model\Elements;


use PHPDraft\Model\DataStructureElement;

class ArrayStructureElement extends DataStructureElement
{

public function parse($item, &$dependencies)
{
$this->element = (isset($item->element)) ? $item->element : 'array';
$this->element = (isset($item->element)) ? $item->element : NULL;
$this->value = (isset($item->content)) ? $item->content : NULL;

if (isset($item->content))
{
foreach ($item->content as $key => $sub_item)
{
$this->type[$key] = $sub_item->element;
switch ($sub_item->element)
{
case 'array':
$value = new ArrayStructureElement();
$this->value[$key] = $value->parse($sub_item, $dependencies);
break;
case 'object':
$value = new DataStructureElement();
$this->value[$key] = $value->parse($sub_item, $dependencies);
break;
default:
$this->value[$key] = (isset($sub_item->content)) ? $sub_item->content : NULL;
break;
}
}
}

return $this;
}

function __toString()
{
if (!is_array($this->type))
{
return '';
}
$return = '<ul class="list-group">';
foreach ($this->type as $key => $item)
{
$type =
(in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . $item . '">' . $item . '</a>';

$value =
(isset($this->value[$key])) ? ': <span class="example-value pull-right">' . json_encode($this->value[$key]) . '</span>' : NULL;

$return .= '<li class="list-group-item">' . $type . $value . '</li>';
}
$return .= '</ul>';

return $return;
}


}
2 changes: 1 addition & 1 deletion src/PHPDraft/Model/Elements/RequestBodyElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function parse($object, &$dependencies)
$this->status =
isset($object->attributes->typeAttributes[0]) ? $object->attributes->typeAttributes[0] : NULL;

if (!in_array($this->type, $this->defaults))
if (!in_array($this->type, parent::DEFAULTS))
{
$dependencies[] = $this->type;
}
Expand Down
File renamed without changes.
File renamed without changes.
62 changes: 16 additions & 46 deletions src/PHPDraft/Out/HTML/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
<?= file_get_contents(__DIR__ . '/index.css');?>
<?= file_get_contents(__DIR__ . '/'.$this->template.'.css');?>
</style>
</head>
<body>
Expand Down Expand Up @@ -101,11 +101,11 @@ class="pull-right <?= $this->get_method_icon($transition->get_method()); ?>"></s
</div>
<div class="col-md-10">
<?php foreach ($base as $category): ?>
<h2><a name="<?= $category->get_href(); ?>"><?= $category->title; ?></a></h2>
<h2><a id="<?= $category->get_href(); ?>"><?= $category->title; ?></a></h2>
<p><?= $category->description; ?></p>
<?php foreach ($category->children as $resource): ?>
<h3>
<a name="<?= $resource->get_href(); ?>"><?= $resource->title; ?></a>
<a id="<?= $resource->get_href(); ?>"><?= $resource->title; ?></a>
<small><?= $resource->href; ?></small>
</h3>
<p><?php $resource->description; ?></p>
Expand All @@ -117,7 +117,7 @@ class="panel panel-default <?= $transition->get_method(); ?>">
<var><?= $transition->get_method(); ?></var>
<code><?= $transition->href; ?></code>
<a class="pull-right transition-title"
name="<?= $transition->get_href(); ?>"><?= $transition->title; ?></a>
id="<?= $transition->get_href(); ?>"><?= $transition->title; ?></a>
</h3>
</div>
<div class="panel-body">
Expand Down Expand Up @@ -163,8 +163,9 @@ class="value"><?= $value; ?></span>
<?php if (!empty($transition->request->body)): ?>
<h5>Body</h5>
<?php foreach ($transition->request->body as $value): ?>
<?php $type = (isset($transition->request->headers['Content-Type']))?$transition->request->headers['Content-Type']:NULL;?>
<?= $value->print_request($type);?>
<?php $type =
(isset($transition->request->headers['Content-Type'])) ? $transition->request->headers['Content-Type'] : NULL; ?>
<?= $value->print_request($type); ?>
<?= $value ?>
<?php endforeach; ?>
<?php endif; ?>
Expand All @@ -173,35 +174,17 @@ class="value"><?= $value; ?></span>
<?php if ($transition->url_variables !== []): ?>
<h5>URI Parameters</h5>
<dl class="dl-horizontal">
<?php foreach ($transition->url_variables as $key => $value): ?>
<?php $status =
($value->status === '') ? '' : '(' . $value->status . ')'; ?>
<dt><?= $key; ?></dt>
<dd>
<code><?= $value->type; ?></code>
<?= $status; ?>
<samp><?= is_array($value->value) ? join('|', $value->value) : $value->value; ?></samp>
<p><?= $value->description; ?></p>
</dd>
<?php foreach ($transition->url_variables as $value): ?>
<?= $value; ?>
<?php endforeach; ?>
</dl>
<?php endif; ?>

<?php if ($transition->data_variables !== []): ?>
<h5>Data object</h5>
<dl class="dl-horizontal">
<?php foreach ($transition->data_variables as $key => $value): ?>
<?php $status =
($value->status === '') ? '' : '(' . $value->status . ')'; ?>
<dt><?= $key; ?></dt>
<dd>
<code><?= $value->type; ?></code>
<?= $status; ?>
<samp><?= is_array($value->value) ? join('|', $value->value) : $value->value; ?></samp>
<p><?= $value->description; ?></p>
</dd>
<?php foreach ($transition->data_variables as $value): ?>
<?= $value ?>
<?php endforeach; ?>
</dl>
<?php endif; ?>
</div>
<?php if (isset($transition->responses)): ?>
Expand Down Expand Up @@ -231,22 +214,9 @@ class="value"><?= $value; ?></span>
<?php endif; ?>
<?php if ($response->structure !== []): ?>
<h5>Data Structure</h5>
<dl class="dl-horizontal">
<?php foreach ($response->structure[0]->value as $value): ?>
<dt><?= $value->key; ?></dt>
<dd>
<a href="#object-<?= $value->type; ?>"><?= $value->type; ?></a>
<span><?= $value->description; ?></span>
<?php if (isset($value->value->element) && $value->value->element === 'object')
: ?>
<?= $this->get_data_structure($value->value); ?>
<?php else: ?>
<blockquote><?= $value->value; ?></blockquote>
<?php endif; ?>

</dd>
<?php endforeach; ?>
</dl>
<?php foreach ($response->structure as $value): ?>
<?= $value; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php foreach ($response->content as $key => $value): ?>
<h5><?= $key; ?></h5>
Expand All @@ -264,7 +234,7 @@ class="value"><?= $value; ?></span>
<div class="panel panel-default <?= $key; ?>">
<div class="panel-heading">
<h3 class="panel-title">
<a name="object-<?= $key; ?>"><?= $key; ?></a>
<a id="object-<?= $key; ?>"><?= $key; ?></a>
</h3>
</div>
<div class="panel-body">
Expand All @@ -281,6 +251,6 @@ class="value"><?= $value; ?></span>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<script><?= file_get_contents(__DIR__ . '/index.js');?></script>
<script><?= file_get_contents(__DIR__ . '/'.$this->template.'.js'); ?></script>
</body>
</html>
Loading

0 comments on commit bba60e3

Please sign in to comment.