diff --git a/Formatter/AbstractFormatter.php b/Formatter/AbstractFormatter.php index 1ef7c3e..eadc844 100644 --- a/Formatter/AbstractFormatter.php +++ b/Formatter/AbstractFormatter.php @@ -20,7 +20,9 @@ abstract class AbstractFormatter implements FormatterInterface */ public function formatOne(ApiDoc $annotation) { - return $this->renderOne($annotation->toArray()); + return $this->renderOne( + $this->processAnnotation($annotation->toArray()) + ); } /** @@ -28,12 +30,9 @@ abstract class AbstractFormatter implements FormatterInterface */ public function format(array $collection) { - $array = array(); - foreach ($collection as $coll) { - $array[$coll['resource']][] = $coll['annotation']->toArray(); - } - - return $this->render($array); + return $this->render( + $this->processCollection($collection) + ); } /** @@ -66,15 +65,14 @@ abstract class AbstractFormatter implements FormatterInterface protected function compressNestedParameters(array $data, $parentName = null, $ignoreNestedReadOnly = false) { $newParams = array(); - foreach ($data as $name => $info) { $newName = $this->getNewName($name, $info, $parentName); $newParams[$newName] = array( - 'description' => $info['description'], - 'dataType' => $info['dataType'], - 'readonly' => $info['readonly'], - 'required' => $info['required'] + 'description' => $info['description'], + 'dataType' => $info['dataType'], + 'readonly' => $info['readonly'], + 'required' => $info['required'], ); if (isset($info['children']) && (!$info['readonly'] || !$ignoreNestedReadOnly)) { @@ -99,10 +97,46 @@ abstract class AbstractFormatter implements FormatterInterface protected function getNewName($name, $data, $parentName = null) { $newName = ($parentName) ? sprintf("%s[%s]", $parentName, $name) : $name; - - $array = (false === strpos($data['dataType'], "array of")) ? "" : "[]"; + $array = (false === strpos($data['dataType'], "array of")) ? "" : "[]"; return sprintf("%s%s", $newName, $array); } + /** + * @param array $annotation + * @return array + */ + protected function processAnnotation($annotation) + { + if (isset($annotation['parameters'])) { + $annotation['parameters'] = $this->compressNestedParameters($annotation['parameters'], null, true); + } + + if (isset($annotation['response'])) { + $annotation['response'] = $this->compressNestedParameters($annotation['response']); + } + + return $annotation; + } + + /** + * @param array[ApiDoc] $collection + * @return array + */ + protected function processCollection(array $collection) + { + $array = array(); + foreach ($collection as $coll) { + $array[$coll['resource']][] = $coll['annotation']->toArray(); + } + + $processedCollection = array(); + foreach ($array as $path => $annotations) { + foreach ($annotations as $annotation) { + $processedCollection[$path][] = $this->processAnnotation($annotation); + } + } + + return $processedCollection; + } } diff --git a/Formatter/FormatterInterface.php b/Formatter/FormatterInterface.php index 1a3302f..95147c2 100644 --- a/Formatter/FormatterInterface.php +++ b/Formatter/FormatterInterface.php @@ -18,7 +18,7 @@ interface FormatterInterface /** * Format a collection of documentation data. * - * @param array $collection + * @param array[ApiDoc] $collection * @return string|array */ public function format(array $collection); diff --git a/Formatter/HtmlFormatter.php b/Formatter/HtmlFormatter.php index 2981ca1..54677c0 100644 --- a/Formatter/HtmlFormatter.php +++ b/Formatter/HtmlFormatter.php @@ -36,7 +36,7 @@ class HtmlFormatter extends AbstractFormatter private $enableSandbox; /** - * @var \Symfony\Component\Templating\EngineInterface + * @var EngineInterface */ private $engine; @@ -98,6 +98,9 @@ class HtmlFormatter extends AbstractFormatter $this->requestFormatMethod = $method; } + /** + * @param string $format + */ public function setDefaultRequestFormat($format) { $this->defaultRequestFormat = $format; @@ -108,16 +111,11 @@ class HtmlFormatter extends AbstractFormatter */ protected function renderOne(array $data) { - if (isset($data['parameters'])) { - $data['parameters'] = $this->compressNestedParameters($data['parameters'], null, true); - } - - if (isset($data['response'])) { - $data['response'] = $this->compressNestedParameters($data['response']); - } - return $this->engine->render('NelmioApiDocBundle::resource.html.twig', array_merge( - array('data' => $data, 'displayContent' => true), + array( + 'data' => $data, + 'displayContent' => true, + ), $this->getGlobalVars() )); } @@ -127,25 +125,10 @@ class HtmlFormatter extends AbstractFormatter */ protected function render(array $collection) { - $processedCollection = array(); - - foreach ($collection as $path => $methods) { - $processedCollection[$path] = array(); - foreach ($methods as $method) { - if (isset($method['parameters'])) { - $method['parameters'] = $this->compressNestedParameters($method['parameters'], null, true); - } - - if (isset($method['response'])) { - $method['response'] = $this->compressNestedParameters($method['response']); - } - - $processedCollection[$path][] = $method; - } - } - return $this->engine->render('NelmioApiDocBundle::resources.html.twig', array_merge( - array('resources' => $processedCollection), + array( + 'resources' => $collection, + ), $this->getGlobalVars() )); } @@ -167,5 +150,4 @@ class HtmlFormatter extends AbstractFormatter 'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'), ); } - } diff --git a/Formatter/MarkdownFormatter.php b/Formatter/MarkdownFormatter.php index b794ba4..3f3ac32 100644 --- a/Formatter/MarkdownFormatter.php +++ b/Formatter/MarkdownFormatter.php @@ -18,14 +18,6 @@ class MarkdownFormatter extends AbstractFormatter */ protected function renderOne(array $data) { - if (isset($data['parameters'])) { - $data['parameters'] = $this->compressNestedParameters($data['parameters'], null, true); - } - - if (isset($data['response'])) { - $data['response'] = $this->compressNestedParameters($data['response']); - } - $markdown = sprintf("### `%s` %s ###\n", $data['method'], $data['uri']); if (isset($data['description'])) { diff --git a/Formatter/SimpleFormatter.php b/Formatter/SimpleFormatter.php index 788f913..e6aab92 100644 --- a/Formatter/SimpleFormatter.php +++ b/Formatter/SimpleFormatter.php @@ -11,14 +11,36 @@ namespace Nelmio\ApiDocBundle\Formatter; +use Nelmio\ApiDocBundle\Annotation\ApiDoc; + class SimpleFormatter extends AbstractFormatter { + /** + * {@inheritdoc} + */ + public function formatOne(ApiDoc $annotation) + { + return $annotation->toArray(); + } + + /** + * {@inheritdoc} + */ + public function format(array $collection) + { + $array = array(); + foreach ($collection as $coll) { + $array[$coll['resource']][] = $coll['annotation']->toArray(); + } + + return $array; + } + /** * {@inheritdoc} */ protected function renderOne(array $data) { - return $data; } /** @@ -26,6 +48,5 @@ class SimpleFormatter extends AbstractFormatter */ protected function render(array $collection) { - return $collection; } }