Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions phpdraft
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,25 @@ try
// Define the cli options.
$cli = new Cli();
$cli->description('Parse API Blueprint files.')
->opt('file:f', 'Specifies the file to parse.', false, 'string')
->opt('yes:y', 'Always accept using the online mode.', false)
->opt('online:o', 'Always use the online mode.', false)
->opt('template:t', 'Specifies the template to use. (defaults to \'default\').', false, 'string')
->opt('sort:s', 'Sort displayed values [All|None|Structures|Webservices] (defaults to the way the objects are in the file).', false, 'string')
->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('help:h', 'This help text', 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');
->opt('file:f', 'Specifies the file to parse.', false)
->opt('yes:y', 'Always accept using the online mode.', false, 'bool')
->opt('online:o', 'Always use the online mode.', false, 'bool')
->opt('template:t', 'Specifies the template to use. (defaults to \'default\').', false)
->opt('sort:s', 'Sort displayed values [All|None|Structures|Webservices] (defaults to the way the objects are in the file).', false)
->opt('header_image:i', 'Specifies an image to display in the header.', false)
->opt('css:c', 'Specifies a CSS file to include (value is put in a link element without checking).', false)
->opt('javascript:j', 'Specifies a JS file to include (value is put in a script element without checking).', false)
->opt('debug-json-file', 'Input a rendered JSON file for debugging.', false)
->opt('debug-json', 'Input a rendered JSON text for debugging.', false);

// Parse and return cli args.
$args = $cli->parse($argv, FALSE);
if (isset($args['help']) || empty($args->getOpts())) {
$cli->writeHelp();
throw new ExecutionException('', 0);
}
if (isset($args['version'])) {
Version::version();
throw new ExecutionException('', 0);
Expand All @@ -53,6 +58,10 @@ try
$file = tempnam(sys_get_temp_dir(), 'phpdraft');
file_put_contents($file, $stdin);
}
if ($file === NULL || $file === '')
{
throw new ExecutionException('ERROR: File does not exist', 200);
}

if (!($file !== NULL || isset($args['debug-json-file']) || isset($args['debug-json']))) {
throw new ExecutionException('Missing required option: file', 1);
Expand Down Expand Up @@ -97,12 +106,7 @@ try

echo $html;
}
catch (ExecutionException $exception)
{
file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL);
exit($exception->getCode());
}
catch (Exception $exception)
catch (ExecutionException|Exception $exception)
{
file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL);
exit($exception->getCode());
Expand Down
6 changes: 3 additions & 3 deletions src/PHPDraft/In/ApibFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ class ApibFileParser
*
* @var string
*/
protected $full_apib;
protected string $full_apib;

/**
* Location of the API Blueprint to parse.
*
* @var string
*/
protected $location;
protected string $location;

/**
* Filename to parse.
*
* @var string
*/
private $filename;
private string $filename;

/**
* FileParser constructor.
Expand Down
19 changes: 4 additions & 15 deletions src/PHPDraft/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

namespace PHPDraft\Model;

use PHPDraft\Model\Elements\BasicStructureElement;
use PHPDraft\Model\Elements\ObjectStructureElement;
use stdClass;

/**
* Class Category.
Expand All @@ -26,28 +24,19 @@ class Category extends HierarchyElement
*
* @var ObjectStructureElement[]
*/
public $structures = [];

/**
* Category type.
*
* @var ?string
*/
public $type = null;
public array $structures = [];

