Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @restdock-ignoreField and @restdock-ignoreExtraField tags #19

Merged
merged 3 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ Use @restdoc-extraField and @restdoc-extraLink for extra fields.

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

### Skip fields

Use @restdoc-ignoreField and @restdoc-ignoreExtraField to skip field.

```php

/**
* @restdoc-ignoreField $hidden
* @restdoc-ignoreExtraField $hidden_too
*/
```

## 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
35 changes: 35 additions & 0 deletions models/ModelDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ private function _addField(&$fields, $name, $type = '', $description = '', $scen
$fields[$name]->setType($type);
}

/**
* @param array $fields
* @param string $name
*/
private function _removeField(&$fields, $name)
{
if (isset($fields[$name])) {
unset($fields[$name]);
}
}

/**
* @param string $name
* @param string $type
Expand All @@ -65,6 +76,22 @@ public function addExtraField($name, $type = '', $description = '')
$this->_addField($this->_extraFields, $name, $type, $description);
}

/**
* @param string $name
*/
public function removeField($name)
{
$this->_removeField($this->_fields, $name);
}

/**
* @param string $name
*/
public function removeExtraField($name)
{
$this->_removeField($this->_extraFields, $name);
}

/**
* @param $name
*/
Expand Down Expand Up @@ -263,6 +290,14 @@ public function prepare()
}
}

foreach ($this->getTagsByName('ignoreField') as $key => $tag) {
$this->removeField(trim($tag->getVariableName(), '$'));
}

foreach ($this->getTagsByName('ignoreExtraField') as $key => $tag) {
$this->removeExtraField(trim($tag->getVariableName(), '$'));
}

foreach ($this->_fields as $field) {
$field->prepare();
}
Expand Down
2 changes: 2 additions & 0 deletions models/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public static function registerTagHandlers()
'label' => '\phpDocumentor\Reflection\DocBlock\Tag',
'extraField' => '\phpDocumentor\Reflection\DocBlock\Tag\ParamTag',
'extraLink' => '\phpDocumentor\Reflection\DocBlock\Tag\ParamTag',
'ignoreField' => '\phpDocumentor\Reflection\DocBlock\Tag\ParamTag',
'ignoreExtraField' => '\phpDocumentor\Reflection\DocBlock\Tag\ParamTag'
];
foreach ($mapping as $suffix => $class) {
$tagName = Doc::TAG_PREFIX . $suffix;
Expand Down
4 changes: 4 additions & 0 deletions tests/models/NewSpecialOffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class NewSpecialOffer extends SpecialOffer
* @inheritdoc
*
* @restdoc-extraField string $alpha2 Code country. <a href="http://example.com">Detail link.</a>
* @restdoc-ignoreExtraField $ignore
*/
public function extraFields()
{
Expand All @@ -23,6 +24,9 @@ public function extraFields()
},
'full_name' => function () {
return 'full_name';
},
'ignore' => function () {
return 'ignore';
}
];
}
Expand Down
6 changes: 4 additions & 2 deletions tests/models/SpecialOffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SpecialOffer extends Product
* @restdoc-link comment $text
* @restdoc-link $comment
* @restdoc-sortField $text
* @restdoc-ignoreField $is_ignore
*/
public function fields()
{
Expand All @@ -28,14 +29,15 @@ public function fields()
'title' => function () {
return 'title';
},
'text' => 'Comment'
'text' => 'Comment',
'is_ignore'
];
}

public function scenarios()
{
return [
'api-create' => ['id', 'title', 'note'],
'api-create' => ['id', 'title', 'note', 'is_ignore'],
'api-update' => ['comment'],
];
}
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/models/ModelParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ public function testInherit()
$this->assertEquals('Code country. <a href="http://example.com">Detail link.</a>', $doc->extraFields['alpha2']->description);
$this->assertEquals('string|null', $doc->extraFields['full_name']->type);
$this->assertEquals('Full name. <a href="http://example.com">Detail link.</a>', $doc->extraFields['full_name']->description);
$this->assertFalse(isset($doc->extraFields['ignore']));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be used:

$this->assertArrayNotHasKey('ignore', $doc->extraFields);


$this->assertEquals(5, count($doc->fields));
$this->assertEquals('int', $doc->fields['id']->type);
$this->assertEquals('string', $doc->fields['title']->type);
$this->assertEquals('string', $doc->fields['comment']->type);
$this->assertFalse(isset($doc->fields['is_ignore']));

$this->assertEquals('string', $doc->fields['note']->type);
$this->assertEquals('string', $doc->fields['text']->type);
Expand Down