Skip to content

Commit

Permalink
Merge pull request #18 from SMillerDev/feature/enums
Browse files Browse the repository at this point in the history
Feature/enums
  • Loading branch information
SMillerDev authored Sep 23, 2016
2 parents 2d43667 + f5fa874 commit 1754c93
Show file tree
Hide file tree
Showing 44 changed files with 5,029 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
# JIRA plugin
atlassian-ide-plugin.xml

config.json
*.pem
4 changes: 0 additions & 4 deletions config.json

This file was deleted.

4 changes: 0 additions & 4 deletions config.json.sample

This file was deleted.

1 change: 0 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Set up include path for source handling
*/
set_include_path(get_include_path().":".__DIR__.'/src/');
$config = json_decode(file_get_contents(__DIR__."/config.json"));

/**
* Set up required classes (with the autoloader)
Expand Down
38 changes: 38 additions & 0 deletions src/PHPDraft/In/Tests/ApibFileParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* This file contains the ApibFileParserTest.php
*
* @package php-drafter\SOMETHING
* @author Sean Molenaar<[email protected]>
*/

namespace PHPDraft\In\Tests;


use PHPDraft\Core\TestBase;
use PHPDraft\In\ApibFileParser;
use ReflectionClass;

class ApibFileParserTest extends TestBase
{
public function setUp()
{
$this->class = new ApibFileParser(__DIR__.'/ApibFileParserTest.php');
$this->reflection = new ReflectionClass('PHPDraft\In\ApibFileParser');
}

public function tearDown()
{
unset($this->class);
unset($this->reflection);
}

public function testSetup()
{
$property = $this->reflection->getProperty('location');
$property->setAccessible(TRUE);
$this->assertSame(__DIR__.'/', $property->getValue($this->class));
}


}
4 changes: 3 additions & 1 deletion src/PHPDraft/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

namespace PHPDraft\Model;

class Category extends APIBlueprintElement
use PHPDraft\Model\Elements\DataStructureElement;

