Skip to content

Commit bba60e3

Browse files
committed
Fix arrays
1 parent ad1bf76 commit bba60e3

8 files changed

+137
-102
lines changed

src/PHPDraft/Model/APIBlueprintElement.php

Lines changed: 3 additions & 2 deletions
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

Lines changed: 46 additions & 37 deletions
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;
@@ -123,44 +125,51 @@ function __toString()
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
}
Lines changed: 71 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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
}
File renamed without changes.
File renamed without changes.

src/PHPDraft/Out/HTML/default.php

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
2020
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
2121
<style>
22-
<?= file_get_contents(__DIR__ . '/index.css');?>
22+
<?= file_get_contents(__DIR__ . '/'.$this->template.'.css');?>
2323
</style>
2424
</head>
2525
<body>
@@ -101,11 +101,11 @@ class="pull-right <?= $this->get_method_icon($transition->get_method()); ?>"></s
101101
</div>
102102
<div class="col-md-10">
103103
<?php foreach ($base as $category): ?>
104-
<h2><a name="<?= $category->get_href(); ?>"><?= $category->title; ?></a></h2>
104+
<h2><a id="<?= $category->get_href(); ?>"><?= $category->title; ?></a></h2>
105105
<p><?= $category->description; ?></p>
106106
<?php foreach ($category->children as $resource): ?>
107107
<h3>
108-
<a name="<?= $resource->get_href(); ?>"><?= $resource->title; ?></a>
108+
<a id="<?= $resource->get_href(); ?>"><?= $resource->title; ?></a>
109109
<small><?= $resource->href; ?></small>
110110
</h3>
111111
<p><?php $resource->description; ?></p>
@@ -117,7 +117,7 @@ class="panel panel-default <?= $transition->get_method(); ?>">
117117
<var><?= $transition->get_method(); ?></var>
118118
<code><?= $transition->href; ?></code>
119119
<a class="pull-right transition-title"
120-
name="<?= $transition->get_href(); ?>"><?= $transition->title; ?></a>
120+
id="<?= $transition->get_href(); ?>"><?= $transition->title; ?></a>
121121
</h3>
122122
</div>
123123
<div class="panel-body">
@@ -163,8 +163,9 @@ class="value"><?= $value; ?></span>
163163
<?php if (!empty($transition->request->body)): ?>
164164
<h5>Body</h5>
165165
<?php foreach ($transition->request->body as $value): ?>
166-
<?php $type = (isset($transition->request->headers['Content-Type']))?$transition->request->headers['Content-Type']:NULL;?>
167-
<?= $value->print_request($type);?>
166+
<?php $type =
167+
(isset($transition->request->headers['Content-Type'])) ? $transition->request->headers['Content-Type'] : NULL; ?>
168+
<?= $value->print_request($type); ?>
168169
<?= $value ?>
169170
<?php endforeach; ?>
170171
<?php endif; ?>
@@ -173,35 +174,17 @@ class="value"><?= $value; ?></span>
173174
<?php if ($transition->url_variables !== []): ?>
174175
<h5>URI Parameters</h5>
175176
<dl class="dl-horizontal">
176-
<?php foreach ($transition->url_variables as $key => $value): ?>
177-
<?php $status =
178-
($value->status === '') ? '' : '(' . $value->status . ')'; ?>
179-
<dt><?= $key; ?></dt>
180-
<dd>
181-
<code><?= $value->type; ?></code>
182-
<?= $status; ?>
183-
<samp><?= is_array($value->value) ? join('|', $value->value) : $value->value; ?></samp>
184-
<p><?= $value->description; ?></p>
185-
</dd>
177+
<?php foreach ($transition->url_variables as $value): ?>
178+
<?= $value; ?>
186179
<?php endforeach; ?>
187180
</dl>
188181
<?php endif; ?>
189182

190183
<?php if ($transition->data_variables !== []): ?>
191184
<h5>Data object</h5>
192-
<dl class="dl-horizontal">
193-
<?php foreach ($transition->data_variables as $key => $value): ?>
194-
<?php $status =
195-
($value->status === '') ? '' : '(' . $value->status . ')'; ?>
196-
<dt><?= $key; ?></dt>
197-
<dd>
198-
<code><?= $value->type; ?></code>
199-
<?= $status; ?>
200-
<samp><?= is_array($value->value) ? join('|', $value->value) : $value->value; ?></samp>
201-
<p><?= $value->description; ?></p>
202-
</dd>
185+
<?php foreach ($transition->data_variables as $value): ?>
186+
<?= $value ?>
203187
<?php endforeach; ?>
204-
</dl>
205188
<?php endif; ?>
206189
</div>
207190
<?php if (isset($transition->responses)): ?>
@@ -231,22 +214,9 @@ class="value"><?= $value; ?></span>
231214
<?php endif; ?>
232215
<?php if ($response->structure !== []): ?>
233216
<h5>Data Structure</h5>
234-
<dl class="dl-horizontal">
235-
<?php foreach ($response->structure[0]->value as $value): ?>
236-
<dt><?= $value->key; ?></dt>
237-
<dd>
238-
<a href="#object-<?= $value->type; ?>"><?= $value->type; ?></a>
239-
<span><?= $value->description; ?></span>
240-
<?php if (isset($value->value->element) && $value->value->element === 'object')
241-
: ?>
242-
<?= $this->get_data_structure($value->value); ?>
243-
<?php else: ?>
244-
<blockquote><?= $value->value; ?></blockquote>
245-
<?php endif; ?>
246-
247-
</dd>
248-
<?php endforeach; ?>
249-
</dl>
217+
<?php foreach ($response->structure as $value): ?>
218+
<?= $value; ?>
219+
<?php endforeach; ?>
250220
<?php endif; ?>
251221
<?php foreach ($response->content as $key => $value): ?>
252222
<h5><?= $key; ?></h5>
@@ -264,7 +234,7 @@ class="value"><?= $value; ?></span>
264234
<div class="panel panel-default <?= $key; ?>">
265235
<div class="panel-heading">
266236
<h3 class="panel-title">
267-
<a name="object-<?= $key; ?>"><?= $key; ?></a>
237+
<a id="object-<?= $key; ?>"><?= $key; ?></a>
268238
</h3>
269239
</div>
270240
<div class="panel-body">
@@ -281,6 +251,6 @@ class="value"><?= $value; ?></span>
281251
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
282252
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
283253
crossorigin="anonymous"></script>
284-
<script><?= file_get_contents(__DIR__ . '/index.js');?></script>
254+
<script><?= file_get_contents(__DIR__ . '/'.$this->template.'.js'); ?></script>
285255
</body>
286256
</html>

0 commit comments

Comments
 (0)