Skip to content

Commit

Permalink
more spec 1.0 additions and bug fixes
Browse files Browse the repository at this point in the history
 - Added improved and more thorough support for complex typing of model properties
    - __see the readme.md for details of implementation__
 - added more granular testing of entity parsing classes (currently +/- 88% code coverage more to follow)
 - updates to the readme.md to reflect recent additions.

fixes #5
  • Loading branch information
zircote committed Jul 8, 2012
1 parent 7bd687d commit 4d7cee5
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 100 deletions.
50 changes: 45 additions & 5 deletions library/Swagger/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,25 @@ protected function _getModelProperties()
)
) {
foreach ($matches[1] as $match) {
preg_match('/(\w+)\s{1,}(\$\w+)(.*)/', $match, $prop);
preg_match('/(\w+)(<.*>|)\s{1,}(\$\w+)(.*)/i', $match, $prop);
if ($prop) {
if (isset($prop[2])) {
$name = str_replace('$', '', $prop[2]);
if (isset($prop[3])) {
$name = str_replace('$', '', $prop[3]);
if (isset($prop[1])) {
$result['type'] = $prop[1];
}
if (isset($prop[3])) {
$result['description'] = trim($prop[3]);
if (isset($prop[4])) {
$result['description'] = trim($prop[4]);
}
$this->results['properties'][$name] = $result;
}
if (!empty($prop[2])) {
$this->results['properties'][$name] =
array_merge(
$this->results['properties'][$name],
$this->_parseComplexTypes($prop[2])
);
}
}
}
}
Expand All @@ -140,6 +147,39 @@ protected function _getModelProperties()
return $this;
}

/**
* @param string $complexType
* @return array
*/
protected function _parseComplexTypes($complexType)
{
$primitives = array(
'<boolean>' => array('items' => array('type' => 'boolean')),
'<bool>' => array('items' => array('type' => 'boolean')),
'<int>' => array('items' => array('type' => 'integer')),
'<integer>' => array('items' => array('type' => 'integer')),
'<string>' => array('items' => array('type' => 'string')),
'<float>' => array('items' => array('type' => 'float'))
);
if (array_key_exists(strtolower($complexType), $primitives)) {
return $primitives[strtolower($complexType)];
}
if(preg_match('/<ref:(\w+)>/i', $complexType, $match)){
return array('items' => array('$ref' => $match[1]));
}
if(preg_match_all('/([a-zA-Z0-9 ]+)/i', $complexType, $matches)){
$enum = array('enum' => array());
foreach ($matches[1] as $match) {
$match = trim($match);
if(!empty($match)){
$enum['enum'][] = $match;
}
}
return $enum;
}
return array();
}

/**
*
* @param \ReflectionProperty $value
Expand Down
102 changes: 66 additions & 36 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,47 @@ class LeadResponder_RoutesController

* `@SwaggerModel`

### Complex Types via Annotations:

Besides the basic primitive type definitions in models you may also define the following:


**Javascript Reference**

- `@property array<ref:tag> $tags this is a reference to tag`


**Array Member Types**

- __string__

- `@property array<string> $arrayItem This is an array of strings`

- __integer__

- `@property array<integer> $refArr This is an array of integers.`

**enum**

- `@property string<'Two Pigs','Duck', 'And 1 Cow'> $enumVal This is an enum value.`

#### Example Use:

```php
<?php
/**
*@SwaggerModel(
* @SwaggerModel(
* id="leadresonder_route",
* description="some long description of the model"
* )
* @property integer $usr_mlr_route_id description of blah
* @property string $route
* @property string $createdDate
* @property string $tag
*
* @property integer $usr_mlr_route_id some long winded description.
* @property string $route some long description of the model.
* @property string $createdDate
* @property array<ref:tag> $tags this is a reference to `tag`
* @property array<string> $arrayItem This is an array of strings
* @property array<integer> $refArr This is an array of integers.
* @property string<'Two Pigs','Duck', 'And 1 Cow'> $enumVal This is an enum value.
*
*/
class Model_LeadResponder_Route
Expand Down Expand Up @@ -204,48 +232,50 @@ _Outputs:_
{
"id":"leadresonder_route",
"description":"some long description of the model",
"properties":[
{
"name":"usr_mlr_route_id",
"properties":{
"usr_mlr_route_id":{
"type":"integer",
"description":"some long winded description"
"description":"some long winded description."
},
{
"name":"route",
"type":"string"
},
{
"name":"createdDate",
"type":"Date"
},
{
"name":"tag",
"type":"string"
"route":{
"type":"string",
"description":"some long description of the model."
},
{
"name":"enumVal",
"createdDate":{
"type":"string",
"enum":[
"item1",
"item2",
"item"
]
"description":""
},
{
"name":"arrayItem",
"tags":{
"type":"array",
"items":{
"type":"string"
"description":"this is a reference to `tag`",
"items" : {
"$ref": "tag"
}
},
{
"name":"refArr",
"arrayItem":{
"type":"array",
"description":"This is an array of strings",
"items" : {
"type": "string"
}
},
"refArr":{
"type":"array",
"items":{
"$ref":"ref_item"
"description":"This is an array of integers.",
"items" : {
"type": "integer"
}
},
"enumVal":{
"type":"string",
"description":"This is an enum value.",
"enum": ["Two Pigs","Duck","And 1 Cow"]
},
"integerParam":{
"description":"This is an integer Param",
"type":"integer"
}
]
}
}
],
"operations":[
Expand Down
7 changes: 3 additions & 4 deletions tests/Swagger/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ protected function setUp ()
}
EOF;

$this->Api = new \Swagger\Api('\\Organic\\RoutesController');

}

Expand All @@ -97,18 +96,18 @@ protected function setUp ()
*/
protected function tearDown ()
{
// TODO Auto-generated ApiTest::tearDown()

$this->Api = null;

parent::tearDown();
}

/**
* Tests Api->__construct()
*
*/
public function testResults ()
public function testApi ()
{
$this->Api = new \Swagger\Api('\\Organic\\RoutesController');
$actual = $this->Api->results;
$this->assertEquals(json_decode($this->fixture, true), $actual);

Expand Down
35 changes: 22 additions & 13 deletions tests/Swagger/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ protected function setUp ()
{
parent::setUp();

// TODO Auto-generated ModelTest::setUp()

$this->Model = new Model('Model_Organic_Route');
$this->fixture = <<<EOF
$this->fixture = <<<'EOF'
{
"id":"leadresonder_route",
"description":"some long description of the model",
Expand All @@ -43,21 +40,31 @@ protected function setUp ()
"type":"string",
"description":""
},
"tag":{
"type":"string",
"description":""
"tags":{
"type":"array",
"description":"this is a reference to `tag`",
"items" : {
"$ref": "tag"
}
},
"arrayItem":{
"type":"array",
"description":""
"description":"This is an array of strings",
"items" : {
"type": "string"
}
},
"refArr":{
"type":"array",
"description":""
"description":"This is an array of integers.",
"items" : {
"type": "integer"
}
},
"enumVal":{
"type":"array",
"description":""
"type":"string",
"description":"This is an enum value.",
"enum": ["Two Pigs","Duck","And 1 Cow"]
},
"integerParam":{
"description":"This is an integer Param",
Expand All @@ -82,11 +89,13 @@ protected function tearDown ()
}

/**
* Tests Model->__construct()
*
*/
public function test__construct ()
public function testModel ()
{
$this->Model = new Model('Model_Organic_Route');
$actual = $this->Model->results;
// print_r($actual);
$this->assertEquals(json_decode($this->fixture, true), $actual);

}
Expand Down
37 changes: 23 additions & 14 deletions tests/Swagger/ModelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ModelsTest extends \PHPUnit_Framework_TestCase
protected function setUp ()
{
parent::setUp();
$this->fixture = <<<EOF
$this->fixture = <<<'EOF'
{
"leadresonder_route":{
"id":"leadresonder_route",
Expand All @@ -40,21 +40,31 @@ protected function setUp ()
"type":"string",
"description":""
},
"tag":{
"type":"string",
"description":""
"tags":{
"type":"array",
"description":"this is a reference to `tag`",
"items" : {
"$ref": "tag"
}
},
"arrayItem":{
"type":"array",
"description":""
"description":"This is an array of strings",
"items" : {
"type": "string"
}
},
"refArr":{
"type":"array",
"description":""
"description":"This is an array of integers.",
"items" : {
"type": "integer"
}
},
"enumVal":{
"type":"array",
"description":""
"type":"string",
"description":"This is an enum value.",
"enum": ["Two Pigs","Duck","And 1 Cow"]
},
"integerParam":{
"description":"This is an integer Param",
Expand All @@ -63,12 +73,8 @@ protected function setUp ()
}
}
}
EOF;

$this->Models = new Models(
array('Model_Organic_Route','Model_LeadResponder_RouteCollection')
);

}

Expand All @@ -83,10 +89,13 @@ protected function tearDown ()
}

/**
* Tests Models->__construct()
*
*/
public function test__construct ()
public function testModels ()
{
$this->Models = new Models(
array('Model_Organic_Route','Model_LeadResponder_RouteCollection')
);
$this->assertEquals(json_decode($this->fixture, true), $this->Models->results);

}
Expand Down
Loading

0 comments on commit 4d7cee5

Please sign in to comment.