Skip to content

Commit

Permalink
Fix #133
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Jun 30, 2020
1 parent e6ba581 commit 55989ac
Show file tree
Hide file tree
Showing 11 changed files with 844 additions and 390 deletions.
2 changes: 1 addition & 1 deletion phpdraft
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use PHPDraft\Parse\ParserFactory;
use PHPDraft\Parse\ResourceException;

define('VERSION', '0');
#define('ID_STATIC', 'SOME_ID');
//define('ID_STATIC', 'SOME_ID');
try
{
// Define the cli options.
Expand Down
10 changes: 6 additions & 4 deletions src/PHPDraft/Model/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class Resource extends HierarchyElement
/**
* URL variables.
*
* @var ObjectStructureElement|null
* @var ObjectStructureElement[]
*/
public $url_variables = null;
public $url_variables = [];

/**
* Resource constructor.
Expand Down Expand Up @@ -58,8 +58,10 @@ public function parse(stdClass $object): self

if (isset($object->attributes->hrefVariables)) {
$deps = [];
$struct = new ObjectStructureElement();
$this->url_variables = $struct->parse($object->attributes->hrefVariables, $deps);
foreach ($object->attributes->hrefVariables->content as $variable) {
$struct = new ObjectStructureElement();
$this->url_variables[] = $struct->parse($variable, $deps);
}
}

foreach ($object->content as $item) {
Expand Down
23 changes: 10 additions & 13 deletions src/PHPDraft/Model/Tests/TransitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function testParseIsCalledIssetHrefVariables(): void
{
$this->set_reflection_property_value('parent', $this->parent);

$json = '{"attributes":{"href":"something", "hrefVariables":{"hello":"world"}}, "content":[]}';
$json = '{"attributes":{"href":"something", "hrefVariables":{"content": [{"element": "member", "hello":"world"}]}}, "content":[]}';
$obj = json_decode($json);
$this->class->parse($obj);

Expand All @@ -86,7 +86,7 @@ public function testParseIsCalledIssetData(): void
{
$this->set_reflection_property_value('parent', $this->parent);

$obj = '{"attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[]}';
$obj = '{"element": "dataStructure", "attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[]}';

$this->class->parse(json_decode($obj));

Expand All @@ -101,7 +101,7 @@ public function testParseIsCalledIsNotArrayContent(): void
{
$this->set_reflection_property_value('parent', $this->parent);

$obj = '{"attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":""}';
$obj = '{"element": "dataStructure", "attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":""}';

$this->class->parse(json_decode($obj));

Expand All @@ -116,7 +116,7 @@ public function testParseIsCalledIsArrayContentNoChild(): void
{
$this->set_reflection_property_value('parent', $this->parent);

$obj = '{"attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":{}}]}';
$obj = '{"element": "dataStructure", "attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":{}}]}';

$this->class->parse(json_decode($obj));

Expand All @@ -131,7 +131,7 @@ public function testParseIsCalledIsArrayContentWrongChild(): void
{
$this->set_reflection_property_value('parent', $this->parent);

$obj = '{"attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":[{"element":"test"}]}]}';
$obj = '{"element": "dataStructure", "attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":[{"element":"test"}]}]}';

$this->class->parse(json_decode($obj));

Expand All @@ -146,7 +146,7 @@ public function testParseIsCalledIsArrayContentRequest(): void
{
$this->set_reflection_property_value('parent', $this->parent);

$obj = '{"attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":[{"element":"httpRequest", "attributes":{"method":"TEST"}, "content":{}}]}]}';
$obj = '{"element": "dataStructure", "attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":[{"element":"httpRequest", "attributes":{"method":"TEST"}, "content":{}}]}]}';

$this->class->parse(json_decode($obj));

Expand All @@ -161,7 +161,7 @@ public function testParseIsCalledIsArrayContentResponse(): void
{
$this->set_reflection_property_value('parent', $this->parent);

$obj = '{"attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":[{"element":"httpResponse", "content":[], "attributes":{"statusCode":"1000", "headers":{"content":[]}}}]}]}';
$obj = '{"element": "dataStructure", "attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":[{"element":"httpResponse", "content":[], "attributes":{"statusCode":"1000", "headers":{"content":[]}}}]}]}';

$this->class->parse(json_decode($obj));

Expand All @@ -176,7 +176,7 @@ public function testParseIsCalledIsArrayContentDefault(): void
{
$this->set_reflection_property_value('parent', $this->parent);

$obj = '{"attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":[{"element":"Cow", "content":[], "attributes":{"statusCode":"1000", "headers":{"content":[]}}}]}]}';
$obj = '{"element": "dataStructure", "attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":[{"element":"Cow", "content":[], "attributes":{"statusCode":"1000", "headers":{"content":[]}}}]}]}';

$this->class->parse(json_decode($obj));

Expand All @@ -202,7 +202,7 @@ public function testParseIsCalledIsArrayContentRequestList(): void
$req_property->setAccessible(true);
$req_property->setValue($this->class, $requests);

$obj = '{"attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":[{"element":"httpRequest", "attributes":{"method":"TEST"}}]}]}';
$obj = '{"element": "dataStructure", "attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":[{"element":"httpRequest", "attributes":{"method":"TEST"}}]}]}';

$this->class->parse(json_decode($obj));

Expand Down Expand Up @@ -375,12 +375,9 @@ public function testBuildURLVars(): void
->will($this->returnValue('STRING'));
$var1->key = $key1;

$vars = new \stdClass();
$vars->value = [ $var1 ];

$property = $this->reflection->getProperty('url_variables');
$property->setAccessible(true);
$property->setValue($this->class, $vars);
$property->setValue($this->class, [$var1]);

$req_property = $this->reflection->getProperty('href');
$req_property->setAccessible(true);
Expand Down
38 changes: 20 additions & 18 deletions src/PHPDraft/Model/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ class Transition extends HierarchyElement
/**
* URL variables.
*
* @var ObjectStructureElement|null
* @var StructureElement[]
*/
public $url_variables = null;
public $url_variables = [];

/**
* Data variables.
*
* @var array|StructureElement|null
* @var StructureElement|null
*/
public $data_variables = null;

Expand All @@ -65,7 +65,7 @@ class Transition extends HierarchyElement
/**
* Structures used (if any).
*
* @var ObjectStructureElement[]
* @var StructureElement[]
*/
public $structures = [];

Expand All @@ -74,7 +74,7 @@ class Transition extends HierarchyElement
*
* @param \PHPDraft\Model\Resource $parent A reference to the parent object
*/
public function __construct(Resource &$parent)
public function __construct(\PHPDraft\Model\Resource &$parent)
{
$this->parent = $parent;
}
Expand All @@ -95,13 +95,15 @@ public function parse(stdClass $object): self

if (isset($object->attributes->hrefVariables)) {
$deps = [];
$struct = new ObjectStructureElement();
$this->url_variables = $struct->parse($object->attributes->hrefVariables, $deps);
foreach ($object->attributes->hrefVariables->content as $variable) {
$struct = (new ObjectStructureElement())->get_class($variable->element);
$this->url_variables[] = $struct->parse($variable, $deps);
}
}

if (isset($object->attributes->data)) {
$deps = [];
$struct = new ObjectStructureElement();
$struct = (new ObjectStructureElement())->get_class($object->element);
$this->data_variables = $struct->parse($object->attributes->data->content, $deps);
}

Expand Down Expand Up @@ -169,23 +171,23 @@ public function build_url(string $base_url = '', bool $clean = false): string
}
$tpl = new UriTemplate($url);
$vars = [];
if ($this->url_variables !== null) {
foreach ($this->url_variables->value as $item) {
$urlvalue = $item->value;
if (is_subclass_of($item, BasicStructureElement::class)) {
$urlvalue = $item->string_value();
if ($this->url_variables !== []) {
foreach ($this->url_variables as $item) {
if (!is_subclass_of($item, BasicStructureElement::class)) {
continue;
}

$urlvalue = $item->string_value();
$vars[$item->key->value] = $urlvalue;
}
}
if ($this->parent->url_variables !== null) {
foreach ($this->parent->url_variables->value as $item) {
$urlvalue = $item->value;
if (is_subclass_of($item, BasicStructureElement::class)) {
$urlvalue = $item->string_value(true);
if ($this->parent->url_variables !== []) {
foreach ($this->parent->url_variables as $item) {
if (!is_subclass_of($item, BasicStructureElement::class)) {
continue;
}

$urlvalue = $item->string_value();
$vars[$item->key->value] = $urlvalue;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/PHPDraft/Out/HTML/default.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
$(function () {
$('[data-toggle="popover"]').popover();
$('[data-toggle="popover"]').popover({
html: true,
sanitize: false,
});
$('[data-toggle="tooltip"]').tooltip();
$('body').on('click', function (e) {
$('[data-toggle="popover"]').each(function () {
Expand Down
40 changes: 28 additions & 12 deletions src/PHPDraft/Out/HTML/default.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use PHPDraft\Out\Minifier;
<title><?= $this->base_data['TITLE']; ?></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css" integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous">
<?php foreach ($this->css as $style): ?><link rel="stylesheet" href="<?= $style ?>"><?php endforeach; ?>
<style><?= Minifier::minify_css(file_get_contents($this->find_include_file($this->template, 'css'), true));?></style>
Expand Down Expand Up @@ -69,7 +69,7 @@ use PHPDraft\Out\Minifier;
<?php endforeach; ?>
<?php if ($this->base_structures !== []): ?>
<nav id="nav-datastructures" class="navbar navbar-light structures" aria-label="Data structures">
<a class="navbar-brand" href="#<?= $category->get_href(); ?>">Data Structures</a>
<a class="navbar-brand" href="#data-structures">Data Structures</a>
<?php foreach ($this->base_structures as $key => $structure): ?>
<nav class="nav nav-pills flex-column structure" aria-label="Structure <?= $key?>">
<a class="nav-link" href="#object-<?= $this->strip_link_spaces($key); ?>"><?= $key; ?></a>
Expand Down Expand Up @@ -104,7 +104,14 @@ use PHPDraft\Out\Minifier;
<?php if ($resource->url_variables !== null): ?>
<h5>URI Parameters</h5>
<div class="row">
<?= $resource->url_variables; ?>
<table class="table table-striped mdl-data-table mdl-js-data-table ">
<thead><tr><td>key</td><td>type</td><td>status</td><td>description</td><td>value</td></tr></thead>
<tbody>
<?php foreach ($resource->url_variables as $url_variable): ?>
<?= $url_variable; ?>
<?php endforeach;?>
</tbody>
</table>
</div>
<?php endif; ?>
<?php foreach ($resource->children as $transition): ?>
Expand Down Expand Up @@ -152,10 +159,8 @@ use PHPDraft\Out\Minifier;
data-content="<textarea rows='8' cols='75'><?= $transition->get_curl_command($this->base_data['HOST']); ?></textarea>">
<span class="fas fa-copy"></span>
</a>
<?php if ($transition->url_variables !== []): ?>
<h5>Example URI</h5>
<span class="base-url"><?= $this->base_data['HOST']; ?></span><em><?= $transition->build_url(); ?></em>
<?php endif; ?>
<h5>Example URI</h5>
<span class="base-url"><?= $this->base_data['HOST']; ?></span><em><?= $transition->build_url(); ?></em>
<?php if ($request->headers !== []): ?>
<h5>Headers</h5>
<ul class="headers list-unstyled">
Expand Down Expand Up @@ -188,10 +193,17 @@ use PHPDraft\Out\Minifier;
</div>
<?php endif; ?>

<?php if ($transition->url_variables !== null): ?>
<?php if ($transition->url_variables !== []): ?>
<h5>URI Parameters</h5>
<div class="row">
<?= $transition->url_variables; ?>
<table class="table table-striped mdl-data-table mdl-js-data-table ">
<thead><tr><td>key</td><td>type</td><td>status</td><td>description</td><td>value</td></tr></thead>
<tbody>
<?php foreach ($transition->url_variables as $url_variable): ?>
<?= $url_variable; ?>
<?php endforeach;?>
</tbody>
</table>
</div>
<?php endif; ?>

Expand Down Expand Up @@ -248,6 +260,9 @@ use PHPDraft\Out\Minifier;
$response->get_id() . '-' . $response->statuscode . '-' . str_replace([
'/',
'+',
'; ',
';',
'=',
], '-', $response_key); ?>
<h5 class="response-body"
data-toggle="collapse"
Expand Down Expand Up @@ -310,10 +325,11 @@ if ($extras !== []):
<?php foreach ($this->js as $js): ?>
<script src="<?= $js ?>"></script>
<?php endforeach; ?>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha256-/ijcOLwFf26xEYAjW75FizKVo5tnTYiQddPZoLUHHZ8=" crossorigin="anonymous"></script>
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/4.2.2/anchor.min.js" integrity="sha256-E4RlfxwyJVmkkk0szw7LYJxuPlp6evtPSBDlWHsYYL8=" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<script><?= Minifier::minify_js(file_get_contents($this->find_include_file($this->template, 'js'), true)); ?></script>
</body>
</html>
Loading

0 comments on commit 55989ac

Please sign in to comment.