Skip to content

Commit

Permalink
Merge pull request #10 from SMillerDev/fix/arrays
Browse files Browse the repository at this point in the history
Fix arrays and display fixes
  • Loading branch information
SMillerDev authored Sep 5, 2016
2 parents ad1bf76 + edd663f commit b219ece
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 222 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));
}
}
85 changes: 47 additions & 38 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 @@ -118,49 +120,56 @@ function __toString()
{
if ($this->value === NULL && $this->key === NULL)
{
return '{ ... }';
return '<span class="example-value pull-right">{ ... }</span>';
}

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
7 changes: 5 additions & 2 deletions src/PHPDraft/Model/HTTPRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ private function parse_structure($objects)
*
* @param string $base_url URL to the base server
*
* @param array $additional Extra options to pass to cURL
*
* @return string An executable cURL command
*/
public function get_curl_command($base_url)
public function get_curl_command($base_url, $additional = [])
{
$options = [];

Expand All @@ -120,7 +122,8 @@ public function get_curl_command($base_url)
{
$options[] = '-H "'.$header.': '.$value. '"';
}
$options[] = '-v';
$options = array_merge($options, $additional);

return htmlspecialchars('curl '.join(' ', $options). ' "'.$this->parent->build_url($base_url).'"');
}

Expand Down
6 changes: 4 additions & 2 deletions src/PHPDraft/Model/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ public function get_method()
*
* @param string $base_url base URL of the server
*
* @param array $additional additional arguments to pass
*
* @return string A cURL CLI command
*/
public function get_curl_command($base_url)
public function get_curl_command($base_url, $additional = [])
{
return $this->request->get_curl_command($base_url);
return $this->request->get_curl_command($base_url, $additional);
}
}
Loading

0 comments on commit b219ece

Please sign in to comment.