diff --git a/src/PHPDraft/Model/Category.php b/src/PHPDraft/Model/Category.php index a3301503..5d906482 100644 --- a/src/PHPDraft/Model/Category.php +++ b/src/PHPDraft/Model/Category.php @@ -24,6 +24,13 @@ class Category extends HierarchyElement */ public $structures = []; + /** + * Category type. + * + * @var ?string + */ + public $type = NULL; + /** * Fill class values based on JSON object. * @@ -34,6 +41,9 @@ class Category extends HierarchyElement public function parse(stdClass $object) { parent::parse($object); + + $this->type = $object->meta->classes->content ?? NULL; + foreach ($object->content as $item) { switch ($item->element) { case 'resource': @@ -48,6 +58,8 @@ public function parse(stdClass $object) if (is_array($item->content) && isset($item->content[0]->meta->id)) { $this->structures[$item->content[0]->meta->id] = $struct; + } elseif (isset($item->content->meta->id->content)) { + $this->structures[$item->content->meta->id->content] = $struct; } else { $this->structures[] = $struct; } diff --git a/src/PHPDraft/Model/Elements/BasicStructureElement.php b/src/PHPDraft/Model/Elements/BasicStructureElement.php index 64c21dae..30766222 100644 --- a/src/PHPDraft/Model/Elements/BasicStructureElement.php +++ b/src/PHPDraft/Model/Elements/BasicStructureElement.php @@ -88,10 +88,10 @@ abstract protected function new_instance(): StructureElement; * * @return void */ - protected function parse_common($object, array &$dependencies): void + protected function parse_common(stdClass $object, array &$dependencies): void { - $this->key = (isset($object->content->key->content)) ? $object->content->key->content : NULL; - $this->type = (isset($object->content->value->element)) ? $object->content->value->element : NULL; + $this->key = $object->content->key->content ?? NULL; + $this->type = $object->content->value->element ?? NULL; $this->description = NULL; if (isset($object->meta->description->content)){ $this->description = htmlentities($object->meta->description->content); diff --git a/src/PHPDraft/Model/HTTPRequest.php b/src/PHPDraft/Model/HTTPRequest.php index 9e845512..ab726259 100644 --- a/src/PHPDraft/Model/HTTPRequest.php +++ b/src/PHPDraft/Model/HTTPRequest.php @@ -57,21 +57,26 @@ class HTTPRequest implements Comparable * * @var mixed */ - public $body = NULL; + public $body = null; /** - * Identifier for the request. + * Schema of the body of the request (if POST or PUT). * - * @var string + * @var mixed */ - protected $id; - + public $body_schema = null; /** * Structure of the request (if POST or PUT). * * @var RequestBodyElement */ public $struct = []; + /** + * Identifier for the request. + * + * @var string + */ + protected $id; /** * HTTPRequest constructor. @@ -94,21 +99,38 @@ public function __construct(Transition &$parent) public function parse(stdClass $object): self { $this->method = $object->attributes->method->content ?? $object->attributes->method; - $this->title = isset($object->meta->title) ? $object->meta->title : NULL; + $this->title = isset($object->meta->title) ? $object->meta->title : null; if (($this->method === 'POST' || $this->method === 'PUT') && !empty($object->content)) { foreach ($object->content as $value) { if ($value->element === 'dataStructure') { $this->parse_structure($value); continue; - } elseif ($value->element === 'copy') { + } + + if ($value->element === 'copy') { $this->description = MarkdownExtra::defaultTransform(htmlentities($value->content)); - } elseif ($value->element === 'asset') { - if (in_array('messageBody', $value->meta->classes)) { - $this->body[] = (isset($value->content)) ? $value->content : NULL; - $this->headers['Content-Type'] = - (isset($value->attributes->contentType)) ? $value->attributes->contentType : ''; - } + continue; + } + + if ($value->element !== 'asset') { + continue; + } + if (is_array($value->meta->classes) && in_array('messageBody', $value->meta->classes)) { + $this->body[] = (isset($value->content)) ? $value->content : null; + $this->headers['Content-Type'] = (isset($value->attributes->contentType)) ? $value->attributes->contentType : ''; + continue; + } + + if (isset($value->meta->classes->content) + && is_array($value->meta->classes->content) + && $value->meta->classes->content[0]->content === 'messageBody') { + $this->body[] = (isset($value->content)) ? $value->content : null; + $this->headers['Content-Type'] = (isset($value->attributes->contentType->content)) ? $value->attributes->contentType->content : ''; + } elseif (isset($value->meta->classes->content) + && is_array($value->meta->classes->content) + && $value->meta->classes->content[0]->content === 'messageBodySchema') { + $this->body_schema = (isset($value->content)) ? $value->content : null; } } } @@ -119,7 +141,7 @@ public function parse(stdClass $object): self } } - if ($this->body === NULL) { + if ($this->body === null) { $this->body = &$this->struct; } @@ -152,15 +174,15 @@ public function get_id(): string * @param string $base_url URL to the base server * @param array $additional Extra options to pass to cURL * + * @return string An executable cURL command * @throws Exception * - * @return string An executable cURL command */ public function get_curl_command(string $base_url, array $additional = []): string { $options = []; - $type = $this->headers['Content-Type'] ?? NULL; + $type = $this->headers['Content-Type'] ?? null; $options[] = '-X' . $this->method; if (empty($this->body)) { @@ -171,6 +193,9 @@ public function get_curl_command(string $base_url, array $additional = []): stri $options[] = '--data-binary ' . escapeshellarg(join('', $this->body)); } elseif (is_subclass_of($this->struct, StructureElement::class)) { foreach ($this->struct->value as $body) { + if (empty($body)) { + continue; + } $options[] = '--data-binary ' . escapeshellarg(strip_tags($body->print_request($type))); } } @@ -179,7 +204,8 @@ public function get_curl_command(string $base_url, array $additional = []): stri } $options = array_merge($options, $additional); - return htmlspecialchars('curl ' . join(' ', $options) . ' ' . escapeshellarg($this->parent->build_url($base_url, TRUE))); + return htmlspecialchars('curl ' . join(' ', $options) . ' ' . escapeshellarg($this->parent->build_url($base_url, + true))); } /** @@ -200,17 +226,17 @@ public function is_equal_to($b): bool * @param string $base_url URL to the base server * @param array $additional Extra options to pass to the service * + * @return string * @throws Exception * - * @return string */ public function get_hurl_link(string $base_url, array $additional = []): string { $options = []; - $type = (isset($this->headers['Content-Type'])) ? $this->headers['Content-Type'] : NULL; + $type = (isset($this->headers['Content-Type'])) ? $this->headers['Content-Type'] : null; - $url = $this->parent->build_url($base_url, TRUE); + $url = $this->parent->build_url($base_url, true); $url = explode('?', $url); if (isset($url[1])) { $params = []; diff --git a/src/PHPDraft/Model/HTTPResponse.php b/src/PHPDraft/Model/HTTPResponse.php index fa7c1a3b..bab06825 100644 --- a/src/PHPDraft/Model/HTTPResponse.php +++ b/src/PHPDraft/Model/HTTPResponse.php @@ -124,14 +124,17 @@ protected function parse_headers(stdClass $object): void protected function parse_content(stdClass $object): void { foreach ($object->content as $value) { - if ($value->element === 'dataStructure') { - $this->parse_structure($value->content); - continue; - } elseif ($value->element === 'copy') { + if ($value->element === 'copy') { $this->description = MarkdownExtra::defaultTransform(htmlentities($value->content)); continue; } + if ($value->element === 'dataStructure') { + $content = is_array($value->content) ? $value->content : [$value->content]; + $this->parse_structure($content); + continue; + } + if (isset($value->attributes->contentType->content)) { $this->content[$value->attributes->contentType->content] = $value->content; } elseif (isset($value->attributes->contentType)) { diff --git a/src/PHPDraft/Out/HTML/default.phtml b/src/PHPDraft/Out/HTML/default.phtml index cdc59a7a..6363eb92 100644 --- a/src/PHPDraft/Out/HTML/default.phtml +++ b/src/PHPDraft/Out/HTML/default.phtml @@ -235,21 +235,21 @@ use PHPDraft\Out\Minifier;
structure as $value): ?> - +
- content as $key => $value): ?> + content as $response_key => $value): ?>
get_id() . '-' . $response->statuscode . '-' . str_replace([ '/', '+', - ], '-', $key); ?> + ], '-', $response_key); ?>
- +
json->content as $item) {
             if ($item->element === 'annotation') {
                 $warnings = TRUE;
-                $prefix   = strtoupper($item->meta->classes[0]);
+                $line     = $item->attributes->sourceMap->content[0]->content[0]->content[0]->attributes->line->content ?? 'UNKNOWN';
+                $prefix   = (is_array($item->meta->classes)) ? strtoupper($item->meta->classes[0]) : strtoupper($item->meta->classes->content[0]->content);
                 $error    = $item->content;
-                file_put_contents('php://stderr', "$prefix: $error\n");
-                file_put_contents('php://stdout', "
$prefix: $error
\n"); + file_put_contents('php://stderr', "$prefix: $error (line $line)\n"); + file_put_contents('php://stdout', "
$prefix: $error (line $line)
\n"); } }