Skip to content

Commit 1754c93

Browse files
authored
Merge pull request #18 from SMillerDev/feature/enums
Feature/enums
2 parents 2d43667 + f5fa874 commit 1754c93

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+5029
-99
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
# JIRA plugin
1111
atlassian-ide-plugin.xml
1212

13-
config.json
13+
*.pem

config.json

-4
This file was deleted.

config.json.sample

-4
This file was deleted.

index.php

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Set up include path for source handling
44
*/
55
set_include_path(get_include_path().":".__DIR__.'/src/');
6-
$config = json_decode(file_get_contents(__DIR__."/config.json"));
76

87
/**
98
* Set up required classes (with the autoloader)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* This file contains the ApibFileParserTest.php
4+
*
5+
* @package php-drafter\SOMETHING
6+
* @author Sean Molenaar<[email protected]>
7+
*/
8+
9+
namespace PHPDraft\In\Tests;
10+
11+
12+
use PHPDraft\Core\TestBase;
13+
use PHPDraft\In\ApibFileParser;
14+
use ReflectionClass;
15+
16+
class ApibFileParserTest extends TestBase
17+
{
18+
public function setUp()
19+
{
20+
$this->class = new ApibFileParser(__DIR__.'/ApibFileParserTest.php');
21+
$this->reflection = new ReflectionClass('PHPDraft\In\ApibFileParser');
22+
}
23+
24+
public function tearDown()
25+
{
26+
unset($this->class);
27+
unset($this->reflection);
28+
}
29+
30+
public function testSetup()
31+
{
32+
$property = $this->reflection->getProperty('location');
33+
$property->setAccessible(TRUE);
34+
$this->assertSame(__DIR__.'/', $property->getValue($this->class));
35+
}
36+
37+
38+
}

src/PHPDraft/Model/Category.php

+3-1
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

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88

99
namespace PHPDraft\Model\Elements;
1010

11+
use PHPDraft\Model\StructureElement;
1112

