Skip to content

Commit

Permalink
Fix some more drafter 4.0 issues
Browse files Browse the repository at this point in the history
Issue GH-75
  • Loading branch information
SMillerDev committed Sep 3, 2019
1 parent 479cc8e commit d40d31d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 34 deletions.
12 changes: 12 additions & 0 deletions src/PHPDraft/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class Category extends HierarchyElement
*/
public $structures = [];

/**
* Category type.
*
* @var ?string
*/
public $type = NULL;

/**
* Fill class values based on JSON object.
*
Expand All @@ -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':
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/PHPDraft/Model/Elements/BasicStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
66 changes: 46 additions & 20 deletions src/PHPDraft/Model/HTTPRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
}
}
Expand All @@ -119,7 +141,7 @@ public function parse(stdClass $object): self
}
}

if ($this->body === NULL) {
if ($this->body === null) {
$this->body = &$this->struct;
}

Expand Down Expand Up @@ -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)) {
Expand All @@ -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)));
}
}
Expand All @@ -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)));
}

/**
Expand All @@ -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 = [];
Expand Down
11 changes: 7 additions & 4 deletions src/PHPDraft/Model/HTTPResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
8 changes: 4 additions & 4 deletions src/PHPDraft/Out/HTML/default.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,21 @@ use PHPDraft\Out\Minifier;
<div class="row">
<?php foreach ($response->structure as $value): ?>
<?= $value; ?>
<?php endforeach; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php foreach ($response->content as $key => $value): ?>
<?php foreach ($response->content as $response_key => $value): ?>
<div>
<?php $href =
$response->get_id() . '-' . $response->statuscode . '-' . str_replace([
'/',
'+',
], '-', $key); ?>
], '-', $response_key); ?>
<h5 class="response-body"
data-toggle="collapse"
data-target="#response-<?= $href ?>">
<i class="fas indicator fa-angle-up"></i>
<?= $key; ?>
<?= $response_key; ?>

</h5>
<pre class="collapse response-body"
Expand Down
7 changes: 4 additions & 3 deletions src/PHPDraft/Parse/BaseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ public function parseToJson(): stdClass
foreach ($this->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', "<pre>$prefix: $error</pre>\n");
file_put_contents('php://stderr', "$prefix: $error (line $line)\n");
file_put_contents('php://stdout', "<pre>$prefix: $error (line $line)</pre>\n");
}
}

Expand Down

0 comments on commit d40d31d

Please sign in to comment.