Skip to content

Commit dd948b1

Browse files
committed
fix enum generation in the RequestBody
1 parent 90c9454 commit dd948b1

File tree

5 files changed

+84
-111
lines changed

5 files changed

+84
-111
lines changed

src/PHPDraft/Core/BaseTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
namespace PHPDraft\Core;
1010

11-
use PHPUnit_Framework_TestCase;
11+
use PHPUnit\Framework\TestCase;
1212
use ReflectionClass;
1313

1414
/**
1515
* Class BaseTest
1616
*/
17-
class BaseTest extends PHPUnit_Framework_TestCase
17+
class BaseTest extends TestCase
1818
{
1919
const FUNCTION_ID = '_phpdraftbu';
2020
/**

src/PHPDraft/Model/Elements/EnumStructureElement.php

+32-96
Original file line numberDiff line numberDiff line change
@@ -11,130 +11,66 @@
1111
use Michelf\MarkdownExtra;
1212
use PHPDraft\Model\StructureElement;
1313

14-
class EnumStructureElement implements StructureElement
14+
class EnumStructureElement extends ObjectStructureElement implements StructureElement
1515
{
1616
/**
17-
* Object description
17+
* Parse an array object
1818
*
19-
* @var string
20-
*/
21-
public $description;
22-
/**
23-
* Type of element
24-
*
25-
* @var string
26-
*/
27-
public $element = NULL;
28-
/**
29-
* Object value
19+
* @param \stdClass $object APIb Item to parse
20+
* @param array $dependencies List of dependencies build
3021
*
31-
* @var mixed
22+
* @return $this
3223
*/
33-
public $value = NULL;
34-
/**
35-
* /**
36-
* List of object dependencies
37-
*
38-
* @var string[]
39-
*/
40-
public $deps;
41-
42-
/**
43-
* Parse a JSON object to a structure
44-
*
45-
* @param \stdClass $object An object to parse
46-
* @param array $dependencies Dependencies of this object
47-
*
48-
* @return EnumStructureElement self reference
49-
*/
50-
function parse($object, &$dependencies)
24+
public function parse($object, &$dependencies)
5125
{
52-
$this->element = (isset($object->element)) ? $object->element : NULL;
53-
$this->description = (isset($object->meta->description)) ? htmlentities($object->meta->description) : NULL;
54-
if (isset($object->content) && is_array($object->content))
26+
$this->element = (isset($object->element)) ? $object->element : 'array';
27+
28+
$this->parse_common($object, $dependencies);
29+
30+
if(!isset($object->content->value->content))
31+
{
32+
$this->value = [];
33+
return $this;
34+
}
35+
36+
foreach ($object->content->value->content as $sub_item)
5537
{
56-
$deps = [];
57-
foreach ($object->content as $value) {
58-
$element = new EnumStructureElement();
59-
$this->value[] = $element->parse($value, $deps);
38+
if (!in_array($sub_item->element, self::DEFAULTS))
39+
{
40+
$dependencies[] = $sub_item->element;
6041
}
6142

62-
$this->element = $this->element . '(' . $deps[0] . ')';
63-
} else {
64-
$this->value = (isset($object->content)) ? $object->content : NULL;
43+
$this->value[$sub_item->content] = (isset($sub_item->element)) ? $sub_item->element : '';
6544
}
6645

67-
$this->description_as_html();
68-
69-
$dependencies[] = $this->element;
46+
$this->deps = $dependencies;
7047

7148
return $this;
7249
}
7350

7451
/**
75-
* Parse the description to HTML
76-
*
77-
* @return void
78-
*/
79-
public function description_as_html()
80-
{
81-
$this->description = MarkdownExtra::defaultTransform($this->description);
82-
}
83-
84-
/**
85-
* Print a string representation
52+
* Provide HTML representation
8653
*
8754
* @return string
8855
*/
8956
function __toString()
9057
{
91-
if (is_array($this->value))
92-
{
93-
$return = '<ul class="list-group">';
94-
foreach ($this->value as $item) {
95-
$return .= '<li class="list-group-item">' . $item->simple_string() . '</li>';
96-
}
97-
98-
$return .= '</ul>';
58+
$return = '<ul class="list-group">';
9959

100-
return $return;
60+
if (!is_array($this->value))
61+
{
62+
return '<span class="example-value pull-right">//list of options</span>';
10163
}
10264

103-
$type = (!in_array($this->element, self::DEFAULTS)) ?
104-
'<a class="code" href="#object-' . str_replace(' ', '-',
105-
strtolower($this->element)) . '">' . $this->element . '</a>' : '<code>' . $this->element . '</code>';
106-
$return = '<tr>' .
107-
'<td><span>' . $this->value . '</span></td>' .
108-
'<td>' . $type . '</td>' .
109-
'<td>' . $this->description . '</td>' .
110-
'</tr>';
111-
112-
return $return;
113-
}
114-
115-
function simple_string()
116-
{
117-
$return = '<span>' . $this->value . "</span>";
65+
foreach ($this->value as $key=>$item) {
66+
$type = (in_array($item, self::DEFAULTS)) ? $key : '<a href="#object-' . str_replace(' ', '-',
67+
strtolower($item)) . '">' . $key . '</a>';
11868

119-
if (!empty($this->description)) {
120-
$return .= ' - ' . $this->description;
69+
$return .= '<li class="list-group-item">' . $type . '</li>';
12170
}
12271

72+
$return .= '</ul>';
12373

12474
return $return;
12575
}
126-
127-
function strval()
128-
{
129-
if (is_array($this->value)) {
130-
$key = rand(0, count($this->value));
131-
if (is_subclass_of($this->value[$key], StructureElement::class)) {
132-
return $this->value[$key]->strval();
133-
}
134-
135-
return $this->value[$key];
136-
}
137-
138-
return $this->value;
139-
}
14076
}

src/PHPDraft/Model/Elements/ObjectStructureElement.php

+4
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ protected function parse_value_structure($object, &$dependencies)
143143
$struct = new ArrayStructureElement();
144144
$this->value = $struct->parse($object, $dependencies);
145145
break;
146+
case 'enum':
147+
$struct = new EnumStructureElement();
148+
$this->value = $struct->parse($object, $dependencies);
149+
break;
146150
case 'object':
147151
default:
148152
$value = isset($object->content->value->content) ? $object->content->value->content : NULL;

src/PHPDraft/Model/Elements/RequestBodyElement.php

+44-12
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,11 @@ public function parse($object, &$dependencies)
3535

3636
if (isset($object->content) && is_array($object->content))
3737
{
38-
foreach ($object->content as $value) {
39-
$struct = new RequestBodyElement();
40-
$this->value[] = $struct->parse($value, $dependencies);
41-
}
42-
38+
$this->parse_array_content($object, $dependencies);
4339
return $this;
4440
}
4541

4642
$this->parse_common($object, $dependencies);
47-
4843
if ($this->type === 'object')
4944
{
5045
$value = isset($object->content->value->content) ? $object->content->value : NULL;
@@ -53,16 +48,21 @@ public function parse($object, &$dependencies)
5348

5449
return $this;
5550
}
56-
57-
if ($this->type === 'array')
51+
if (in_array($this->type, ['object', 'array', 'enum'], TRUE) || !in_array($this->type, self::DEFAULTS, TRUE))
5852
{
59-
$this->value = new ArrayStructureElement();
60-
$this->value = $this->value->parse($object, $dependencies);
61-
53+
$this->parse_value_structure($object, $dependencies);
6254
return $this;
6355
}
6456

65-
$this->value = isset($object->content->value->content) ? $object->content->value->content : NULL;
57+
if (isset($object->content->value->content))
58+
{
59+
$this->value = $object->content->value->content;
60+
} elseif (isset($object->content->value->attributes->samples))
61+
{
62+
$this->value = join(' | ', $object->content->value->attributes->samples);
63+
} else {
64+
$this->value = NULL;
65+
}
6666

6767
return $this;
6868
}
@@ -116,6 +116,38 @@ public function print_request($type = 'application/x-www-form-urlencoded')
116116
}
117117
}
118118

