Skip to content

Commit

Permalink
Fix array and enum parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Apr 7, 2020
1 parent 697de00 commit 19a6438
Show file tree
Hide file tree
Showing 21 changed files with 621 additions and 640 deletions.
1 change: 1 addition & 0 deletions phpdraft
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use PHPDraft\Parse\ParserFactory;
use PHPDraft\Parse\ResourceException;

define('VERSION', '0');
#define('ID_STATIC', 'SOME_ID');
try
{
// Define the cli options.
Expand Down
3 changes: 2 additions & 1 deletion src/PHPDraft/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace PHPDraft\Model;

use PHPDraft\Model\Elements\BasicStructureElement;
use PHPDraft\Model\Elements\ObjectStructureElement;
use stdClass;

Expand Down Expand Up @@ -54,7 +55,7 @@ public function parse(stdClass $object)
break;
case 'dataStructure':
$deps = [];
$struct = new ObjectStructureElement();
$struct = BasicStructureElement::get_class($item->content);
$struct->deps = $deps;
$struct->parse($item->content, $deps);

Expand Down
22 changes: 8 additions & 14 deletions src/PHPDraft/Model/Elements/ArrayStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public function parse(?object $object, array &$dependencies): StructureElement

$this->parse_common($object, $dependencies);

if (!isset($object->content->value->content)) {
if (!isset($object->content)) {
$this->value = [];

return $this;
}

foreach ($object->content->value->content as $sub_item) {
foreach ($object->content as $sub_item) {
if (!in_array($sub_item->element, self::DEFAULTS)) {
$dependencies[] = $sub_item->element;
}
Expand All @@ -58,28 +58,22 @@ public function parse(?object $object, array &$dependencies): StructureElement
*/
public function __toString(): string
{
$return = '<ul class="list-group mdl-list">';
if (is_string($this->value)) {
$type = $this->get_element_as_html($this->element);

if (!is_array($this->value)) {
return '<span class="example-value pull-right">[ ]</span>';
return '<tr><td>' . $this->key . '</td><td>' . $type . '</td><td>' . $this->description . '</td></tr>';
}

$return = '';
foreach ($this->value as $item) {
$value = key($item);
$key = $item[$value];
$type = (in_array($key, self::DEFAULTS)) ? "<code>$key</code>" : '<a href="#object-' . str_replace(
' ',
'-',
strtolower($key)
) . '">' . $key . '</a>';
$type = $this->get_element_as_html($item[$value]);

$value = empty($value) ? '' : " - <span class=\"example-value pull-right\">$value</span>";
$return .= '<li class="list-group-item mdl-list__item">' . $type . $value . '</li>';
}

$return .= '</ul>';

return $return;
return '<ul class="list-group mdl-list">' . $return . '</ul>';
}

/**
Expand Down
52 changes: 47 additions & 5 deletions src/PHPDraft/Model/Elements/BasicStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected function parse_common(object $object, array &$dependencies): void

$this->description_as_html();

if (!in_array($this->type, self::DEFAULTS)) {
if (!in_array($this->type, self::DEFAULTS) && $this->type !== NULL) {
$dependencies[] = $this->type;
}
}
Expand All @@ -145,6 +145,23 @@ public function description_as_html(): void
$this->description = MarkdownExtra::defaultTransform($this->description);
}

/**
* Represent the element in HTML.
*
* @param string $element Element name
*
* @return string HTML string
*/
protected function get_element_as_html($element): string
{
if (in_array($element, self::DEFAULTS)) {
return '<code>' . $element . '</code>';
}

$link_name = str_replace(' ', '-', strtolower($element));
return '<a class="code" title="' . $element . '" href="#object-' . $link_name . '">' . $element . '</a>';
}

/**
* Get a string representation of the value.
*
Expand All @@ -155,17 +172,42 @@ public function description_as_html(): void
public function string_value($flat = FALSE)
{
if (is_array($this->value)) {
$key = rand(0, count($this->value));
if (is_subclass_of($this->value[$key], StructureElement::class) && $flat === FALSE) {
return $this->value[$key]->string_value($flat);
$value_key = rand(0, count($this->value));
if (is_subclass_of($this->value[$value_key], StructureElement::class) && $flat === FALSE) {
return $this->value[$value_key]->string_value($flat);
}

return $this->value[$key];
return $this->value[$value_key];
}

if (is_subclass_of($this->value, BasicStructureElement::class) && $flat === TRUE) {
return is_array($this->value->value) ? array_keys($this->value->value)[0] : $this->value->value;
}
return $this->value;
}

/**
* Get what element to parse with.
*
* @param object $object The object to parse.
*
* @return BasicStructureElement The element to parse to
*/
public static function get_class(object $object): BasicStructureElement
{
switch ($object->element) {
default:
case 'object':
$struct = new ObjectStructureElement();
break;
case 'array':
$struct = new ArrayStructureElement();
break;
case 'enum':
$struct = new EnumStructureElement();
break;
}

return $struct;
}
}
56 changes: 30 additions & 26 deletions src/PHPDraft/Model/Elements/EnumStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,40 @@ class EnumStructureElement extends BasicStructureElement
*/
public function parse(?object $object, array &$dependencies): StructureElement
{
$this->element = (isset($object->element)) ? $object->element : 'enum';
$this->element = $object->element;

$this->parse_common($object, $dependencies);

$this->key = $this->key ?? $object->content ?? 'UNKNOWN';
$this->type = $this->type ?? $object->element;
$this->key = $this->key ?? $object->content->content ?? NULL;
$this->type = $this->type ?? $object->content->element ?? NULL;

if (!isset($object->content->value->content)) {
if (!isset($object->content) && !isset($object->attributes)) {
$this->value = $this->key;

return $this;
}

$enumerations = $object->content->value->attributes->enumerations->content ?? $object->content->value->content;
foreach ($enumerations as $sub_item) {
if (isset($object->attributes->default)) {
if (!in_array($object->attributes->default->content->element ?? '', self::DEFAULTS)) {
$dependencies[] = $object->attributes->default->content->element;
}
$this->value = $object->attributes->default->content->content;
$this->deps = $dependencies;

return $this;
}

if (isset($object->content)) {
if (!in_array($object->content->element, self::DEFAULTS)) {
$dependencies[] = $object->content->element;
}
$this->value = $object->content->content;
$this->deps = $dependencies;

return $this;
}

foreach ($object->attributes->enumerations->content as $sub_item) {
if (!in_array($sub_item->element, self::DEFAULTS)) {
$dependencies[] = $sub_item->element;
}
Expand All @@ -57,36 +76,21 @@ public function parse(?object $object, array &$dependencies): StructureElement
*/
public function __toString(): string
{
$return = '<ul class="list-group mdl-list">';

if (is_string($this->value)) {
$type = (in_array($this->element, self::DEFAULTS)) ? $this->element : '<a href="#object-' . str_replace(
' ',
'-',
strtolower($this->element)
) . '">' . $this->element . '</a>';
$type = $this->get_element_as_html($this->element);

return '<tr><td>' . $this->key . '</td><td><code>' . $type . '</code></td><td>' . $this->description . '</td></tr>';
}

if (!is_array($this->value)) {
return '<span class="example-value pull-right">//list of options</span>';
return '<tr><td>' . $this->key . '</td><td>' . $type . '</td><td>' . $this->description . '</td></tr>';
}

$return = '';
foreach ($this->value as $value => $key) {
$type = (in_array($key, self::DEFAULTS)) ? "<code>$key</code>" : '<a href="#object-' . str_replace(
' ',
'-',
strtolower($key)
) . '">' . $key . '</a>';
$type = $type = $this->get_element_as_html($key);

$item = empty($value) ? '' : " - <span class=\"example-value pull-right\">$value</span>";
$return .= '<li class="list-group-item mdl-list__item">' . $type . $item . '</li>';
}

$return .= '</ul>';

return $return;
return '<ul class="list-group mdl-list">' . $return . '</ul>';
}

/**
Expand Down
66 changes: 30 additions & 36 deletions src/PHPDraft/Model/Elements/ObjectStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public function parse(?object $object, array &$dependencies): StructureElement

$this->element = $object->element;
$this->parse_common($object, $dependencies);
if (!isset($object->content) && isset($object->meta)) {
return $this;
}

if (isset($object->content) && is_array($object->content)) {
$this->parse_array_content($object, $dependencies);
Expand Down Expand Up @@ -74,25 +71,30 @@ public function parse(?object $object, array &$dependencies): StructureElement
/**
* Parse $this->value as a structure based on given content.
*
* @param mixed $value APIB content
* @param array $dependencies Object dependencies
* @param object $object APIB content
* @param array $dependencies Object dependencies
*
* @return void
*/
protected function parse_value_structure($value, array &$dependencies)
protected function parse_value_structure(object $object, array &$dependencies)
{
switch ($this->type) {
$type = $this->element === 'member' ? $this->type : $this->element;
if (!isset($object->content->value) && !isset($object->attributes->enumerations)) {
return;
}

$value = $object->content->value ?? $object;
switch ($type) {
default:
case 'object':
$struct = $this->new_instance();
break;
case 'array':
$struct = new ArrayStructureElement();
break;
case 'enum':
$struct = new EnumStructureElement();
break;
default:
case 'object':
$value = $value->content->value ?? null;
$struct = $this->new_instance();
break;
}
$this->value = $struct->parse($value, $dependencies);

Expand All @@ -113,25 +115,26 @@ protected function new_instance(): StructureElement
/**
* Parse content formed as an array.
*
* @param mixed $object APIB content
* @param array $dependencies Object dependencies
* @param object|null $object APIB content
* @param array $dependencies Object dependencies
*
* @return void
*/
protected function parse_array_content($object, array &$dependencies): void
protected function parse_array_content(?object $object, array &$dependencies): void
{
foreach ($object->content as $value) {
switch ($this->type){
$type = $this->element === 'member' ? $this->type : $this->element;
switch ($type){
default:
case 'object':
$struct = $this->new_instance();
break;
case 'enum':
$struct = new EnumStructureElement();
break;
case 'array':
$struct = new ArrayStructureElement();
break;
default:
case 'object':
$struct = $this->new_instance();
break;
}

$this->value[] = $struct->parse($value, $dependencies);
Expand All @@ -154,24 +157,22 @@ public function __toString(): string
}

if (is_array($this->value)) {
$return = '<table class="table table-striped mdl-data-table mdl-js-data-table ">';
$return = '';
foreach ($this->value as $object) {
if (is_string($object) || is_subclass_of(get_class($object), BasicStructureElement::class)) {
if (is_string($object) || is_subclass_of(get_class($object), StructureElement::class)) {
$return .= $object;
}
}

$return .= '</table>';

return $this->description . $return;
return "<table class=\"table table-striped mdl-data-table mdl-js-data-table \">$return</table>";
}

if ($this->ref !== null) {
return '<p>Inherits from <a href="#object-' . strtolower($this->ref) . '">' . $this->ref . '</a></p>' . $this->description;
return '<p>Inherits from <a href="#object-' . strtolower($this->ref) . '">' . $this->ref . '</a></p>';
}

if ($this->value === null && $this->key === null && $this->description !== null) {
return "<div class='description clearfix'>$this->description</div>";
return '';
}

if ($this->value === null && $this->key === null && $this->description === null) {
Expand Down Expand Up @@ -218,15 +219,8 @@ protected function construct_string_return(string $value): string
if ($this->type === NULL) {
return $value;
}
if (!in_array($this->type, self::DEFAULTS)) {
$type = '<a class="code" href="#object-' . str_replace(
' ',
'-',
strtolower($this->type)
) . '">' . $this->type . '</a>';
} else {
$type = '<code>' . $this->type . '</code>';
}

$type = $this->get_element_as_html($this->type);
$variable = '';
if ($this->is_variable) {
$variable = '<span class="fas fa-info variable-info" data-toggle="tooltip" data-placement="top" title="This is a variable key"></span>';
Expand Down
Loading

0 comments on commit 19a6438

Please sign in to comment.