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
|
|
|
|
{
|
2012-04-12 12:48:36 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function renderOne(array $data)
|
2012-04-12 01:28:36 +02:00
|
|
|
{
|
|
|
|
$markdown = sprintf("### `%s` %s ###\n", $data['method'], $data['uri']);
|
|
|
|
|
2013-11-14 10:28:42 +01:00
|
|
|
if (isset($data['deprecated']) && false !== $data['deprecated']) {
|
2013-03-18 08:40:03 +01:00
|
|
|
$markdown .= "### This method is deprecated ###";
|
|
|
|
$markdown .= "\n\n";
|
|
|
|
}
|
|
|
|
|
2012-04-12 17:24:38 +02:00
|
|
|
if (isset($data['description'])) {
|
|
|
|
$markdown .= sprintf("\n_%s_", $data['description']);
|
2012-04-12 01:28:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$markdown .= "\n\n";
|
|
|
|
|
2012-07-13 13:32:53 +01:00
|
|
|
if (isset($data['documentation']) && !empty($data['documentation'])) {
|
2012-07-13 15:26:36 +02:00
|
|
|
if (isset($data['description']) && 0 === strcmp($data['description'], $data['documentation'])) {
|
|
|
|
$markdown .= $data['documentation'];
|
|
|
|
$markdown .= "\n\n";
|
|
|
|
}
|
2012-07-13 13:32:53 +01:00
|
|
|
}
|
|
|
|
|
2012-04-12 01:28:36 +02:00
|
|
|
if (isset($data['requirements']) && !empty($data['requirements'])) {
|
|
|
|
$markdown .= "#### Requirements ####\n\n";
|
|
|
|
|
2012-04-19 20:27:27 +02:00
|
|
|
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-04-19 20:27:27 +02:00
|
|
|
}
|
|
|
|
|
2012-11-17 18:05:05 +01:00
|
|
|
if (!empty($infos['dataType'])) {
|
|
|
|
$markdown .= sprintf(" - Type: %s\n", $infos['dataType']);
|
2012-04-19 20:27:27 +02:00
|
|
|
}
|
2012-07-20 02:02:45 +02:00
|
|
|
|
2012-04-19 20:27:27 +02:00
|
|
|
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) {
|
2012-07-20 02:02:45 +02:00
|
|
|
$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";
|
|
|
|
|
2012-04-12 12:48:36 +02:00
|
|
|
foreach ($data['parameters'] as $name => $parameter) {
|
2012-08-31 11:56:48 -04:00
|
|
|
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
|
|
|
|
2012-08-31 11:56:48 -04:00
|
|
|
if (isset($parameter['description']) && !empty($parameter['description'])) {
|
|
|
|
$markdown .= sprintf(" * description: %s\n", $parameter['description']);
|
|
|
|
}
|
2014-06-17 03:15:55 -04:00
|
|
|
if (isset($parameter['default']) && !empty($parameter['default'])) {
|
|
|
|
$markdown .= sprintf(" * default value: %s\n", $parameter['default']);
|
|
|
|
}
|
2012-08-27 13:25:03 -04:00
|
|
|
|
2012-08-31 11:56:48 -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']);
|
2012-04-13 12:17:11 +02:00
|
|
|
|
|
|
|
if (isset($parameter['description']) && !empty($parameter['description'])) {
|
|
|
|
$markdown .= sprintf(" * description: %s\n", $parameter['description']);
|
|
|
|
}
|
|
|
|
|
2013-04-24 22:28:46 +02:00
|
|
|
if (null !== $parameter['sinceVersion'] || null !== $parameter['untilVersion']) {
|
|
|
|
$markdown .= " * versions: ";
|
|
|
|
if ($parameter['sinceVersion']) {
|
|
|
|
$markdown .= '>='.$parameter['sinceVersion'];
|
|
|
|
}
|
|
|
|
if ($parameter['untilVersion']) {
|
|
|
|
if ($parameter['sinceVersion']) {
|
|
|
|
$markdown .= ',';
|
|
|
|
}
|
|
|
|
$markdown .= '<='.$parameter['untilVersion'];
|
|
|
|
}
|
|
|
|
$markdown .= "\n";
|
|
|
|
}
|
|
|
|
|
2012-04-12 01:28:36 +02:00
|
|
|
$markdown .= "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $markdown;
|
|
|
|
}
|
2012-04-12 12:48:36 +02:00
|
|
|
|
2012-04-12 17:24:38 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-04-13 11:24:57 +02:00
|
|
|
protected function render(array $collection)
|
2012-04-12 17:24:38 +02:00
|
|
|
{
|
2012-04-13 11:24:57 +02:00
|
|
|
$markdown = '';
|
2013-03-17 16:40:31 +01:00
|
|
|
foreach ($collection as $section => $resources) {
|
|
|
|
$markdown .= $this->renderResourceSection($section, $resources);
|
2012-04-12 17:24:38 +02:00
|
|
|
$markdown .= "\n";
|
|
|
|
}
|
|
|
|
|
2012-04-13 14:27:51 +02:00
|
|
|
return trim($markdown);
|
2012-04-12 17:24:38 +02:00
|
|
|
}
|
|
|
|
|
2013-03-17 16:40:31 +01:00
|
|
|
private function renderResourceSection($section, array $resources)
|
2012-04-12 12:48:36 +02:00
|
|
|
{
|
2013-03-17 16:40:31 +01:00
|
|
|
if ('_others' !== $section) {
|
|
|
|
$markdown = sprintf("# %s #\n\n", $section);
|
2013-03-21 12:15:44 +01:00
|
|
|
} else {
|
|
|
|
$markdown = '';
|
2013-03-17 16:40:31 +01:00
|
|
|
}
|
2012-04-13 11:24:57 +02:00
|
|
|
|
2013-03-17 16:40:31 +01:00
|
|
|
foreach ($resources as $resource => $methods) {
|
|
|
|
if ('_others' === $section && 'others' !== $resource) {
|
2013-03-21 12:15:44 +01:00
|
|
|
$markdown .= sprintf("## %s ##\n\n", $resource);
|
2013-03-17 16:40:31 +01:00
|
|
|
} elseif ('others' !== $resource) {
|
2013-03-21 12:15:44 +01:00
|
|
|
$markdown .= sprintf("## %s ##\n\n", $resource);
|
2013-03-17 16:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($methods as $method) {
|
|
|
|
$markdown .= $this->renderOne($method);
|
|
|
|
$markdown .= "\n";
|
|
|
|
}
|
2012-04-12 12:48:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $markdown;
|
|
|
|
}
|
2012-04-12 01:28:36 +02:00
|
|
|
}
|