/**
* Fill class values based on JSON object.
*
* @param stdClass $object JSON object
* @param object $object JSON object
*
* @return $this self-reference
* @return self self-reference
*/
public function parse(stdClass $object)
public function parse(object $object): self
{
parent::parse($object);

$this->type = $object->meta->classes->content ?? null;

foreach ($object->content as $item) {
switch ($item->element) {
case 'resource':
Expand Down
4 changes: 2 additions & 2 deletions src/PHPDraft/Model/Comparable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ interface Comparable
/**
* Check if item is the same as other item.
*
* @param self $b Object to compare to
* @param object $b Object to compare to
*
* @return bool
*/
public function is_equal_to($b): bool;
public function is_equal_to(object $b): bool;
}
5 changes: 2 additions & 3 deletions src/PHPDraft/Model/Elements/ArrayStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ArrayStructureElement extends BasicStructureElement
* Parse an array object.
*
* @param object|null $object APIB Item to parse
* @param array $dependencies List of dependencies build
* @param string[] $dependencies List of dependencies build
*
* @return self Self reference
*/
Expand Down Expand Up @@ -60,8 +60,7 @@ public function __toString(): string
if (is_string($this->value)) {
$type = $this->get_element_as_html($this->element);
$desc = '';
if ($this->description !== NULL)
{
if ($this->description !== null) {
$desc = MarkdownExtra::defaultTransform($this->description);
}

Expand Down
22 changes: 11 additions & 11 deletions src/PHPDraft/Model/Elements/BasicStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ abstract class BasicStructureElement implements StructureElement
*
* @var ElementStructureElement|null
*/
public $key;
public ?ElementStructureElement $key = null;
/**
* Object JSON type.
*
* @var string|null
*/
public $type;
public ?string $type;
/**
* Object description.
*
* @var string|null
*/
public $description;
public ?string $description = null;
/**
* Type of element.
*
* @var string|null
*/
public $element = null;
public ?string $element = null;
/**
* Object value.
*
Expand All @@ -45,31 +45,31 @@ abstract class BasicStructureElement implements StructureElement
*
* @var string|null
*/
public $status = '';
public ?string $status = '';
/**
* Parent structure.
*
* @var string|null
*/
public $ref;
public ?string $ref;
/**
* Is variable.
*
* @var bool
*/
public $is_variable;
public bool $is_variable = false;
/**
* List of object dependencies.
*
* @var string[]|null
*/
public $deps;
public ?array $deps;

/**
* Parse a JSON object to a structure.
*
* @param object|null $object An object to parse
* @param array $dependencies Dependencies of this object
* @param string[] $dependencies Dependencies of this object
*
* @return StructureElement self reference
*/
Expand All @@ -92,8 +92,8 @@ abstract protected function new_instance(): StructureElement;
/**
* Parse common fields to give context.
*
* @param object $object APIB object
* @param array $dependencies Object dependencies
* @param object $object APIB object
* @param string[] $dependencies Object dependencies
*
* @return void
*/
Expand Down
20 changes: 11 additions & 9 deletions src/PHPDraft/Model/Elements/ElementStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ class ElementStructureElement implements StructureElement
/**
* Object JSON type.
*
* @var string|null
* @var string
*/
public $type;
public string $type;

/**
* Object description.
*
* @var string|null
*/
public $description;
public ?string $description = null;

/**
* Object value.
Expand All @@ -26,11 +26,14 @@ class ElementStructureElement implements StructureElement
public $value = null;

/**
* @param object|null $object
* @param array $dependencies
* @return StructureElement
* Parse a JSON object to a structure.
*
* @param object|null $object An object to parse
* @param string[] $dependencies Dependencies of this object
*
* @return self self reference
*/
public function parse(?object $object, array &$dependencies): StructureElement
public function parse(?object $object, array &$dependencies): self
{
if (!in_array($object->element, self::DEFAULTS)) {
$dependencies[] = $object->element;
Expand Down Expand Up @@ -77,8 +80,7 @@ public function string_value(bool $flat = false)
*/
protected function get_element_as_html(?string $element): string
{
if ($element === null)
{
if ($element === null) {
return '<code>null</code>';
}

Expand Down
22 changes: 9 additions & 13 deletions src/PHPDraft/Model/Elements/EnumStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EnumStructureElement extends BasicStructureElement
* Parse an array object.
*
* @param object|null $object APIB Item to parse
* @param array $dependencies List of dependencies build
* @param string[] $dependencies List of dependencies build
*
* @return $this
*/
Expand Down Expand Up @@ -79,23 +79,19 @@ public function parse(?object $object, array &$dependencies): StructureElement
*/
public function __toString(): string
{
if (is_string($this->value)) {
$type = $this->get_element_as_html($this->element);
$desc = '';
if ($this->description !== NULL)
{
$desc = MarkdownExtra::defaultTransform($this->description);
if (is_iterable($this->value)) {
$return = '';
foreach ($this->value as $item) {
$return .= $item->__toString();
}

return "<tr><td>{$this->key->value}</td><td>{$type}</td><td>{$desc}</td></tr>";
return '<ul class="list-group mdl-list">' . $return . '</ul>';
}

$return = '';
foreach ($this->value as $item) {
$return .= $item->__toString();
}
$type = $this->get_element_as_html($this->element);
$desc = $this->description === null ? '' : MarkdownExtra::defaultTransform($this->description);

return '<ul class="list-group mdl-list">' . $return . '</ul>';
return "<tr><td>{$this->key->value}</td><td>{$type}</td><td>{$desc}</td></tr>";
}

/**
Expand Down
18 changes: 8 additions & 10 deletions src/PHPDraft/Model/Elements/ObjectStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
namespace PHPDraft\Model\Elements;

use Michelf\MarkdownExtra;
use stdClass;

/**
* Class ObjectStructureElement.
Expand All @@ -22,10 +21,10 @@ class ObjectStructureElement extends BasicStructureElement
{
/**
* Object representation before parsing
* @var stdClass|null
* @var object|null
* @phpstan-ignore-next-line
*/
private $object;
private ?object $object;

/**
* Unset object function.
Expand All @@ -40,7 +39,7 @@ public function __clearForTest(): void
* Parse a JSON object to a data structure.
*
* @param object|null $object An object to parse
* @param array $dependencies Dependencies of this object
* @param string[] $dependencies Dependencies of this object
*
* @return ObjectStructureElement self reference
*/
Expand Down Expand Up @@ -84,8 +83,8 @@ public function parse(?object $object, array &$dependencies): StructureElement
/**
* Parse $this->value as a structure based on given content.
*
* @param object $object APIB content
* @param array $dependencies Object dependencies
* @param object $object APIB content
* @param string[] $dependencies Object dependencies
*
* @return void
*/
Expand Down Expand Up @@ -118,8 +117,8 @@ protected function new_instance(): StructureElement
/**
* Parse content formed as an array.
*
* @param object $object APIB content
* @param array $dependencies Object dependencies
* @param object $object APIB content
* @param string[] $dependencies Object dependencies
*
* @return void
*/
Expand Down Expand Up @@ -211,8 +210,7 @@ protected function construct_string_return(string $value): string
$variable = '<a class="variable-key" title="' . $this->key->type . '" href="#object-' . $link_name . '"><span class="fas fa-info variable-info" data-toggle="tooltip" data-placement="top" data-tooltip="' . $tooltip . '" title="' . $tooltip . '"></span></a>';
}
$desc = '';
if ($this->description !== NULL)
{
if ($this->description !== null) {
$desc = MarkdownExtra::defaultTransform($this->description);
}

Expand Down
Loading