Skip to content

Commit 8bf0647

Browse files
committed
Starting unittests
1 parent e6f102b commit 8bf0647

19 files changed

+7341
-201
lines changed

.gitignore

+2-21
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
# User-specific stuff:
77
.idea/workspace.xml
8+
.idea/vcs.xml
89
.idea/tasks.xml
910
.idea/dictionaries
10-
.idea/vcs.xml
1111
.idea/jsLibraryMappings.xml
1212

1313
# Sensitive or high-churn files:
@@ -18,32 +18,13 @@
1818
.idea/dynamic.xml
1919
.idea/uiDesigner.xml
2020

21-
# Gradle:
22-
.idea/gradle.xml
23-
.idea/libraries
24-
25-
# Mongo Explorer plugin:
26-
.idea/mongoSettings.xml
27-
2821
## File-based project format:
2922
*.iws
3023

31-
## Plugin-specific files:
32-
3324
# IntelliJ
3425
/out/
35-
36-
# mpeltonen/sbt-idea plugin
37-
.idea_modules/
26+
/build/
3827

3928
# JIRA plugin
4029
atlassian-ide-plugin.xml
4130

42-
# Crashlytics plugin (for Android Studio and IntelliJ)
43-
com_crashlytics_export_strings.xml
44-
crashlytics.properties
45-
crashlytics-build.properties
46-
fabric.properties
47-
48-
# Phar creation file
49-
create_phar.php

create_phar.php

-34
This file was deleted.

index.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2-
set_include_path(get_include_path().":".__DIR__);
2+
set_include_path(get_include_path().":".__DIR__.'/src/');
33
$config = json_decode(file_get_contents(__DIR__."/config.json"));
4-
require_once 'src/PHPDraft/Core/Autoloader.php';
4+
require_once 'PHPDraft/Core/Autoloader.php';
55
use PHPDraft\In\ApibFileParser;
66
use PHPDraft\Parse\ApibToJson;
77
use PHPDraft\Parse\JsonToHTML;

src/PHPDraft/Core/Autoloader.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ function ($classname) {
1414
$classname = ltrim($classname, '\\');
1515
preg_match('/^(.+)?([^\\\\]+)$/U', $classname, $match);
1616
$classname = str_replace('\\', '/', $match[1]) . str_replace(['\\', '_'], '/', $match[2]) . '.php';
17-
include_once 'src/' . $classname;
17+
if (strpos($classname, 'PHPDraft') !== FALSE || strpos($classname, 'Michelf') !== FALSE){
18+
include_once $classname;
19+
}
1820
}
1921
);
2022
?>

src/PHPDraft/HTML/default.php

+68-60
Large diffs are not rendered by default.

src/PHPDraft/Model/APIBlueprintElement.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
* This file contains the APIBlueprintElement class
44
*
55
* @package PHPDraft\Model
6-
* @author Sean Molenaar<[email protected]>
6+
* @author Sean Molenaar<[email protected]>
77
*/
88

99
namespace PHPDraft\Model;
1010

11-
use \Michelf\Markdown;
11+
use Michelf\Markdown;
1212

1313
abstract class APIBlueprintElement
1414
{
@@ -49,8 +49,14 @@ abstract class APIBlueprintElement
4949
*/
5050
function parse($object)
5151
{
52-
if (isset($object->meta) && isset($object->meta->title))$this->title = $object->meta->title;
53-
if (!isset($object->content)) return;
52+
if (isset($object->meta) && isset($object->meta->title))
53+
{
54+
$this->title = $object->meta->title;
55+
}
56+
if (!isset($object->content))
57+
{
58+
return;
59+
}
5460
foreach ($object->content as $key => $item)
5561
{
5662
if ($item->element === 'copy')
@@ -61,7 +67,10 @@ function parse($object)
6167
}
6268
}
6369

64-
if (!empty($object->content))$object->content = array_slice($object->content, 0);
70+
if (!empty($object->content))
71+
{
72+
$object->content = array_slice($object->content, 0);
73+
}
6574

6675
}
6776

@@ -73,6 +82,7 @@ function parse($object)
7382
public function get_href()
7483
{
7584
$prep = ($this->parent !== NULL) ? $this->parent->get_href() . '-' : '';
85+
7686
return $prep . str_replace(' ', '-', strtolower($this->title));
7787
}
7888
}

src/PHPDraft/Model/Category.php

+27-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ class Category extends APIBlueprintElement
1616
*/
1717
public $structures = [];
1818

19+
/**
20+
* Add a struct dependency
21+
*
22+
* @param string $object Name of the struct to add
23+
* @internal param string $name Name of the type
24+
*/
25+
public function add_struct($object)
26+
{
27+
echo "<pre>";
28+
var_dump($object);
29+
echo "</pre>";
30+
}
31+
1932
/**
2033
* Parse the category
2134
*
@@ -26,24 +39,26 @@ class Category extends APIBlueprintElement
2639
function parse($object)
2740
{
2841
parent::parse($object);
29-
foreach ($object->content as $key => $item)
30-
{
31-
switch ($item->element)
32-
{
42+
foreach ($object->content as $key => $item) {
43+
switch ($item->element) {
3344
case 'resource':
34-
$resource = new Resource($this);
45+
$resource = new Resource($this);
3546
$this->children[] = $resource->parse($item);
3647
break;
3748
case 'dataStructure':
38-
$struct = new DataStructureElement();
39-
if (isset($item->content[0]->meta->id))
40-
{
41-
$this->structures[$item->content[0]->meta->id] = $struct->parse($item);
42-
} else
43-
{
44-
$this->structures[] = $struct->parse($item);
49+
echo "<pre>";
50+
$deps = [];
51+
$struct = new DataStructureElement([$this, 'add_struct']);
52+
$struct = $struct->parse($item, $deps);
53+
$struct_array = ['struct' => $struct, 'deps' => $deps];
54+
var_dump($deps);
55+
if (isset($item->content[0]->meta->id)) {
56+
$this->structures[$item->content[0]->meta->id] = $struct_array;
57+
} else {
58+
$this->structures[] = $struct_array;
4559
}
4660

61+
echo "</pre>";
4762
break;
4863
default:
4964
continue;

src/PHPDraft/Model/DataStructureElement.php

+25-33
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,71 @@
33
* This file contains the DataStructureElement.php
44
*
55
* @package php-drafter\SOMETHING
6-
* @author Sean Molenaar<[email protected]>
6+
* @author Sean Molenaar<[email protected]>
77
*/
88

