mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 23:59:26 +03:00
Merge pull request #76 from evillemez/nested_formatters
Implemented nested parameter handling in formatters
This commit is contained in:
commit
be07de7ffe
@ -51,4 +51,58 @@ abstract class AbstractFormatter implements FormatterInterface
|
|||||||
* @return string|array
|
* @return string|array
|
||||||
*/
|
*/
|
||||||
abstract protected function render(array $collection);
|
abstract protected function render(array $collection);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compresses nested parameters into a flat by changing the parameter
|
||||||
|
* names to strings which contain the nested property names, for example:
|
||||||
|
* `user[group][name]`
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @param string $parentName
|
||||||
|
* @param boolean $ignoreNestedReadOnly
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
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']
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isset($info['children']) && (!$info['readonly'] || !$ignoreNestedReadOnly)) {
|
||||||
|
foreach ($this->compressNestedParameters($info['children'], $newName, $ignoreNestedReadOnly) as $nestedItemName => $nestedItemData) {
|
||||||
|
$newParams[$nestedItemName] = $nestedItemData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $newParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new property name, taking into account whether or not the property
|
||||||
|
* is an array of some other data type.
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param array $data
|
||||||
|
* @param string $parentName
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getNewName($name, $data, $parentName = null)
|
||||||
|
{
|
||||||
|
$newName = ($parentName) ? sprintf("%s[%s]", $parentName, $name) : $name;
|
||||||
|
|
||||||
|
$array = (false === strpos($data['dataType'], "array of")) ? "" : "[]";
|
||||||
|
|
||||||
|
return sprintf("%s%s", $newName, $array);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -85,6 +85,14 @@ class HtmlFormatter extends AbstractFormatter
|
|||||||
*/
|
*/
|
||||||
protected function renderOne(array $data)
|
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(
|
return $this->engine->render('NelmioApiDocBundle::resource.html.twig', array_merge(
|
||||||
array('data' => $data, 'displayContent' => true),
|
array('data' => $data, 'displayContent' => true),
|
||||||
$this->getGlobalVars()
|
$this->getGlobalVars()
|
||||||
@ -96,8 +104,25 @@ class HtmlFormatter extends AbstractFormatter
|
|||||||
*/
|
*/
|
||||||
protected function render(array $collection)
|
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(
|
return $this->engine->render('NelmioApiDocBundle::resources.html.twig', array_merge(
|
||||||
array('resources' => $collection),
|
array('resources' => $processedCollection),
|
||||||
$this->getGlobalVars()
|
$this->getGlobalVars()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -117,4 +142,5 @@ class HtmlFormatter extends AbstractFormatter
|
|||||||
'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'),
|
'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,14 @@ class MarkdownFormatter extends AbstractFormatter
|
|||||||
*/
|
*/
|
||||||
protected function renderOne(array $data)
|
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']);
|
$markdown = sprintf("### `%s` %s ###\n", $data['method'], $data['uri']);
|
||||||
|
|
||||||
if (isset($data['description'])) {
|
if (isset($data['description'])) {
|
||||||
@ -73,10 +81,10 @@ class MarkdownFormatter extends AbstractFormatter
|
|||||||
$markdown .= "#### Parameters ####\n\n";
|
$markdown .= "#### Parameters ####\n\n";
|
||||||
|
|
||||||
foreach ($data['parameters'] as $name => $parameter) {
|
foreach ($data['parameters'] as $name => $parameter) {
|
||||||
|
if (!$parameter['readonly']) {
|
||||||
$markdown .= sprintf("%s:\n\n", $name);
|
$markdown .= sprintf("%s:\n\n", $name);
|
||||||
$markdown .= sprintf(" * type: %s\n", $parameter['dataType']);
|
$markdown .= sprintf(" * type: %s\n", $parameter['dataType']);
|
||||||
$markdown .= sprintf(" * required: %s\n", $parameter['required'] ? 'true' : 'false');
|
$markdown .= sprintf(" * required: %s\n", $parameter['required'] ? 'true' : 'false');
|
||||||
$markdown .= sprintf(" * readonly: %s\n", $parameter['readonly'] ? 'true' : 'false');
|
|
||||||
|
|
||||||
if (isset($parameter['description']) && !empty($parameter['description'])) {
|
if (isset($parameter['description']) && !empty($parameter['description'])) {
|
||||||
$markdown .= sprintf(" * description: %s\n", $parameter['description']);
|
$markdown .= sprintf(" * description: %s\n", $parameter['description']);
|
||||||
@ -85,6 +93,7 @@ class MarkdownFormatter extends AbstractFormatter
|
|||||||
$markdown .= "\n";
|
$markdown .= "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($data['response'])) {
|
if (isset($data['response'])) {
|
||||||
$markdown .= "#### Response ####\n\n";
|
$markdown .= "#### Response ####\n\n";
|
||||||
@ -92,8 +101,6 @@ class MarkdownFormatter extends AbstractFormatter
|
|||||||
foreach ($data['response'] as $name => $parameter) {
|
foreach ($data['response'] as $name => $parameter) {
|
||||||
$markdown .= sprintf("%s:\n\n", $name);
|
$markdown .= sprintf("%s:\n\n", $name);
|
||||||
$markdown .= sprintf(" * type: %s\n", $parameter['dataType']);
|
$markdown .= sprintf(" * type: %s\n", $parameter['dataType']);
|
||||||
$markdown .= sprintf(" * required: %s\n", $parameter['required'] ? 'true' : 'false');
|
|
||||||
$markdown .= sprintf(" * readonly: %s\n", $parameter['readonly'] ? 'true' : 'false');
|
|
||||||
|
|
||||||
if (isset($parameter['description']) && !empty($parameter['description'])) {
|
if (isset($parameter['description']) && !empty($parameter['description'])) {
|
||||||
$markdown .= sprintf(" * description: %s\n", $parameter['description']);
|
$markdown .= sprintf(" * description: %s\n", $parameter['description']);
|
||||||
|
@ -162,9 +162,8 @@ class JmsMetadataParser implements ParserInterface
|
|||||||
{
|
{
|
||||||
$ref = new \ReflectionClass($className);
|
$ref = new \ReflectionClass($className);
|
||||||
$extracted = $this->commentExtractor->getDocCommentText($ref->getProperty($propertyName));
|
$extracted = $this->commentExtractor->getDocCommentText($ref->getProperty($propertyName));
|
||||||
$description = !empty($extracted) ? $extracted : "No description.";
|
|
||||||
|
|
||||||
return $description;
|
return !empty($extracted) ? $extracted : "No description.";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -83,20 +83,17 @@ a:
|
|||||||
|
|
||||||
* type: string
|
* type: string
|
||||||
* required: true
|
* required: true
|
||||||
* readonly: false
|
|
||||||
* description: A nice description
|
* description: A nice description
|
||||||
|
|
||||||
b:
|
b:
|
||||||
|
|
||||||
* type: string
|
* type: string
|
||||||
* required: false
|
* required: false
|
||||||
* readonly: false
|
|
||||||
|
|
||||||
c:
|
c:
|
||||||
|
|
||||||
* type: boolean
|
* type: boolean
|
||||||
* required: true
|
* required: true
|
||||||
* readonly: false
|
|
||||||
|
|
||||||
|
|
||||||
### `POST` /tests.{_format} ###
|
### `POST` /tests.{_format} ###
|
||||||
@ -114,20 +111,17 @@ a:
|
|||||||
|
|
||||||
* type: string
|
* type: string
|
||||||
* required: true
|
* required: true
|
||||||
* readonly: false
|
|
||||||
* description: A nice description
|
* description: A nice description
|
||||||
|
|
||||||
b:
|
b:
|
||||||
|
|
||||||
* type: string
|
* type: string
|
||||||
* required: false
|
* required: false
|
||||||
* readonly: false
|
|
||||||
|
|
||||||
c:
|
c:
|
||||||
|
|
||||||
* type: boolean
|
* type: boolean
|
||||||
* required: true
|
* required: true
|
||||||
* readonly: false
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -143,7 +137,6 @@ a:
|
|||||||
|
|
||||||
* type: string
|
* type: string
|
||||||
* required: true
|
* required: true
|
||||||
* readonly: false
|
|
||||||
* description: A nice description
|
* description: A nice description
|
||||||
|
|
||||||
|
|
||||||
@ -172,44 +165,60 @@ foo:
|
|||||||
|
|
||||||
* type: string
|
* type: string
|
||||||
* required: false
|
* required: false
|
||||||
* readonly: false
|
|
||||||
* description: No description.
|
|
||||||
|
|
||||||
bar:
|
|
||||||
|
|
||||||
* type: DateTime
|
|
||||||
* required: false
|
|
||||||
* readonly: true
|
|
||||||
* description: No description.
|
* description: No description.
|
||||||
|
|
||||||
number:
|
number:
|
||||||
|
|
||||||
* type: double
|
* type: double
|
||||||
* required: false
|
* required: false
|
||||||
* readonly: false
|
|
||||||
* description: No description.
|
* description: No description.
|
||||||
|
|
||||||
arr:
|
arr:
|
||||||
|
|
||||||
* type: array
|
* type: array
|
||||||
* required: false
|
* required: false
|
||||||
* readonly: false
|
|
||||||
* description: No description.
|
* description: No description.
|
||||||
|
|
||||||
nested:
|
nested:
|
||||||
|
|
||||||
* type: object (JmsNested)
|
* type: object (JmsNested)
|
||||||
* required: false
|
* required: false
|
||||||
* readonly: false
|
|
||||||
* description: No description.
|
* description: No description.
|
||||||
|
|
||||||
nestedArray:
|
nested[bar]:
|
||||||
|
|
||||||
|
* type: string
|
||||||
|
* required: false
|
||||||
|
* description: No description.
|
||||||
|
|
||||||
|
nested[baz][]:
|
||||||
|
|
||||||
|
* type: array of integers
|
||||||
|
* required: false
|
||||||
|
* description: Epic description.
|
||||||
|
|
||||||
|
With multiple lines.
|
||||||
|
|
||||||
|
nestedArray[]:
|
||||||
|
|
||||||
* type: array of objects (JmsNested)
|
* type: array of objects (JmsNested)
|
||||||
* required: false
|
* required: false
|
||||||
* readonly: false
|
|
||||||
* description: No description.
|
* description: No description.
|
||||||
|
|
||||||
|
nestedArray[][bar]:
|
||||||
|
|
||||||
|
* type: string
|
||||||
|
* required: false
|
||||||
|
* description: No description.
|
||||||
|
|
||||||
|
nestedArray[][baz][]:
|
||||||
|
|
||||||
|
* type: array of integers
|
||||||
|
* required: false
|
||||||
|
* description: Epic description.
|
||||||
|
|
||||||
|
With multiple lines.
|
||||||
|
|
||||||
|
|
||||||
### `GET` /jms-return-test ###
|
### `GET` /jms-return-test ###
|
||||||
|
|
||||||
@ -220,8 +229,6 @@ _Testing return_
|
|||||||
a:
|
a:
|
||||||
|
|
||||||
* type: string
|
* type: string
|
||||||
* required: true
|
|
||||||
* readonly: false
|
|
||||||
* description: A nice description
|
* description: A nice description
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user