12-
use PHPDraft\Model\DataStructureElement;
13-
14-
class ArrayStructureElement extends DataStructureElement
13+
class ArrayStructureElement extends DataStructureElement implements StructureElement
1514
{
1615

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

2321
if (isset($item->content))
@@ -35,6 +33,10 @@ public function parse($item, &$dependencies)
3533
$value = new DataStructureElement();
3634
$this->value[$key] = $value->parse($sub_item, $dependencies);
3735
break;
36+
case 'enum':
37+
$value = new EnumStructureElement();
38+
$this->value[$key] = $value->parse($sub_item, $dependencies);
39+
break;
3840
default:
3941
$this->value[$key] = (isset($sub_item->content)) ? $sub_item->content : NULL;
4042
break;
@@ -55,7 +57,7 @@ function __toString()
5557
foreach ($this->type as $key => $item)
5658
{
5759
$type =
58-
(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>';
5961

6062
$value =
6163
(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

+34-13
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
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

13-
class DataStructureElement
13+
class DataStructureElement implements StructureElement
1414
{
15-
/**
16-
* Default datatypes
17-
* @var array
18-
*/
19-
const DEFAULTS = ['boolean', 'string', 'number', 'object', 'array'];
15+
2016
/**
2117
* Object key
2218
* @var string
@@ -68,17 +64,26 @@ public function parse($object, &$dependencies)
6864
return $this;
6965
}
7066
$this->element = $object->element;
67+
7168
if (isset($object->content) && is_array($object->content))
7269
{
7370
foreach ($object->content as $value)
7471
{
75-
$struct = new DataStructureElement();
72+
if (in_array($this->element, ['object', 'dataStructure']))
73+
{
74+
$struct = new DataStructureElement();
75+
}
76+
else
77+
{
78+
$struct = new EnumStructureElement();
79+
}
7680
$this->value[] = $struct->parse($value, $dependencies);
7781
}
7882

7983
return $this;
8084
}
8185

86+
8287
$this->key = $object->content->key->content;
8388
$this->type = $object->content->value->element;
8489
$this->description = isset($object->meta->description) ? $object->meta->description : NULL;
@@ -90,13 +95,17 @@ public function parse($object, &$dependencies)
9095
$dependencies[] = $this->type;
9196
}
9297

93-
if ($this->type === 'object' || $this->type === 'array')
98+
if ($this->type === 'object' || $this->type === 'array' || $this->type === 'enum' || !in_array($this->type, self::DEFAULTS))
9499
{
95100
$value = isset($object->content->value->content) ? $object->content->value : NULL;
96101
if ($this->type === 'array')
97102
{
98103
$this->value = new ArrayStructureElement();
99104
}
105+
elseif ($this->type === 'enum')
106+
{
107+
$this->value = new EnumStructureElement();
108+
}
100109
else
101110
{
102111
$this->value = new DataStructureElement();
@@ -128,7 +137,15 @@ function __toString()
128137
$return = '<table class="table table-striped">';
129138
foreach ($this->value as $object)
130139
{
131-
if (is_string($object) || get_class($object) === self::class || get_class($object) === ArrayStructureElement::class)
140+
if (get_class($object) === \stdClass::class)
141+
{
142+
return json_encode($object);
143+
}
144+
if (is_string($object)
145+
|| get_class($object) === self::class
146+
|| get_class($object) === ArrayStructureElement::class
147+
|| get_class($object) === EnumStructureElement::class
148+
)
132149
{
133150
$return .= $object;
134151
}
@@ -140,7 +157,7 @@ function __toString()
140157
}
141158

142159
$type = (!in_array($this->type, self::DEFAULTS)) ?
143-
'<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>';
144161

145162
if (empty($this->value))
146163
{
@@ -156,6 +173,10 @@ function __toString()
156173
{
157174
$value = '<div class="array-struct">' . $this->value . '</div>';
158175
}
176+
elseif (is_array($this->value) && (EnumStructureElement::class === get_class($this->value[0])))
177+
{
178+
$value = '<div class="enum-struct">' . $this->value . '</div>';
179+
}
159180
else
160181
{
161182
$value = '<span class="example-value pull-right">' . $this->value . '</span>';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* This file contains the ${FILE_NAME}
4+
*
5+
* @package php-drafter\SOMETHING
6+
* @author Sean Molenaar<[email protected]>
7+
*/
8+
9+
namespace PHPDraft\Model\Elements;
10+
11+
use PHPDraft\Model\StructureElement;
12+
13+
class EnumStructureElement implements StructureElement
14+
{
15+
/**
16+
* Object description
17+
* @var string
18+
*/
19+
public $description;
20+
/**
21+
* Type of element
22+
* @var string
23+
*/
24+
public $element = NULL;
25+
/**
26+
* Object value
27+
* @var mixed
28+
*/
29+
public $value = NULL;
30+
/**
31+
* Object status (required|optional)
32+
* @var string
33+
*/
34+
public $status = '';
35+
/**
36+
* List of object dependencies
37+
* @var string[]
38+
*/
39+
public $deps;
40+
41+
/**
42+
* Parse a JSON object to a structure
43+
*
44+
* @param \stdClass $item An object to parse
45+
* @param array $dependencies Dependencies of this object
46+
*
47+
* @return EnumStructureElement self reference
48+
*/
49+
function parse($item, &$dependencies)
50+
{
51+
$this->element = (isset($item->element)) ? $item->element : NULL;
52+
$this->description = (isset($item->meta->description)) ? $item->meta->description : NULL;
53+
$this->value = (isset($item->content)) ? $item->content : NULL;
54+
55+
if (!in_array($this->element, self::DEFAULTS))
56+
{
57+
$dependencies[] = $this->element;
58+
}
59+
60+
return $this;
61+
}
62+
63+
/**
64+
* Print a string representation
65+
*
66+
* @return string
67+
*/
68+
function __toString()
69+
{
70+
$type = (!in_array($this->element, self::DEFAULTS)) ?
71+
'<a class="code" href="#object-' . str_replace(' ', '-', strtolower($this->element)) . '">' . $this->element . '</a>' : '<code>' . $this->element . '</code>';
72+
$return = '<tr>' .
73+
'<td><span>' . $this->value . '</span></td>' .
74+
'<td>'.$type.'</td>' .
75+
'<td><span>' . $this->description . '</span></td>' .
76+
'</tr>';
77+
return $return;
78+
}
79+
80+
81+
}

src/PHPDraft/Model/Elements/RequestBodyElement.php

+21-3
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
}

0 commit comments

Comments
 (0)