Skip to content

Commit

Permalink
Fix enums and add debug methods
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Feb 10, 2020
1 parent 5d8b3af commit c6097a4
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
34 changes: 19 additions & 15 deletions phpdraft
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ try
->opt('header_image:i', 'Specifies an image to display in the header.', false, 'string')
->opt('css:c', 'Specifies a CSS file to include (value is put in a link element without checking).', false, 'string')
->opt('javascript:j', 'Specifies a JS file to include (value is put in a script element without checking).', false, 'string')
->opt('version:v', 'Print the version for PHPDraft.', false);
->opt('version:v', 'Print the version for PHPDraft.', false)
->opt('debug-json-file', 'Input a rendered JSON file for debugging.', false, 'string')
->opt('debug-json', 'Input a rendered JSON text for debugging.', false, 'string');

// Parse and return cli args.
$args = $cli->parse($argv, FALSE);
if (isset($args['version'])) {
Version::version();
throw new ExecutionException('', 0);
}
if (!isset($args['file'])) {
if (!(isset($args['file']) || isset($args['debug-json-file']) || isset($args['debug-json']))) {
throw new ExecutionException('Missing required option: file', 1);
}

Expand All @@ -48,20 +50,22 @@ try
define('DRAFTER_ONLINE_MODE', 1);
}

$apib = new ApibFileParser($args->getOpt('file'));
$apib = $apib->parse();
$offline = FALSE;
$online = FALSE;
if (!isset($args['debug-json-file']) && !isset($args['debug-json'])) {
$apib = new ApibFileParser($args->getOpt('file'));
$apib = $apib->parse();
$offline = FALSE;
$online = FALSE;

try
{
$parser = ParserFactory::getDrafter();
$parser = $parser->init($apib);
$data = $parser->parseToJson();
}
catch (ResourceException $exception)
{
throw new ExecutionException('No drafter available', 255);
try {
$parser = ParserFactory::getDrafter();
$parser = $parser->init($apib);
$data = $parser->parseToJson();
} catch (ResourceException $exception) {
throw new ExecutionException('No drafter available', 255);
}
} else {
$json_string = $args['debug-json'] ?? file_get_contents($args['debug-json-file']);
$data = json_decode($json_string);
}

$html = ParserFactory::getJson()->init($data);
Expand Down
6 changes: 3 additions & 3 deletions src/PHPDraft/Model/Elements/EnumStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function parse(object $object, array &$dependencies): StructureElement

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

$this->key = is_null($this->key) ? $object->content : $this->key;
$this->type = is_null($this->type) ? $object->element : $this->type;
$this->key = $this->key ?? $object->content ?? 'UNKNOWN';
$this->type = $this->type ?? $object->element;

if (!isset($object->content->value->content)) {
$this->value = $this->key;
Expand All @@ -41,7 +41,7 @@ public function parse(object $object, array &$dependencies): StructureElement
$dependencies[] = $sub_item->element;
}

$this->value[$sub_item->content] = (isset($sub_item->element)) ? $sub_item->element : '';
$this->value[$sub_item->content] = $sub_item->element ?? '';
}

$this->deps = $dependencies;
Expand Down
2 changes: 1 addition & 1 deletion src/PHPDraft/Model/Elements/ObjectStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ObjectStructureElement extends BasicStructureElement
* Parse a JSON object to a data structure.
*
* @param mixed|null $object An object to parse
* @param array $dependencies Dependencies of this object
* @param array $dependencies Dependencies of this object
*
* @return ObjectStructureElement self reference
*/
Expand Down
2 changes: 0 additions & 2 deletions src/PHPDraft/Model/Elements/StructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

namespace PHPDraft\Model\Elements;

use stdClass;

interface StructureElement
{
/**
Expand Down
4 changes: 2 additions & 2 deletions src/PHPDraft/Parse/BaseHtmlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ abstract class BaseHtmlGenerator
/**
* JsonToHTML constructor.
*
* @param stdClass $json JSON representation of an API Blueprint
* @param object $json JSON representation of an API Blueprint
*/
public function init(stdClass $json): self
public function init(object $json): self
{
$this->object = $json;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Class JsonToHTMLTest
* @covers \PHPDraft\Parse\LegacyHtmlGenerator
*/
class JsonToHTMLTest extends LunrBaseTest
class LegacyHtmlGeneratorTest extends LunrBaseTest
{
/**
* Test Class
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<file>../src/PHPDraft/Parse/Tests/DrafterTest.php</file>
<file>../src/PHPDraft/Parse/Tests/LegacyDrafterTest.php</file>
<file>../src/PHPDraft/Parse/Tests/DrafterAPITest.php</file>
<file>../src/PHPDraft/Parse/Tests/JsonToHTMLTest.php</file>
<file>../src/PHPDraft/Parse/Tests/LegacyHtmlGeneratorTest.php</file>
</testsuite>
<testsuite name="Model">
<file>../src/PHPDraft/Model/Tests/CategoryTest.php</file>
Expand Down

0 comments on commit c6097a4

Please sign in to comment.