99
namespace PHPDraft\Model;
1010

1111
class DataStructureElement
1212
{
13-
public $defaults = ['boolean', 'string', 'number'];
14-
1513
/**
1614
* Object key
1715
* @var string
1816
*/
1917
public $key;
20-
2118
/**
2219
* Object JSON type
2320
* @var string
2421
*/
2522
public $type;
26-
2723
/**
2824
* Object description
2925
* @var string
3026
*/
3127
public $description;
32-
3328
/**
3429
* Type of element
3530
* @var string
3631
*/
3732
public $element = NULL;
38-
3933
/**
4034
* Object value
4135
* @var mixed|DataStructureElement[]
4236
*/
4337
public $value = NULL;
44-
4538
/**
4639
* Object status (required|optional)
4740
* @var string
4841
*/
4942
public $status = '';
5043

5144
/**
52-
* Callback for data types
53-
* @var callable
45+
* Unreported datatypes
46+
* @var array
5447
*/
55-
protected $callback;
56-
57-
/**
58-
* DataStructureElement constructor.
59-
* @param \stdClass $object Object to parse
60-
* @param callable $callback Call on object discovery
61-
*/
62-
public function __construct($object = NULL, $callback = NULL)
63-
{
64-
$this->callback = $callback;
65-
if ($object !== NULL) $this->parse($object);
66-
}
48+
protected $defaults = ['boolean', 'string', 'number', 'object', 'array'];
6749

6850
/**
6951
* Parse a JSON object to a data structure
7052
*
71-
* @param \stdClass $object An object to parse
53+
* @param \stdClass $object An object to parse
54+
* @param array $dependencies Dependencies of this object
55+
*
7256
* @return DataStructureElement self reference
7357
*/
74-
function parse($object)
58+
function parse($object, &$dependencies)
7559
{
76-
if (empty($object)) return $this;
60+
if (empty($object) || !isset($object->content))
61+
{
62+
return $this;
63+
}
7764
$this->element = $object->element;
78-
if (is_array($object->content))
65+
if (isset($object->content) && is_array($object->content))
7966
{
8067
foreach ($object->content as $value)
8168
{
82-
$this->value[] = new DataStructureElement($value, $this->callback);
69+
$struct = new DataStructureElement($this->callback);
70+
$this->value[] = $struct->parse($value, $dependencies);
8371
}
8472

8573
return $this;
@@ -88,21 +76,25 @@ function parse($object)
8876
$this->key = $object->content->key->content;
8977
$this->type = $object->content->value->element;
9078
$this->description = isset($object->meta->description) ? $object->meta->description : NULL;
91-
$this->status = isset($object->attributes->typeAttributes[0]) ? $object->attributes->typeAttributes[0] : '';
79+
$this->status =
80+
isset($object->attributes->typeAttributes[0]) ? $object->attributes->typeAttributes[0] : NULL;
9281

93-
if (!is_null($this->callback) && !in_array($this->type, $this->defaults))
82+
if (!in_array($this->type, $this->defaults))
9483
{
95-
call_user_func($this->callback, $this->type);
84+
$dependencies[] = $this->type;
9685
}
9786

98-
if ($object->content->value->element === 'object')
87+
if ($this->type === 'object')
9988
{
10089
$value = isset($object->content->value->content) ? $object->content->value : NULL;
101-
$this->value = new DataStructureElement($value, $this->callback);
90+
$this->value = new DataStructureElement($this->callback);
91+
$this->value = $this->value->parse($value, $dependencies);
92+
10293
return $this;
10394
}
10495

10596
$this->value = isset($object->content->value->content) ? $object->content->value->content : NULL;
97+
10698
return $this;
10799
}
108100

@@ -113,7 +105,7 @@ function parse($object)
113105
*/
114106
function __toString()
115107
{
116-
if ($this->value == NULL && $this->key == NULL)
108+
if ($this->value === NULL && $this->key === NULL)
117109
{
118110
return '{ ... }';
119111
}

src/PHPDraft/Model/HTTPResponse.php

+4-11
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,10 @@ protected function parse_structure($objects)
105105
{
106106
foreach ($objects as $object)
107107
{
108-
$this->structure[] = new DataStructureElement($object, [$this, 'spawn_object']);
108+
$deps = [];
109+
$struct = new DataStructureElement();
110+
$struct = $struct->parse($object, $deps);
111+
$this->structure[] = ['struct' => $struct, 'deps' => $deps];
109112
}
110113
}
111-
112-
/**
113-
* Callback to spawn needed data objects
114-
* @param $name
115-
* @return void
116-
*/
117-
public function spawn_object($name)
118-
{
119-
$this->parent->spawn_object($name);
120-
}
121114
}

0 commit comments

Comments
 (0)