From e1c7e8a5bdbe833903b0002c51112515833195e4 Mon Sep 17 00:00:00 2001 From: thenetexperts Date: Sat, 19 Jul 2014 13:43:39 +0200 Subject: [PATCH] adding optional color codes for tags annotation --- Annotation/ApiDoc.php | 33 ++++++++++++++++------------ Controller/ApiDocController.php | 1 - Resources/config/routing.yml | 2 +- Resources/config/swagger_routing.yml | 2 +- Resources/doc/index.md | 22 ++++++++++++++++++- Resources/views/method.html.twig | 6 ++--- Tests/Annotation/ApiDocTest.php | 20 +++++++++++++++-- 7 files changed, 63 insertions(+), 23 deletions(-) diff --git a/Annotation/ApiDoc.php b/Annotation/ApiDoc.php index 90fc24a..dd7242a 100644 --- a/Annotation/ApiDoc.php +++ b/Annotation/ApiDoc.php @@ -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 */ diff --git a/Controller/ApiDocController.php b/Controller/ApiDocController.php index 33c75f8..deebcf2 100644 --- a/Controller/ApiDocController.php +++ b/Controller/ApiDocController.php @@ -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; diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index d0bd03f..4591631 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -2,4 +2,4 @@ nelmio_api_doc_index: pattern: / defaults: { _controller: NelmioApiDocBundle:ApiDoc:index } requirements: - _method: GET \ No newline at end of file + _method: GET diff --git a/Resources/config/swagger_routing.yml b/Resources/config/swagger_routing.yml index c36f6d3..d06cd7b 100644 --- a/Resources/config/swagger_routing.yml +++ b/Resources/config/swagger_routing.yml @@ -8,4 +8,4 @@ nelmio_api_doc_swagger_api_declaration: pattern: /{resource} defaults: { _controller: NelmioApiDocBundle:ApiDoc:swagger } requirements: - _method: GET \ No newline at end of file + _method: GET diff --git a/Resources/doc/index.md b/Resources/doc/index.md index ae551b8..b747933 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -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 + {% if data.tags is defined %} - {% for tag in data.tags %} - {{ tag }} + {% for tag, color_code in data.tags %} + {{ tag }} {% endfor %} {% endif %} @@ -58,7 +58,7 @@
{{ data.link }}
{% endif %} - {% if data.requirements is defined and data.requirements is not empty %} + {% if data.requirements is defined and data.requirements is not empty %}

Requirements

diff --git a/Tests/Annotation/ApiDocTest.php b/Tests/Annotation/ApiDocTest.php index a3dac24..916015b 100644 --- a/Tests/Annotation/ApiDocTest.php +++ b/Tests/Annotation/ApiDocTest.php @@ -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', ) );