Merge pull request #440 from thenetexperts/tags-with-colors

This commit is contained in:
William DURAND 2014-08-25 11:28:31 +02:00
commit b2a996e047
7 changed files with 63 additions and 23 deletions

View File

@ -244,13 +244,17 @@ class ApiDoc
}
if (isset($data['tags'])) {
$tags = $data['tags'];
if (!is_array($tags)) {
$tags = array($tags);
if (is_array($data['tags'])) {
foreach ($data['tags'] as $tag => $colorCode) {
if (is_numeric($tag)) {
$this->addTag($colorCode);
} else {
$this->addTag($tag, $colorCode);
}
}
} else {
$this->tags[] = $data['tags'];
}
$this->tags = $tags;
}
if (isset($data['https'])) {
@ -287,6 +291,15 @@ class ApiDoc
$this->statusCodes[$statusCode] = !is_array($description) ? array($description) : $description;
}
/**
* @param string $tag
* @param string $colorCode
*/
public function addTag($tag, $colorCode = '#d9534f')
{
$this->tags[$tag] = $colorCode;
}
/**
* @param string $name
* @param array $requirement
@ -556,14 +569,6 @@ class ApiDoc
return $this->parameters;
}
/**
* @return array
*/
public function getTags()
{
return $this->tags;
}
/**
* @param boolean $deprecated
*/

View File

@ -13,7 +13,6 @@ namespace Nelmio\ApiDocBundle\Controller;
use Nelmio\ApiDocBundle\Formatter\RequestAwareSwaggerFormatter;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

View File

@ -127,7 +127,27 @@ The following properties are available:
* `deprecated`: allow to set method as deprecated (default: `false`);
* `tags`: allow to tag a method (e.g. `beta` or `in-development`). Either a single tag or an array of tags.
* `tags`: allow to tag a method (e.g. `beta` or `in-development`). Either a single tag or an array of tags. Each tag can have an optional hex colorcode attached.
``` php
<?php
class YourController
{
/**
* @ApiDoc(
* tags={
* "stable",
* "deprecated" = "#ff0000"
* }
* )
*/
public function myFunction()
{
// ...
}
}
```
* `filters`: an array of filters;

View File

@ -26,8 +26,8 @@
{{ data.uri }}
</span>
{% if data.tags is defined %}
{% for tag in data.tags %}
<span class="tag">{{ tag }}</span>
{% for tag, color_code in data.tags %}
<span class="tag" {% if color_code is defined and color_code is not empty %}style="background-color:{{ color_code }};"{% endif %}>{{ tag }}</span>
{% endfor %}
{% endif %}
</h3>
@ -58,7 +58,7 @@
<div><a href="{{ data.link }}" target="_blank">{{ data.link }}</a></div>
{% endif %}
{% if data.requirements is defined and data.requirements is not empty %}
{% if data.requirements is defined and data.requirements is not empty %}
<h4>Requirements</h4>
<table class="fullwidth">
<thead>

View File

@ -303,12 +303,28 @@ class ApiDocTest extends TestCase
$this->assertEquals(array('beta'), $array['tags']);
}
public function testConstructWithOneTagAndColorCode()
{
$data = array(
'tags' => array(
'beta' => '#ff0000'
)
);
$annot = new ApiDoc($data);
$array = $annot->toArray();
$this->assertTrue(is_array($array));
$this->assertTrue(is_array($array['tags']), 'Single tag should be put in array');
$this->assertEquals(array('beta' => '#ff0000'), $array['tags']);
}
public function testConstructWithMultipleTags()
{
$data = array(
'tags' => array(
'experimental',
'alpha'
'experimental' => '#0000ff',
'beta' => '#0000ff',
)
);