NelmioApiDocBundle/Formatter/MarkdownFormatter.php

151 lines
4.7 KiB
PHP
Raw Normal View History

2012-04-12 01:28:36 +02:00
<?php
2012-04-13 11:03:05 +02:00
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2012-04-12 18:37:42 +02:00
namespace Nelmio\ApiDocBundle\Formatter;
2012-04-12 01:28:36 +02:00
class MarkdownFormatter extends AbstractFormatter
{
/**
* {@inheritdoc}
*/
protected function renderOne(array $data)
2012-04-12 01:28:36 +02:00
{
$markdown = sprintf("### `%s` %s ###\n", $data['method'], $data['uri']);
if(isset($data['deprecated']) && false !== $data['deprecated']) {
2013-03-18 08:40:03 +01:00
$markdown .= "### This method is deprecated ###";
$markdown .= "\n\n";
}
if (isset($data['description'])) {
$markdown .= sprintf("\n_%s_", $data['description']);
2012-04-12 01:28:36 +02:00
}
$markdown .= "\n\n";
if (isset($data['documentation']) && !empty($data['documentation'])) {
if (isset($data['description']) && 0 === strcmp($data['description'], $data['documentation'])) {
$markdown .= $data['documentation'];
$markdown .= "\n\n";
}
}
2012-04-12 01:28:36 +02:00
if (isset($data['requirements']) && !empty($data['requirements'])) {
$markdown .= "#### Requirements ####\n\n";
foreach ($data['requirements'] as $name => $infos) {
$markdown .= sprintf("**%s**\n\n", $name);
2012-07-18 12:46:22 +02:00
if (!empty($infos['requirement'])) {
$markdown .= sprintf(" - Requirement: %s\n", $infos['requirement']);
}
2012-11-17 18:05:05 +01:00
if (!empty($infos['dataType'])) {
$markdown .= sprintf(" - Type: %s\n", $infos['dataType']);
}
if (!empty($infos['description'])) {
$markdown .= sprintf(" - Description: %s\n", $infos['description']);
}
2012-04-12 01:28:36 +02:00
}
$markdown .= "\n";
}
if (isset($data['filters'])) {
$markdown .= "#### Filters ####\n\n";
foreach ($data['filters'] as $name => $filter) {
$markdown .= sprintf("%s:\n\n", $name);
foreach ($filter as $key => $value) {
$markdown .= sprintf(" * %s: %s\n", ucwords($key), trim(str_replace('\\\\', '\\', json_encode($value)), '"'));
2012-04-12 01:28:36 +02:00
}
$markdown .= "\n";
}
}
if (isset($data['parameters'])) {
$markdown .= "#### Parameters ####\n\n";
foreach ($data['parameters'] as $name => $parameter) {
if (!$parameter['readonly']) {
$markdown .= sprintf("%s:\n\n", $name);
$markdown .= sprintf(" * type: %s\n", $parameter['dataType']);
$markdown .= sprintf(" * required: %s\n", $parameter['required'] ? 'true' : 'false');
2012-08-27 13:25:03 -04:00
if (isset($parameter['description']) && !empty($parameter['description'])) {
$markdown .= sprintf(" * description: %s\n", $parameter['description']);
}
2012-08-27 13:25:03 -04:00
$markdown .= "\n";
}
2012-08-27 13:25:03 -04:00
}
}
if (isset($data['response'])) {
$markdown .= "#### Response ####\n\n";
foreach ($data['response'] as $name => $parameter) {
$markdown .= sprintf("%s:\n\n", $name);
$markdown .= sprintf(" * type: %s\n", $parameter['dataType']);
if (isset($parameter['description']) && !empty($parameter['description'])) {
$markdown .= sprintf(" * description: %s\n", $parameter['description']);
}
2012-04-12 01:28:36 +02:00
$markdown .= "\n";
}
}
return $markdown;
}
/**
* {@inheritdoc}
*/
protected function render(array $collection)
{
$markdown = '';
foreach ($collection as $section => $resources) {
$markdown .= $this->renderResourceSection($section, $resources);
$markdown .= "\n";
}
2012-04-13 14:27:51 +02:00
return trim($markdown);
}
private function renderResourceSection($section, array $resources)
{
if ('_others' !== $section) {
$markdown = sprintf("# %s #\n\n", $section);
2013-03-21 12:15:44 +01:00
} else {
$markdown = '';
}
foreach ($resources as $resource => $methods) {
if ('_others' === $section && 'others' !== $resource) {
2013-03-21 12:15:44 +01:00
$markdown .= sprintf("## %s ##\n\n", $resource);
} elseif ('others' !== $resource) {
2013-03-21 12:15:44 +01:00
$markdown .= sprintf("## %s ##\n\n", $resource);
}
foreach ($methods as $method) {
$markdown .= $this->renderOne($method);
$markdown .= "\n";
}
}
return $markdown;
}
2012-04-12 01:28:36 +02:00
}