119+
/**
120+
* Parse $this->value as a structure based on given content
121+
*
122+
* @param mixed $object APIB content
123+
* @param array $dependencies Object dependencies
124+
*
125+
* @return void
126+
*/
127+
protected function parse_value_structure($object, &$dependencies)
128+
{
129+
switch ($this->type) {
130+
case 'array':
131+
$struct = new ArrayStructureElement();
132+
$this->value = $struct->parse($object, $dependencies);
133+
break;
134+
case 'enum':
135+
$struct = new EnumStructureElement();
136+
$this->value = $struct->parse($object, $dependencies);
137+
break;
138+
case 'object':
139+
default:
140+
$value = isset($object->content->value->content) ? $object->content->value->content : NULL;
141+
$struct = new RequestBodyElement();
142+
143+
$this->value = $struct->parse($value, $dependencies);
144+
break;
145+
}
146+
147+
unset($struct);
148+
unset($value);
149+
}
150+
119151
/**
120152
*
121153
* @return string

src/PHPDraft/Parse/Tests/JsonToHTMLTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace PHPDraft\Parse\Tests;
1010

11+
use PHPDraft\Core\BaseTest;
1112
use PHPDraft\Parse\JsonToHTML;
1213
use PHPUnit_Framework_TestCase;
1314
use ReflectionClass;
@@ -16,7 +17,7 @@
1617
* Class JsonToHTMLTest
1718
* @covers PHPDraft\Parse\JsonToHTML
1819
*/
19-
class JsonToHTMLTest extends PHPUnit_Framework_TestCase
20+
class JsonToHTMLTest extends BaseTest
2021
{
2122
/**
2223
* Test Class

0 commit comments

Comments
 (0)