Skip to content

Commit b219ece

Browse files
authored
Merge pull request #10 from SMillerDev/fix/arrays
Fix arrays and display fixes
2 parents ad1bf76 + edd663f commit b219ece

11 files changed

+303
-222
lines changed

src/PHPDraft/Model/APIBlueprintElement.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ function parse($object)
8181
*/
8282
public function get_href()
8383
{
84-
$prep = ($this->parent !== NULL) ? $this->parent->get_href() . '-' : '';
84+
$seperator = '-';
85+
$prep = ($this->parent !== NULL) ? $this->parent->get_href() . $seperator : '';
8586

86-
return $prep . str_replace(' ', '-', strtolower($this->title));
87+
return $prep . str_replace(' ', $seperator, strtolower($this->title));
8788
}
8889
}

src/PHPDraft/Model/DataStructureElement.php

+47-38
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,51 @@
88

99
namespace PHPDraft\Model;
1010

11+
use PHPDraft\Model\Elements\ArrayStructureElement;
12+
1113
class DataStructureElement
1214
{
15+
/**
16+
* Default datatypes
17+
* @var array
18+
*/
19+
const DEFAULTS = ['boolean', 'string', 'number', 'object', 'array'];
1320
/**
1421
* Object key
1522
* @var string
1623
*/
1724
public $key;
18-
1925
/**
2026
* Object JSON type
21-
* @var string
27+
* @var mixed
2228
*/
2329
public $type;
24-
2530
/**
2631
* Object description
2732
* @var string
2833
*/
2934
public $description;
30-
3135
/**
3236
* Type of element
3337
* @var string
3438
*/
3539
public $element = NULL;
36-
3740
/**
3841
* Object value
3942
* @var mixed|DataStructureElement[]
4043
*/
4144
public $value = NULL;
42-
4345
/**
4446
* Object status (required|optional)
4547
* @var string
4648
*/
4749
public $status = '';
48-
4950
/**
5051
* List of object dependencies
5152
* @var string[]
5253
*/
5354
public $deps;
5455

55-
/**
56-
* Default datatypes
57-
* @var array
58-
*/
59-
protected $defaults = ['boolean', 'string', 'number', 'object', 'array'];
60-
6156
/**
6257
* Parse a JSON object to a data structure
6358
*
@@ -88,17 +83,24 @@ public function parse($object, &$dependencies)
8883
$this->type = $object->content->value->element;
8984
$this->description = isset($object->meta->description) ? $object->meta->description : NULL;
9085
$this->status =
91-
isset($object->attributes->typeAttributes[0]) ? $object->attributes->typeAttributes[0] : NULL;
86+
isset($object->attributes->typeAttributes) ? join(', ', $object->attributes->typeAttributes) : NULL;
9287

93-
if (!in_array($this->type, $this->defaults))
88+
if (!in_array($this->type, self::DEFAULTS))
9489
{
9590
$dependencies[] = $this->type;
9691
}
9792

98-
if ($this->type === 'object')
93+
if ($this->type === 'object' || $this->type === 'array')
9994
{
100-
$value = isset($object->content->value->content) ? $object->content->value : NULL;
101-
$this->value = new DataStructureElement();
95+
$value = isset($object->content->value->content) ? $object->content->value : NULL;
96+
if ($this->type === 'array')
97+
{
98+
$this->value = new ArrayStructureElement();
99+
}
100+
else
101+
{
102+
$this->value = new DataStructureElement();
103+
}
102104
$this->value = $this->value->parse($value, $dependencies);
103105

104106
return $this;
@@ -118,49 +120,56 @@ function __toString()
118120
{
119121
if ($this->value === NULL && $this->key === NULL)
120122
{
121-
return '{ ... }';
123+
return '<span class="example-value pull-right">{ ... }</span>';
122124
}
123125

124126
if (is_array($this->value))
125127
{
126-
$return = '<dl class="dl-horizontal">';
128+
$return = '<table class="table table-striped">';
127129
foreach ($this->value as $object)
128130
{
129-
if (get_class($object) === get_class($this))
131+
if (get_class($object) === self::class || get_class($object) === ArrayStructureElement::class)
130132
{
131133
$return .= $object;
132134
}
133135
}
134136

135-
$return .= '</dl>';
137+
$return .= '</table>';
136138

137139
return $return;
138140
}
139141

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

143145
if (empty($this->value))
144146
{
145-
$value = '<s class="example-value pull-right">no example</s>';
147+
$value = '';
146148
}
147-
else if (is_object($this->value) && self::class === get_class($this->value))
149+
else
148150
{
149-
$value = '<div class="sub-struct">'.$this->value.'</div>';
150-
}
151-
else{
152-
$value = '<span class="example-value pull-right">Example: ' . $this->value . '</span>';
151+
if (is_object($this->value) && self::class === get_class($this->value))
152+
{
153+
$value = '<div class="sub-struct">' . $this->value . '</div>';
154+
}
155+
elseif (is_object($this->value) && (ArrayStructureElement::class === get_class($this->value)))
156+
{
157+
$value = '<div class="array-struct">' . $this->value . '</div>';
158+
}
159+
else
160+
{
161+
$value = '<span class="example-value pull-right">' . $this->value . '</span>';
162+
}
153163
}
154164

155165
$return =
156-
'<dt>' .
157-
'<span>' . $this->key . "</span>" .
158-
"</dt>\t" .
159-
'<dd>' .
160-
$type .
161-
'<span>' . $this->description . '</span>' .
162-
$value .
163-
'</dd>';
166+
'<tr>' .
167+
'<td>' . '<span>' . $this->key . "</span>" . '</td>' .
168+
'<td>' . $type . '</td>' .
169+
'<td> <span class="status">' . $this->status . '</span></td>' .
170+
'<td><span>' . $this->description . '</span></td>' .
171+
'<td>' . $value . '</td>' .
172+
'</tr>';
164173

165174
return $return;
166175
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: smillernl
5+
* Date: 2-9-16
6+
* Time: 15:21
7+
*/
8+
9+
namespace PHPDraft\Model\Elements;
10+
11+
12+
use PHPDraft\Model\DataStructureElement;
13+
14+
class ArrayStructureElement extends DataStructureElement
15+
{
16+
17+
public function parse($item, &$dependencies)
18+
{
19+
$this->element = (isset($item->element)) ? $item->element : 'array';
20+
$this->element = (isset($item->element)) ? $item->element : NULL;
21+
$this->value = (isset($item->content)) ? $item->content : NULL;
22+
23+
if (isset($item->content))
24+
{
25+
foreach ($item->content as $key => $sub_item)
26+
{
27+
$this->type[$key] = $sub_item->element;
28+
switch ($sub_item->element)
29+
{
30+
case 'array':
31+
$value = new ArrayStructureElement();
32+
$this->value[$key] = $value->parse($sub_item, $dependencies);
33+
break;
34+
case 'object':
35+
$value = new DataStructureElement();
36+
$this->value[$key] = $value->parse($sub_item, $dependencies);
37+
break;
38+
default:
39+
$this->value[$key] = (isset($sub_item->content)) ? $sub_item->content : NULL;
40+
break;
41+
}
42+
}
43+
}
44+
45+
return $this;
46+
}
47+
48+
function __toString()
49+
{
50+
if (!is_array($this->type))
51+
{
52+
return '';
53+
}
54+
$return = '<ul class="list-group">';
55+
foreach ($this->type as $key => $item)
56+
{
57+
$type =
58+
(in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . $item . '">' . $item . '</a>';
59+
60+
$value =
61+
(isset($this->value[$key])) ? ': <span class="example-value pull-right">' . json_encode($this->value[$key]) . '</span>' : NULL;
62+
63+
$return .= '<li class="list-group-item">' . $type . $value . '</li>';
64+
}
65+
$return .= '</ul>';
66+
67+
return $return;
68+
}
69+
70+
71+
}

src/PHPDraft/Model/Elements/RequestBodyElement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function parse($object, &$dependencies)
4444
$this->status =
4545
isset($object->attributes->typeAttributes[0]) ? $object->attributes->typeAttributes[0] : NULL;
4646

47-
if (!in_array($this->type, $this->defaults))
47+
if (!in_array($this->type, parent::DEFAULTS))
4848
{
4949
$dependencies[] = $this->type;
5050
}

src/PHPDraft/Model/HTTPRequest.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ private function parse_structure($objects)
103103
*
104104
* @param string $base_url URL to the base server
105105
*
106+
* @param array $additional Extra options to pass to cURL
107+
*
106108
* @return string An executable cURL command
107109
*/
108-
public function get_curl_command($base_url)
110+
public function get_curl_command($base_url, $additional = [])
109111
{
110112
$options = [];
111113

@@ -120,7 +122,8 @@ public function get_curl_command($base_url)
120122
{
121123
$options[] = '-H "'.$header.': '.$value. '"';
122124
}
123-
$options[] = '-v';
125+
$options = array_merge($options, $additional);
126+
124127
return htmlspecialchars('curl '.join(' ', $options). ' "'.$this->parent->build_url($base_url).'"');
125128
}
126129

src/PHPDraft/Model/Transition.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,12 @@ public function get_method()
169169
*
170170
* @param string $base_url base URL of the server
171171
*
172+
* @param array $additional additional arguments to pass
173+
*
172174
* @return string A cURL CLI command
173175
*/
174-
public function get_curl_command($base_url)
176+
public function get_curl_command($base_url, $additional = [])
175177
{
176-
return $this->request->get_curl_command($base_url);
178+
return $this->request->get_curl_command($base_url, $additional);
177179
}
178180
}

0 commit comments

Comments
 (0)