From bba60e33cea9d338a89d8bc7b6ca8057d382c8c2 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Mon, 5 Sep 2016 09:16:38 +0200 Subject: [PATCH 1/2] Fix arrays --- src/PHPDraft/Model/APIBlueprintElement.php | 5 +- src/PHPDraft/Model/DataStructureElement.php | 83 ++++++++++--------- .../Model/Elements/ArrayStructureElement.php | 71 ++++++++++++++++ .../Model/Elements/RequestBodyElement.php | 2 +- .../Out/HTML/{index.css => default.css} | 0 .../Out/HTML/{index.js => default.js} | 0 src/PHPDraft/Out/HTML/default.php | 62 ++++---------- src/PHPDraft/Out/TemplateGenerator.php | 16 ---- 8 files changed, 137 insertions(+), 102 deletions(-) create mode 100644 src/PHPDraft/Model/Elements/ArrayStructureElement.php rename src/PHPDraft/Out/HTML/{index.css => default.css} (100%) rename src/PHPDraft/Out/HTML/{index.js => default.js} (100%) diff --git a/src/PHPDraft/Model/APIBlueprintElement.php b/src/PHPDraft/Model/APIBlueprintElement.php index b6b1aec8..d20d7be3 100644 --- a/src/PHPDraft/Model/APIBlueprintElement.php +++ b/src/PHPDraft/Model/APIBlueprintElement.php @@ -81,8 +81,9 @@ function parse($object) */ public function get_href() { - $prep = ($this->parent !== NULL) ? $this->parent->get_href() . '-' : ''; + $seperator = '-'; + $prep = ($this->parent !== NULL) ? $this->parent->get_href() . $seperator : ''; - return $prep . str_replace(' ', '-', strtolower($this->title)); + return $prep . str_replace(' ', $seperator, strtolower($this->title)); } } \ No newline at end of file diff --git a/src/PHPDraft/Model/DataStructureElement.php b/src/PHPDraft/Model/DataStructureElement.php index 4ba552fa..796dfc89 100644 --- a/src/PHPDraft/Model/DataStructureElement.php +++ b/src/PHPDraft/Model/DataStructureElement.php @@ -8,56 +8,51 @@ namespace PHPDraft\Model; +use PHPDraft\Model\Elements\ArrayStructureElement; + class DataStructureElement { + /** + * Default datatypes + * @var array + */ + const DEFAULTS = ['boolean', 'string', 'number', 'object', 'array']; /** * Object key * @var string */ public $key; - /** * Object JSON type - * @var string + * @var mixed */ public $type; - /** * Object description * @var string */ public $description; - /** * Type of element * @var string */ public $element = NULL; - /** * Object value * @var mixed|DataStructureElement[] */ public $value = NULL; - /** * Object status (required|optional) * @var string */ public $status = ''; - /** * List of object dependencies * @var string[] */ public $deps; - /** - * Default datatypes - * @var array - */ - protected $defaults = ['boolean', 'string', 'number', 'object', 'array']; - /** * Parse a JSON object to a data structure * @@ -88,17 +83,24 @@ public function parse($object, &$dependencies) $this->type = $object->content->value->element; $this->description = isset($object->meta->description) ? $object->meta->description : NULL; $this->status = - isset($object->attributes->typeAttributes[0]) ? $object->attributes->typeAttributes[0] : NULL; + isset($object->attributes->typeAttributes) ? join(', ', $object->attributes->typeAttributes) : NULL; - if (!in_array($this->type, $this->defaults)) + if (!in_array($this->type, self::DEFAULTS)) { $dependencies[] = $this->type; } - if ($this->type === 'object') + if ($this->type === 'object' || $this->type === 'array') { - $value = isset($object->content->value->content) ? $object->content->value : NULL; - $this->value = new DataStructureElement(); + $value = isset($object->content->value->content) ? $object->content->value : NULL; + if ($this->type === 'array') + { + $this->value = new ArrayStructureElement(); + } + else + { + $this->value = new DataStructureElement(); + } $this->value = $this->value->parse($value, $dependencies); return $this; @@ -123,44 +125,51 @@ function __toString() if (is_array($this->value)) { - $return = '
'; + $return = ''; foreach ($this->value as $object) { - if (get_class($object) === get_class($this)) + if (get_class($object) === self::class || get_class($object) === ArrayStructureElement::class) { $return .= $object; } } - $return .= ''; + $return .= '
'; return $return; } - $type = (!in_array($this->type, $this->defaults)) ? - '' . $this->type . '' : ''.$this->type.''; + $type = (!in_array($this->type, self::DEFAULTS)) ? + '' . $this->type . '' : '' . $this->type . ''; if (empty($this->value)) { - $value = 'no example'; + $value = ''; } - else if (is_object($this->value) && self::class === get_class($this->value)) + else { - $value = '
'.$this->value.'
'; - } - else{ - $value = 'Example: ' . $this->value . ''; + if (is_object($this->value) && self::class === get_class($this->value)) + { + $value = '
' . $this->value . '
'; + } + elseif (is_object($this->value) && (ArrayStructureElement::class === get_class($this->value))) + { + $value = '
' . $this->value . '
'; + } + else + { + $value = '' . $this->value . ''; + } } $return = - '
' . - '' . $this->key . "" . - "
\t" . - '
' . - $type . - '' . $this->description . '' . - $value . - '
'; + '' . + '' . '' . $this->key . "" . '' . + '' . $type . '' . + ' ' . $this->status . '' . + '' . $this->description . '' . + '' . $value . '' . + ''; return $return; } diff --git a/src/PHPDraft/Model/Elements/ArrayStructureElement.php b/src/PHPDraft/Model/Elements/ArrayStructureElement.php new file mode 100644 index 00000000..af129a4e --- /dev/null +++ b/src/PHPDraft/Model/Elements/ArrayStructureElement.php @@ -0,0 +1,71 @@ +element = (isset($item->element)) ? $item->element : 'array'; + $this->element = (isset($item->element)) ? $item->element : NULL; + $this->value = (isset($item->content)) ? $item->content : NULL; + + if (isset($item->content)) + { + foreach ($item->content as $key => $sub_item) + { + $this->type[$key] = $sub_item->element; + switch ($sub_item->element) + { + case 'array': + $value = new ArrayStructureElement(); + $this->value[$key] = $value->parse($sub_item, $dependencies); + break; + case 'object': + $value = new DataStructureElement(); + $this->value[$key] = $value->parse($sub_item, $dependencies); + break; + default: + $this->value[$key] = (isset($sub_item->content)) ? $sub_item->content : NULL; + break; + } + } + } + + return $this; + } + + function __toString() + { + if (!is_array($this->type)) + { + return ''; + } + $return = ''; + + return $return; + } + + +} \ No newline at end of file diff --git a/src/PHPDraft/Model/Elements/RequestBodyElement.php b/src/PHPDraft/Model/Elements/RequestBodyElement.php index 610753df..14bd384f 100644 --- a/src/PHPDraft/Model/Elements/RequestBodyElement.php +++ b/src/PHPDraft/Model/Elements/RequestBodyElement.php @@ -44,7 +44,7 @@ public function parse($object, &$dependencies) $this->status = isset($object->attributes->typeAttributes[0]) ? $object->attributes->typeAttributes[0] : NULL; - if (!in_array($this->type, $this->defaults)) + if (!in_array($this->type, parent::DEFAULTS)) { $dependencies[] = $this->type; } diff --git a/src/PHPDraft/Out/HTML/index.css b/src/PHPDraft/Out/HTML/default.css similarity index 100% rename from src/PHPDraft/Out/HTML/index.css rename to src/PHPDraft/Out/HTML/default.css diff --git a/src/PHPDraft/Out/HTML/index.js b/src/PHPDraft/Out/HTML/default.js similarity index 100% rename from src/PHPDraft/Out/HTML/index.js rename to src/PHPDraft/Out/HTML/default.js diff --git a/src/PHPDraft/Out/HTML/default.php b/src/PHPDraft/Out/HTML/default.php index bff03624..d8a56fd8 100644 --- a/src/PHPDraft/Out/HTML/default.php +++ b/src/PHPDraft/Out/HTML/default.php @@ -19,7 +19,7 @@ @@ -101,11 +101,11 @@ class="pull-right get_method_icon($transition->get_method()); ?>">
-

title; ?>

+

title; ?>

description; ?>

children as $resource): ?>

- title; ?> + title; ?> href; ?>

description; ?>

@@ -117,7 +117,7 @@ class="panel panel-default get_method(); ?>"> get_method(); ?> href; ?> title; ?> + id="get_href(); ?>">title; ?>
@@ -163,8 +163,9 @@ class="value"> request->body)): ?>
Body
request->body as $value): ?> - request->headers['Content-Type']))?$transition->request->headers['Content-Type']:NULL;?> - print_request($type);?> + request->headers['Content-Type'])) ? $transition->request->headers['Content-Type'] : NULL; ?> + print_request($type); ?> @@ -173,35 +174,17 @@ class="value"> url_variables !== []): ?>
URI Parameters
- url_variables as $key => $value): ?> - status === '') ? '' : '(' . $value->status . ')'; ?> -
-
- type; ?> - - value) ? join('|', $value->value) : $value->value; ?> -

description; ?>

-
+ url_variables as $value): ?> +
data_variables !== []): ?>
Data object
-
- data_variables as $key => $value): ?> - status === '') ? '' : '(' . $value->status . ')'; ?> -
-
- type; ?> - - value) ? join('|', $value->value) : $value->value; ?> -

description; ?>

-
+ data_variables as $value): ?> + -
responses)): ?> @@ -231,22 +214,9 @@ class="value"> structure !== []): ?>
Data Structure
-
- structure[0]->value as $value): ?> -
key; ?>
-
- type; ?> - description; ?> - value->element) && $value->value->element === 'object') - : ?> - get_data_structure($value->value); ?> - -
value; ?>
- - -
- -
+ structure as $value): ?> + + content as $key => $value): ?>
@@ -264,7 +234,7 @@ class="value">

- +

@@ -281,6 +251,6 @@ class="value"> - + \ No newline at end of file diff --git a/src/PHPDraft/Out/TemplateGenerator.php b/src/PHPDraft/Out/TemplateGenerator.php index 3e24d379..977a236e 100644 --- a/src/PHPDraft/Out/TemplateGenerator.php +++ b/src/PHPDraft/Out/TemplateGenerator.php @@ -146,20 +146,4 @@ function get_response_status($response) } } - /** - * Determine if an object should be printed - * - * @param DataStructureElement $object Objects to print - * - * @return string Object representation - */ - function get_data_structure($object) - { - if (!get_class($object) === 'DataStructureElement') - { - return; - } - return $object; - } - } \ No newline at end of file From edd663ffddcb3c4246153c69127dbd81bbad6742 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Mon, 5 Sep 2016 11:31:06 +0200 Subject: [PATCH 2/2] Fix #8 and #9 --- src/PHPDraft/Model/DataStructureElement.php | 2 +- src/PHPDraft/Model/HTTPRequest.php | 7 +- src/PHPDraft/Model/Transition.php | 6 +- src/PHPDraft/Out/HTML/default.css | 67 +++---- src/PHPDraft/Out/HTML/default.js | 20 ++- src/PHPDraft/Out/HTML/default.php | 190 +++++++++++--------- src/PHPDraft/Out/TemplateGenerator.php | 6 +- 7 files changed, 172 insertions(+), 126 deletions(-) diff --git a/src/PHPDraft/Model/DataStructureElement.php b/src/PHPDraft/Model/DataStructureElement.php index 796dfc89..4d785d73 100644 --- a/src/PHPDraft/Model/DataStructureElement.php +++ b/src/PHPDraft/Model/DataStructureElement.php @@ -120,7 +120,7 @@ function __toString() { if ($this->value === NULL && $this->key === NULL) { - return '{ ... }'; + return '{ ... }'; } if (is_array($this->value)) diff --git a/src/PHPDraft/Model/HTTPRequest.php b/src/PHPDraft/Model/HTTPRequest.php index f60074b9..86a1481a 100644 --- a/src/PHPDraft/Model/HTTPRequest.php +++ b/src/PHPDraft/Model/HTTPRequest.php @@ -103,9 +103,11 @@ private function parse_structure($objects) * * @param string $base_url URL to the base server * + * @param array $additional Extra options to pass to cURL + * * @return string An executable cURL command */ - public function get_curl_command($base_url) + public function get_curl_command($base_url, $additional = []) { $options = []; @@ -120,7 +122,8 @@ public function get_curl_command($base_url) { $options[] = '-H "'.$header.': '.$value. '"'; } - $options[] = '-v'; + $options = array_merge($options, $additional); + return htmlspecialchars('curl '.join(' ', $options). ' "'.$this->parent->build_url($base_url).'"'); } diff --git a/src/PHPDraft/Model/Transition.php b/src/PHPDraft/Model/Transition.php index b0592842..411102f8 100644 --- a/src/PHPDraft/Model/Transition.php +++ b/src/PHPDraft/Model/Transition.php @@ -169,10 +169,12 @@ public function get_method() * * @param string $base_url base URL of the server * + * @param array $additional additional arguments to pass + * * @return string A cURL CLI command */ - public function get_curl_command($base_url) + public function get_curl_command($base_url, $additional = []) { - return $this->request->get_curl_command($base_url); + return $this->request->get_curl_command($base_url, $additional); } } \ No newline at end of file diff --git a/src/PHPDraft/Out/HTML/default.css b/src/PHPDraft/Out/HTML/default.css index 1175aef8..92e4657b 100644 --- a/src/PHPDraft/Out/HTML/default.css +++ b/src/PHPDraft/Out/HTML/default.css @@ -7,17 +7,6 @@ var.url-value { padding: 2px; } -div.main-url.fixed { - position: fixed; - bottom: -5px; - left: 10px; - z-index: 999; - background: #fff; - border-radius: 5px; - padding: 5px; - box-shadow: 0px 0px 2px rgba(0, 0, 0, 0.5); -} - div.main-url { text-align: justify; line-break: loose; @@ -77,38 +66,25 @@ body .col-md-10 h2:first-of-type { line-height: 22px; } -.POST .panel-heading, span.POST { +.POST > .panel-heading, span.POST { background: #62c462; } -.GET .panel-heading, span.GET { +.GET > .panel-heading, span.GET { background: #5bc0de; } -.DELETE .panel-heading, span.DELETE { +.DELETE > .panel-heading, span.DELETE { background: #ee5f5b; } -.PUT .panel-heading, span.PUT { +.PUT > .panel-heading, span.PUT { background: #f89406; } -.row dl.dl-horizontal dt { - width: 80px; - line-height: 30px; -} -.row dl.dl-horizontal dd { - line-height: 30px; - margin-left: 100px; -} - -.row dl.dl-horizontal dd a.code, -.row dl.dl-horizontal dd code{ - margin-right: 5px; -} - -.example-value{ - color: rgba(0,0,0,0.4); +.example-value { + color: rgba(0, 0, 0, 0.4); + text-align: right; } a.code { @@ -116,5 +92,32 @@ a.code { font-size: 90%; background-color: #f9f2f4; border-radius: 4px; - font-family: Menlo,Monaco,Consolas,"Courier New",monospace; + font-family: Menlo, Monaco, Consolas, "Courier New", monospace; +} + +.popover { + width: auto; + max-width: 50%; +} + +.popover-content { + width: auto; + max-width: 100%; + line-break: normal; + white-space: pre-wrap; +} + +.panel-body { + position: relative; +} + +.curl.btn { + z-index: 999; + position: absolute; + right: 15px; + top: 15px; +} + +h5.response-body, h4.request { + cursor: pointer; } \ No newline at end of file diff --git a/src/PHPDraft/Out/HTML/default.js b/src/PHPDraft/Out/HTML/default.js index 3552c242..5e4ed522 100644 --- a/src/PHPDraft/Out/HTML/default.js +++ b/src/PHPDraft/Out/HTML/default.js @@ -3,4 +3,22 @@ */ $(function () { $('[data-toggle="popover"]').popover() -}) \ No newline at end of file +}) + +$('.collapse.request-panel').on('shown.bs.collapse', function () { + $(this).parent().find("h4.request .glyphicon.indicator").removeClass("glyphicon-menu-up").addClass("glyphicon-menu-down"); +}).on('hidden.bs.collapse', function () { + $(this).parent().find("h4.request .glyphicon.indicator").removeClass("glyphicon-menu-down").addClass("glyphicon-menu-up"); +}); + +$('.collapse.response-panel').on('shown.bs.collapse', function () { + $(this).parent().find("h4.response .glyphicon.indicator").removeClass("glyphicon-menu-up").addClass("glyphicon-menu-down"); +}).on('hidden.bs.collapse', function () { + $(this).parent().find("h4.response .glyphicon.indicator").removeClass("glyphicon-menu-down").addClass("glyphicon-menu-up"); +}); + +$('pre.collapse.response-body').on('shown.bs.collapse', function () { + $(this).parent().find("h5.response-body .glyphicon.indicator").removeClass("glyphicon-menu-up").addClass("glyphicon-menu-down"); +}).on('hidden.bs.collapse', function () { + $(this).parent().find("h5.response-body .glyphicon.indicator").removeClass("glyphicon-menu-down").addClass("glyphicon-menu-up"); +}); \ No newline at end of file diff --git a/src/PHPDraft/Out/HTML/default.php b/src/PHPDraft/Out/HTML/default.php index d8a56fd8..6544cd21 100644 --- a/src/PHPDraft/Out/HTML/default.php +++ b/src/PHPDraft/Out/HTML/default.php @@ -89,7 +89,7 @@ class="pull-right get_method_icon($transition->get_method()); ?>"> base_structures as $key => $structure): ?>
  • - +
  • @@ -121,89 +121,37 @@ class="panel panel-default get_method(); ?>">
    + + +

    description; ?>

    url_variables !== []): ?>

    Example URI

    base_data['HOST']; ?> build_url(); ?> - -

    Request - - - - -

    - - responses)): ?> - responses as $response): ?> -

    - Response statuscode; ?> - +
    +
    +

    Request +

    - +
    + request)): ?> + request->headers !== []): ?>
    Headers
      - headers as $name => $value): ?> + request->headers as $name => $value): ?>
    • : @@ -212,16 +160,86 @@ class="value">
    - structure !== []): ?> -
    Data Structure
    - structure as $value): ?> - + request->body)): ?> +
    Body
    + request->body as $value): ?> + request->headers['Content-Type'])) ? $transition->request->headers['Content-Type'] : NULL; ?> + print_request($type); ?> + - content as $key => $value): ?> -
    -
    + + + url_variables !== []): ?> +
    URI Parameters
    +
    + url_variables as $value): ?> + + +
    + + + data_variables !== []): ?> +
    Data object
    + data_variables as $value): ?> + + +
    +
    + responses)): ?> + responses as $response): ?> +
    +
    +

    + Response statuscode; ?> + +

    +
    +
    + headers !== []): ?> +
    Headers
    +
      + headers as $name => $value): ?> +
    • + : + +
    • + +
    + + structure !== []): ?> +
    Data Structure
    +
    + structure as $value): ?> + + +
    + + content as $key => $value): ?> +
    + get_href() . '-' . $response->statuscode . '-' . str_replace(['/', + '+'], '-', $key); ?> +
    + + + +
    + +
    + +
    @@ -234,11 +252,13 @@ class="value">

    - +

    - +
    + +
    @@ -251,6 +271,6 @@ class="value"> - + \ No newline at end of file diff --git a/src/PHPDraft/Out/TemplateGenerator.php b/src/PHPDraft/Out/TemplateGenerator.php index 977a236e..9c381d89 100644 --- a/src/PHPDraft/Out/TemplateGenerator.php +++ b/src/PHPDraft/Out/TemplateGenerator.php @@ -134,15 +134,15 @@ function get_response_status($response) { if ($response <= 299) { - return 'success'; + return 'text-success'; } elseif ($response > 299 && $response <= 399) { - return 'warning'; + return 'text-warning'; } else { - return 'error'; + return 'text-error'; } }