Skip to content

Commit

Permalink
Add rudimentary support for drafter v4
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Aug 14, 2019
1 parent 4ef2c81 commit f24e508
Show file tree
Hide file tree
Showing 17 changed files with 468 additions and 69 deletions.
6 changes: 4 additions & 2 deletions phpdraft
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use PHPDraft\Out\Sorting;
use PHPDraft\Out\Version;
use PHPDraft\Parse\ExecutionException;
use PHPDraft\Parse\JsonToHTML;
use PHPDraft\Parse\LegacyHtmlGenerator;
use PHPDraft\Parse\ParserFactory;
use PHPDraft\Parse\ResourceException;

Expand Down Expand Up @@ -56,15 +57,16 @@ try

try
{
$parser = ParserFactory::get();
$parser = ParserFactory::getDrafter();
$parser = $parser->init($apib);
$data = $parser->parseToJson();
}
catch (ResourceException $exception)
{
throw new ExecutionException('No drafter available', 255);
}

$html = new JsonToHTML($parser->parseToJson());
$html = ParserFactory::getJson()->init($data);
$name = 'PHPD_SORT_' . strtoupper($args->getOpt('sort', ''));
$html->sorting = Sorting::${$name} ?? -1;

Expand Down
17 changes: 14 additions & 3 deletions src/PHPDraft/Model/Elements/BasicStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,20 @@ protected function parse_common($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->description = isset($object->meta->description) ? htmlentities($object->meta->description) : NULL;
$this->status =
isset($object->attributes->typeAttributes) ? join(', ', $object->attributes->typeAttributes) : NULL;
$this->description = NULL;
if (isset($object->meta->description->content)){
$this->description = htmlentities($object->meta->description->content);
} elseif (isset($object->meta->description)) {
$this->description = htmlentities($object->meta->description);
}

$this->status = NULL;
if (isset($object->attributes->typeAttributes->content)){
$data = array_map(function ($item) { return $item->content; }, $object->attributes->typeAttributes->content);
$this->status = join(', ', $data);
} elseif (isset($object->attributes->typeAttributes)) {
$this->status = join(', ', $object->attributes->typeAttributes);
}

$this->description_as_html();

Expand Down
3 changes: 2 additions & 1 deletion src/PHPDraft/Model/Elements/EnumStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function parse($object, array &$dependencies): StructureElement
return $this;
}

foreach ($object->content->value->content as $sub_item) {
$enumerations = $object->content->value->attributes->enumerations->content ?? $object->content->value->content;
foreach ($enumerations as $sub_item) {
if (!in_array($sub_item->element, self::DEFAULTS)) {
$dependencies[] = $sub_item->element;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PHPDraft/Model/HTTPRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function __construct(Transition &$parent)
*/
public function parse(stdClass $object): self
{
$this->method = $object->attributes->method;
$this->method = $object->attributes->method->content ?? $object->attributes->method;
$this->title = isset($object->meta->title) ? $object->meta->title : NULL;

if (($this->method === 'POST' || $this->method === 'PUT') && !empty($object->content)) {
Expand Down
8 changes: 6 additions & 2 deletions src/PHPDraft/Model/HTTPResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public function __construct(Transition $parent)
*/
public function parse(stdClass $object): self
{
if (isset($object->attributes->statusCode)) {
if (isset($object->attributes->statusCode->content)) {
$this->statuscode = intval($object->attributes->statusCode->content);
} elseif (isset($object->attributes->statusCode)) {
$this->statuscode = intval($object->attributes->statusCode);
}
if (isset($object->attributes->headers)) {
Expand Down Expand Up @@ -130,7 +132,9 @@ protected function parse_content(stdClass $object): void
continue;
}

if (isset($value->attributes)) {
if (isset($value->attributes->contentType->content)) {
$this->content[$value->attributes->contentType->content] = $value->content;
} elseif (isset($value->attributes->contentType)) {
$this->content[$value->attributes->contentType] = $value->content;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/PHPDraft/Model/HierarchyElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class HierarchyElement
public function parse(stdClass $object)
{
if (isset($object->meta) && isset($object->meta->title)) {
$this->title = $object->meta->title;
$this->title = $object->meta->title->content ?? $object->meta->title;
}

if (!isset($object->content) || !is_array($object->content)) {
Expand Down
2 changes: 1 addition & 1 deletion src/PHPDraft/Model/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function parse(stdClass $object): self
parent::parse($object);

if (isset($object->attributes)) {
$this->href = $object->attributes->href;
$this->href = $object->attributes->href->content ?? $object->attributes->href;
}

foreach ($object->content as $item) {
Expand Down
3 changes: 2 additions & 1 deletion src/PHPDraft/Model/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Transition extends HierarchyElement
*
* @param \PHPDraft\Model\Resource $parent A reference to the parent object
*/
public function __construct(\PHPDraft\Model\Resource &$parent)
public function __construct(Resource &$parent)
{
$this->parent = $parent;
}
Expand All @@ -87,6 +87,7 @@ public function parse(stdClass $object): self
parent::parse($object);

$this->href = (isset($object->attributes->href)) ? $object->attributes->href : $this->parent->href;
$this->href = $this->href->content ?? $this->href;

if (isset($object->attributes->hrefVariables)) {
$deps = [];
Expand Down
72 changes: 72 additions & 0 deletions src/PHPDraft/Out/BaseTemplateGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* This file contains the BaseTemplateGenerator.php.
*
* @package PHPDraft\Out
*
* @author Sean Molenaar<sean@seanmolenaar.eu>
*/

namespace PHPDraft\Out;


use Lukasoppermann\Httpstatus\Httpstatus;
use PHPDraft\Model\Elements\ObjectStructureElement;

abstract class BaseTemplateGenerator
{
/**
* Type of sorting to do on objects.
*
* @var int
*/
public $sorting;
/**
* CSS Files to load.
*
* @var array
*/
public $css = [];
/**
* JS Files to load.
*
* @var array
*/
public $js = [];
/**
* JSON object of the API blueprint.
*
* @var mixed
*/
protected $categories = [];
/**
* The template file to load.
*
* @var string
*/
protected $template;
/**
* The image to use as a logo.
*
* @var string
*/
protected $image = null;
/**
* The base URl of the API.
*
* @var
*/
protected $base_data;
/**
* The Http Status resolver.
*
* @var Httpstatus
*/
protected $http_status;
/**
* Structures used in all data.
*
* @var ObjectStructureElement[]
*/
protected $base_structures = [];
}
Loading

0 comments on commit f24e508

Please sign in to comment.