Skip to content

Commit 1525c48

Browse files
committed
Fix #15
1 parent 7088e37 commit 1525c48

14 files changed

+121
-62
lines changed

config.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/PHPDraft/Model/Category.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
namespace PHPDraft\Model;
1010

11-
class Category extends APIBlueprintElement
11+
use PHPDraft\Model\Elements\DataStructureElement;
12+
13+
class Category extends HierarchyElement
1214
{
1315
/**
1416
* API Structure element

src/PHPDraft/Model/Elements/ArrayStructureElement.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
namespace PHPDraft\Model\Elements;
1010

11-
12-
use PHPDraft\Model\DataStructureElement;
13-
use PHPDraft\Model\EnumStructureElement;
1411
use PHPDraft\Model\StructureElement;
1512

1613
class ArrayStructureElement extends DataStructureElement implements StructureElement
@@ -60,7 +57,7 @@ function __toString()
6057
foreach ($this->type as $key => $item)
6158
{
6259
$type =
63-
(in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . $item . '">' . $item . '</a>';
60+
(in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . str_replace(' ', '-', strtolower($item)) . '">' . $item . '</a>';
6461

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

src/PHPDraft/Model/DataStructureElement.php renamed to src/PHPDraft/Model/Elements/DataStructureElement.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
/**
33
* This file contains the DataStructureElement.php
44
*
5-
* @package PHPDraft\Model
5+
* @package PHPDraft\Model\Elements
66
* @author Sean Molenaar<[email protected]>
77
*/
88

9-
namespace PHPDraft\Model;
9+
namespace PHPDraft\Model\Elements;
1010

11-
use PHPDraft\Model\Elements\ArrayStructureElement;
11+
use PHPDraft\Model\StructureElement;
1212

1313
class DataStructureElement implements StructureElement
1414
{
@@ -157,7 +157,7 @@ function __toString()
157157
}
158158

159159
$type = (!in_array($this->type, self::DEFAULTS)) ?
160-
'<a class="code" href="#object-' . $this->type . '">' . $this->type . '</a>' : '<code>' . $this->type . '</code>';
160+
'<a class="code" href="#object-' . str_replace(' ', '-', strtolower($this->type)). '">' . $this->type . '</a>' : '<code>' . $this->type . '</code>';
161161

162162
if (empty($this->value))
163163
{

src/PHPDraft/Model/EnumStructureElement.php renamed to src/PHPDraft/Model/Elements/EnumStructureElement.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
* @author Sean Molenaar<[email protected]>
77
*/
88

9-
namespace PHPDraft\Model;
9+
namespace PHPDraft\Model\Elements;
1010

11-
12-
use PHPDraft\Model\Elements\ArrayStructureElement;
11+
use PHPDraft\Model\StructureElement;
1312

1413
class EnumStructureElement implements StructureElement
1514
{
@@ -69,7 +68,7 @@ function parse($item, &$dependencies)
6968
function __toString()
7069
{
7170
$type = (!in_array($this->element, self::DEFAULTS)) ?
72-
'<a class="code" href="#object-' . $this->element . '">' . $this->element . '</a>' : '<code>' . $this->element . '</code>';
71+
'<a class="code" href="#object-' . str_replace(' ', '-', strtolower($this->element)) . '">' . $this->element . '</a>' : '<code>' . $this->element . '</code>';
7372
$return = '<tr>' .
7473
'<td><span>' . $this->value . '</span></td>' .
7574
'<td>'.$type.'</td>' .

src/PHPDraft/Model/Elements/RequestBodyElement.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace PHPDraft\Model\Elements;
1010

11-
use PHPDraft\Model\DataStructureElement;
11+
use PHPDraft\Model\StructureElement;
1212

13-
class RequestBodyElement extends DataStructureElement
13+
class RequestBodyElement extends DataStructureElement implements StructureElement
1414
{
1515
/**
1616
* Parse a JSON object to a data structure
@@ -58,6 +58,13 @@ public function parse($object, &$dependencies)
5858
return $this;
5959
}
6060

61+
if ($this->type === 'array')
62+
{
63+
$this->value = '[]';
64+
65+
return $this;
66+
}
67+
6168
$this->value = isset($object->content->value->content) ? $object->content->value->content : NULL;
6269

6370
return $this;
@@ -86,7 +93,7 @@ public function print_request($type = 'application/x-www-form-urlencoded')
8693

8794
switch ($type) {
8895
case 'application/x-www-form-urlencoded':
89-
$return .= join('&amp;', $list);
96+
$return .= join('&', $list);
9097
break;
9198
default:
9299
$return .= join(PHP_EOL, $list);
@@ -113,4 +120,15 @@ public function print_request($type = 'application/x-www-form-urlencoded')
113120
break;
114121
}
115122
}
123+
124+
/**
125+
*
126+
* @return string
127+
*/
128+
function __toString()
129+
{
130+
return parent::__toString();
131+
}
132+
133+
116134
}

src/PHPDraft/Model/Tests/DataStructureElementTest.php renamed to src/PHPDraft/Model/Elements/Tests/DataStructureElementTest.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function testSuccesfulParse($object, $expected)
4040
{
4141
$dep = [];
4242
$this->class->parse(json_decode($object), $dep);
43+
var_dump($this->class);
4344
$this->assertSame($this->class->key, $expected->key);
4445
$this->assertSame($this->class->value, $expected->value);
4546
$this->assertSame($this->class->element, $expected->element);
@@ -82,12 +83,13 @@ public function parseObjectProvider()
8283
$base2->type = 'Struct1';
8384

8485
$return[] = [
85-
'{"element": "member","content": {"key": {"element": "string","content": "Content-Type"},' .
86-
'"value": {"element": "Struct2","content": "application/json"}}}',
86+
'{"element":"object","content":[{"element":"member","meta":{"description":"API version with optional client
87+
architecture identifier"},"content":{"key":{"element":"string"
88+
,"content":"version"},"value":{"element":"Struct2","content":"120.a"}}}',
8789
$base1,
8890
];
8991
$return[] = [
90-
'{"element": "member","content": {"key": {"element": "string","content": "Auth2"},' .
92+
'{"element": "object","content": {"key": {"element": "string","content": "Auth2"},' .
9193
'"value": {"element": "Struct1","content": "something"}}}',
9294
$base2,
9395
];
@@ -103,17 +105,17 @@ public function parseObjectDepProvider()
103105
{
104106
$return = [];
105107
$return[] = [
106-
'{"element": "member","content": {"key": {"element": "string","content": "Content-Type"}' .
107-
',"value": {"element": "Struct2","content": "application/json"}}}',
108+
'{"element":"object","content":[{"element":"member","meta":{"description":"API version"},
109+
"content":{"key":{"element":"string","content":"version"},"value":{"element":"Struct2","content":"120.a"}}}',
108110
['Struct2'],
109111
];
110112
$return[] = [
111-
'{"element": "member","content": {"key": {"element": "string","content": "Auth2"}' .
112-
',"value": {"element": "Struct1","content": "something"}}}',
113-
['Struct1'],
113+
'{"element":"member","content":{"key":{"element":"string","content":"flight_list"},
114+
"value":{"element":"array","content":[{"element":"Flight"}]}}}',
115+
['Flight'],
114116
];
115117
$return[] = [
116-
'{"element": "member",
118+
'{"element": "object",
117119
"meta": {"description": "Update Data Object"},
118120
"content": {
119121
"key": {"element": "string","content": "data"},

src/PHPDraft/Model/HTTPRequest.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ class HTTPRequest
3232

3333
/**
3434
* Body of the request (if POST or PUT)
35+
* @var mixed
36+
*/
37+
public $body = NULL;
38+
39+
40+
/**
41+
* Structure of the request (if POST or PUT)
3542
* @var RequestBodyElement[]
3643
*/
37-
public $body = [];
44+
public $struct = [];
3845

3946
/**
4047
* HTTPRequest constructor.
@@ -66,6 +73,15 @@ public function parse($object)
6673
$this->parse_structure($value->content);
6774
continue;
6875
}
76+
elseif ($value->element === 'asset')
77+
{
78+
if (in_array('messageBody', $value->meta->classes))
79+
{
80+
$this->body[] = (isset($value->content)) ? $value->content : NULL;
81+
$this->headers['Content-Type'] =
82+
(isset($value->attributes->contentType)) ? $value->attributes->contentType : '';
83+
}
84+
}
6985
}
7086
}
7187

@@ -77,6 +93,11 @@ public function parse($object)
7793
}
7894
}
7995

96+
if ($this->body === NULL)
97+
{
98+
$this->body = &$this->struct;
99+
}
100+
80101
return $this;
81102
}
82103

@@ -94,14 +115,14 @@ private function parse_structure($objects)
94115
$struct->parse($object, $deps);
95116
$struct->deps = $deps;
96117

97-
$this->body[] = $struct;
118+
$this->struct[] = $struct;
98119
}
99120
}
100121

101122
/**
102123
* Generate a cURL command for the HTTP request
103124
*
104-
* @param string $base_url URL to the base server
125+
* @param string $base_url URL to the base server
105126
*
106127
* @param array $additional Extra options to pass to cURL
107128
*
@@ -111,20 +132,27 @@ public function get_curl_command($base_url, $additional = [])
111132
{
112133
$options = [];
113134

114-
$type = (isset($this->headers['Content-Type']))?$this->headers['Content-Type']:NULL;
135+
$type = (isset($this->headers['Content-Type'])) ? $this->headers['Content-Type'] : NULL;
115136

116-
$options[] = '-X'.$this->method;
117-
foreach ($this->body as $body)
137+
$options[] = '-X' . $this->method;
138+
if (is_string($this->body))
139+
{
140+
$options[] = '--data-binary "' . $this->body . '"';
141+
}
142+
else
118143
{
119-
$options[] = '--data-binary "'.strip_tags($body->print_request($type)).'"';
144+
foreach ($this->struct as $body)
145+
{
146+
$options[] = '--data-binary "' . strip_tags($body->print_request($type)) . '"';
147+
}
120148
}
121-
foreach ($this->headers as $header=>$value)
149+
foreach ($this->headers as $header => $value)
122150
{
123-
$options[] = '-H "'.$header.': '.$value. '"';
151+
$options[] = '-H "' . $header . ': ' . $value . '"';
124152
}
125153
$options = array_merge($options, $additional);
126154

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

130158

src/PHPDraft/Model/HTTPResponse.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace PHPDraft\Model;
1010

11+
use PHPDraft\Model\Elements\DataStructureElement;
12+
1113
class HTTPResponse
1214
{
1315
/**

src/PHPDraft/Model/APIBlueprintElement.php renamed to src/PHPDraft/Model/HierarchyElement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use Michelf\Markdown;
1212

13-
abstract class APIBlueprintElement
13+
abstract class HierarchyElement
1414
{
1515
/**
1616
* Title of the element
@@ -29,14 +29,14 @@ abstract class APIBlueprintElement
2929
/**
3030
* Child elements
3131
*
32-
* @var APIBlueprintElement[]
32+
* @var HierarchyElement[]
3333
*/
3434
public $children = [];
3535

3636
/**
3737
* Parent Element
3838
*
39-
* @var APIBlueprintElement|NULL
39+
* @var HierarchyElement|NULL
4040
*/
4141
protected $parent = NULL;
4242

src/PHPDraft/Model/Resource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace PHPDraft\Model;
1010

11-
class Resource extends APIBlueprintElement
11+
class Resource extends HierarchyElement
1212
{
1313
/**
1414
* Location relative to the base URL

src/PHPDraft/Model/Transition.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
namespace PHPDraft\Model;
1010

11-
class Transition extends APIBlueprintElement
11+
use PHPDraft\Model\Elements\DataStructureElement;
12+
13+
class Transition extends HierarchyElement
1214
{
1315
/**
1416
* HTTP method used

src/PHPDraft/Out/HTML/default.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ body .col-md-10 h3:first-child
6868
line-height: 22px;
6969
}
7070

71-
.POST > .panel-heading, span.POST {
71+
.POST:not(.structure) > .panel-heading, span.POST {
7272
background: #62c462;
7373
}
7474

75-
.GET > .panel-heading, span.GET {
75+
.GET:not(.structure) > .panel-heading, span.GET {
7676
background: #5bc0de;
7777
}
7878

79-
.DELETE > .panel-heading, span.DELETE {
79+
.DELETE:not(.structure) > .panel-heading, span.DELETE {
8080
background: #ee5f5b;
8181
}
8282

83-
.PUT > .panel-heading, span.PUT {
83+
.PUT:not(.structure) > .panel-heading, span.PUT {
8484
background: #f89406;
8585
}
8686

0 commit comments

Comments
 (0)