Skip to content

Commit

Permalink
Merge pull request #9 from PaVeL-Ekt/feature/add-sorted-fields
Browse files Browse the repository at this point in the history
Добавил поддержку полей сортировки
  • Loading branch information
pahanini committed May 10, 2016
2 parents f91f583 + a7c0987 commit ea907f6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ Example:

Use @restdoc-extraField and @restdoc-extraLink for extra fields.

### Sort fields

Use @restdoc-sortField to sort field according to your code.

## Integrate With Slate

[Slate](https://github.com/tripit/slate) is probably one of the best tools to generate nice API. So you can
Expand Down
34 changes: 34 additions & 0 deletions models/ModelDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class ModelDoc extends Doc

private $_fields = [];

private $_sortFields = [];

private $_parent = null;

private $_properties = [];
Expand Down Expand Up @@ -63,6 +65,14 @@ public function addExtraField($name, $type = '', $description = '')
$this->_addField($this->_extraFields, $name, $type, $description);
}

/**
* @param $name
*/
public function addSortField($name)
{
$this->_addField($this->_sortFields, $name);
}

/**
* Adds scenario info
*
Expand Down Expand Up @@ -90,6 +100,14 @@ public function getFields()
return $this->_fields;
}

/**
* @return \pahanini\restdoc\models\FieldDoc[]
*/
public function getSortFields()
{
return $this->_sortFields;
}

/**
* @return \pahanini\restdoc\models\ControllerDoc
*/
Expand Down Expand Up @@ -161,6 +179,14 @@ public function hasFields()
return !empty($this->_fields);
}

/**
* @return bool If model has sort fields
*/
public function hasSortFields()
{
return !empty($this->_sortFields);
}

/**
* @param string $name
* @return bool
Expand Down Expand Up @@ -200,6 +226,11 @@ public function prepare()
$this->addExtraField($name, $tag->getType(), $tag->getDescription());
}

foreach ($this->getTagsByName('sortField') as $tag) {
$name = trim($tag->getContent(), '$');
$this->addSortField($name);
}

foreach($this->getTagsByName('link') as $tag) {
$name = trim($tag->getVariableName(), '$');
$propertyName = $tag->getType() ? trim($tag->getType(), '\\') : $name;
Expand Down Expand Up @@ -238,6 +269,9 @@ public function prepare()
foreach ($this->_extraFields as $field) {
$field->prepare();
}
foreach ($this->_sortFields as $field) {
$field->prepare();
}
}

public function setParent(Doc $value)
Expand Down
1 change: 1 addition & 0 deletions tests/models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Product extends ActiveRecord
/**
* @restdoc-field int $id
* @restdoc-field string $title
* @restdoc-sortField $title
*/
public function fields()
{
Expand Down
1 change: 1 addition & 0 deletions tests/models/SpecialOffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SpecialOffer extends Product
* @restdoc-field string $title
* @restdoc-link comment $text
* @restdoc-link $comment
* @restdoc-sortField $text
*/
public function fields()
{
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/models/ModelParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function testSimple()

$this->assertTrue($doc->hasFields());
$this->assertFalse($doc->hasExtraFields());
$this->assertTrue($doc->hasSortFields());

}

Expand Down Expand Up @@ -55,6 +56,10 @@ public function testInherit()
$this->assertEquals('string', $doc->fields['note']->type);
$this->assertEquals('string', $doc->fields['text']->type);

$this->assertEquals(2, count($doc->sortFields));
$this->assertEquals('title', $doc->sortFields['title']->name);
$this->assertEquals('text', $doc->sortFields['text']->name);

$this->assertTrue($doc->fields['id']->isInScenario('api-create'));
$this->assertFalse($doc->fields['id']->isInScenario('api-update'));

Expand All @@ -63,6 +68,6 @@ public function testInherit()

$this->assertTrue($doc->hasFields());
$this->assertTrue($doc->hasExtraFields());
$this->assertTrue($doc->hasSortFields());
}

}

0 comments on commit ea907f6

Please sign in to comment.