class Category extends HierarchyElement
{
/**
* API Structure element
Expand Down
12 changes: 7 additions & 5 deletions src/PHPDraft/Model/Elements/ArrayStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@

namespace PHPDraft\Model\Elements;

use PHPDraft\Model\StructureElement;

use PHPDraft\Model\DataStructureElement;

class ArrayStructureElement extends DataStructureElement
class ArrayStructureElement extends DataStructureElement implements StructureElement
{

public function parse($item, &$dependencies)
{
$this->element = (isset($item->element)) ? $item->element : 'array';
$this->element = (isset($item->element)) ? $item->element : NULL;
$this->value = (isset($item->content)) ? $item->content : NULL;

if (isset($item->content))
Expand All @@ -35,6 +33,10 @@ public function parse($item, &$dependencies)
$value = new DataStructureElement();
$this->value[$key] = $value->parse($sub_item, $dependencies);
break;
case 'enum':
$value = new EnumStructureElement();
$this->value[$key] = $value->parse($sub_item, $dependencies);
break;
default:
$this->value[$key] = (isset($sub_item->content)) ? $sub_item->content : NULL;
break;
Expand All @@ -55,7 +57,7 @@ function __toString()
foreach ($this->type as $key => $item)
{
$type =
(in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . $item . '">' . $item . '</a>';
(in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . str_replace(' ', '-', strtolower($item)) . '">' . $item . '</a>';

$value =
(isset($this->value[$key])) ? ': <span class="example-value pull-right">' . json_encode($this->value[$key]) . '</span>' : NULL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@
/**
* This file contains the DataStructureElement.php
*
* @package PHPDraft\Model
* @package PHPDraft\Model\Elements
* @author Sean Molenaar<[email protected]>
*/

namespace PHPDraft\Model;
namespace PHPDraft\Model\Elements;

use PHPDraft\Model\Elements\ArrayStructureElement;
use PHPDraft\Model\StructureElement;

class DataStructureElement
class DataStructureElement implements StructureElement
{
/**
* Default datatypes
* @var array
*/
const DEFAULTS = ['boolean', 'string', 'number', 'object', 'array'];

/**
* Object key
* @var string
Expand Down Expand Up @@ -68,17 +64,26 @@ public function parse($object, &$dependencies)
return $this;
}
$this->element = $object->element;

if (isset($object->content) && is_array($object->content))
{
foreach ($object->content as $value)
{
$struct = new DataStructureElement();
if (in_array($this->element, ['object', 'dataStructure']))
{
$struct = new DataStructureElement();
}
else
{
$struct = new EnumStructureElement();
}
$this->value[] = $struct->parse($value, $dependencies);
}

return $this;
}


$this->key = $object->content->key->content;
$this->type = $object->content->value->element;
$this->description = isset($object->meta->description) ? $object->meta->description : NULL;
Expand All @@ -90,13 +95,17 @@ public function parse($object, &$dependencies)
$dependencies[] = $this->type;
}

if ($this->type === 'object' || $this->type === 'array')
if ($this->type === 'object' || $this->type === 'array' || $this->type === 'enum' || !in_array($this->type, self::DEFAULTS))
{
$value = isset($object->content->value->content) ? $object->content->value : NULL;
if ($this->type === 'array')
{
$this->value = new ArrayStructureElement();
}
elseif ($this->type === 'enum')
{
$this->value = new EnumStructureElement();
}
else
{
$this->value = new DataStructureElement();
Expand Down Expand Up @@ -128,7 +137,15 @@ function __toString()
$return = '<table class="table table-striped">';
foreach ($this->value as $object)
{
if (is_string($object) || get_class($object) === self::class || get_class($object) === ArrayStructureElement::class)
if (get_class($object) === \stdClass::class)
{
return json_encode($object);
}
if (is_string($object)
|| get_class($object) === self::class
|| get_class($object) === ArrayStructureElement::class
|| get_class($object) === EnumStructureElement::class
)
{
$return .= $object;
}
Expand All @@ -140,7 +157,7 @@ function __toString()
}

$type = (!in_array($this->type, self::DEFAULTS)) ?
'<a class="code" href="#object-' . $this->type . '">' . $this->type . '</a>' : '<code>' . $this->type . '</code>';
'<a class="code" href="#object-' . str_replace(' ', '-', strtolower($this->type)). '">' . $this->type . '</a>' : '<code>' . $this->type . '</code>';

if (empty($this->value))
{
Expand All @@ -156,6 +173,10 @@ function __toString()
{
$value = '<div class="array-struct">' . $this->value . '</div>';
}
elseif (is_array($this->value) && (EnumStructureElement::class === get_class($this->value[0])))
{
$value = '<div class="enum-struct">' . $this->value . '</div>';
}
else
{
$value = '<span class="example-value pull-right">' . $this->value . '</span>';
Expand Down
81 changes: 81 additions & 0 deletions src/PHPDraft/Model/Elements/EnumStructureElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* This file contains the ${FILE_NAME}
*
* @package php-drafter\SOMETHING
* @author Sean Molenaar<[email protected]>
*/

namespace PHPDraft\Model\Elements;

use PHPDraft\Model\StructureElement;

class EnumStructureElement implements StructureElement
{
/**
* Object description
* @var string
*/
public $description;
/**
* Type of element
* @var string
*/
public $element = NULL;
/**
* Object value
* @var mixed
*/
public $value = NULL;
/**
* Object status (required|optional)
* @var string
*/
public $status = '';
/**
* List of object dependencies
* @var string[]
*/
public $deps;

/**
* Parse a JSON object to a structure
*
* @param \stdClass $item An object to parse
* @param array $dependencies Dependencies of this object
*
* @return EnumStructureElement self reference
*/
function parse($item, &$dependencies)
{
$this->element = (isset($item->element)) ? $item->element : NULL;
$this->description = (isset($item->meta->description)) ? $item->meta->description : NULL;
$this->value = (isset($item->content)) ? $item->content : NULL;

if (!in_array($this->element, self::DEFAULTS))
{
$dependencies[] = $this->element;
}

return $this;
}

/**
* Print a string representation
*
* @return string
*/
function __toString()
{
$type = (!in_array($this->element, self::DEFAULTS)) ?
'<a class="code" href="#object-' . str_replace(' ', '-', strtolower($this->element)) . '">' . $this->element . '</a>' : '<code>' . $this->element . '</code>';
$return = '<tr>' .
'<td><span>' . $this->value . '</span></td>' .
'<td>'.$type.'</td>' .
'<td><span>' . $this->description . '</span></td>' .
'</tr>';
return $return;
}


}
24 changes: 21 additions & 3 deletions src/PHPDraft/Model/Elements/RequestBodyElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace PHPDraft\Model\Elements;

use PHPDraft\Model\DataStructureElement;
use PHPDraft\Model\StructureElement;

class RequestBodyElement extends DataStructureElement
class RequestBodyElement extends DataStructureElement implements StructureElement
{
/**
* Parse a JSON object to a data structure
Expand Down Expand Up @@ -58,6 +58,13 @@ public function parse($object, &$dependencies)
return $this;
}

if ($this->type === 'array')
{
$this->value = '[]';

return $this;
}

$this->value = isset($object->content->value->content) ? $object->content->value->content : NULL;

return $this;
Expand Down Expand Up @@ -86,7 +93,7 @@ public function print_request($type = 'application/x-www-form-urlencoded')

switch ($type) {
case 'application/x-www-form-urlencoded':
$return .= join('&amp;', $list);
$return .= join('&', $list);
break;
default:
$return .= join(PHP_EOL, $list);
Expand All @@ -113,4 +120,15 @@ public function print_request($type = 'application/x-www-form-urlencoded')
break;
}
}

/**
*
* @return string
*/
function __toString()
{
return parent::__toString();
}


}
Loading

0 comments on commit 1754c93

Please sign in to comment.