diff --git a/Form/Extension/DescriptionFieldTypeExtension.php b/Form/Extension/DescriptionFieldTypeExtension.php new file mode 100644 index 0000000..c309bb1 --- /dev/null +++ b/Form/Extension/DescriptionFieldTypeExtension.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Nelmio\ApiDocBundle\Form\Extension; + +use Symfony\Component\Form\AbstractTypeExtension; +use Symfony\Component\Form\FormBuilder; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; + +class DescriptionFieldTypeExtension extends AbstractTypeExtension +{ + /** + * {@inheritdoc} + */ + public function buildForm(FormBuilder $builder, array $options) + { + $builder->setAttribute('description', $options['description']); + } + + /** + * {@inheritdoc} + */ + public function buildView(FormView $view, FormInterface $form) + { + $view->set('description', $form->getAttribute('description')); + } + + /** + * {@inheritdoc} + */ + public function getDefaultOptions() + { + return array( + 'description' => '', + ); + } + + /** + * {@inheritdoc} + */ + public function getExtendedType() + { + return 'field'; + } +} diff --git a/Formatter/MarkdownFormatter.php b/Formatter/MarkdownFormatter.php index dc748e3..a3859ec 100644 --- a/Formatter/MarkdownFormatter.php +++ b/Formatter/MarkdownFormatter.php @@ -56,7 +56,12 @@ class MarkdownFormatter extends AbstractFormatter foreach ($data['parameters'] as $name => $parameter) { $markdown .= sprintf("%s:\n\n", $name); $markdown .= sprintf(" * type: %s\n", $parameter['dataType']); - $markdown .= sprintf(" * is_required: %s\n", $parameter['required'] ? 'true' : 'false'); + $markdown .= sprintf(" * required: %s\n", $parameter['required'] ? 'true' : 'false'); + + if (isset($parameter['description']) && !empty($parameter['description'])) { + $markdown .= sprintf(" * description: %s\n", $parameter['description']); + } + $markdown .= "\n"; } } diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index e533054..643bd9f 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -45,6 +45,7 @@ class FormTypeParser * Returns an array of data where each data is an array with the following keys: * - dataType * - required + * - description * * @param AbstractType $type * @return array @@ -65,8 +66,9 @@ class FormTypeParser } $parameters[$name] = array( - 'dataType' => $bestType, - 'required' => $b->getRequired() + 'dataType' => $bestType, + 'required' => $b->getRequired(), + 'description' => $b->getAttribute('description'), ); } diff --git a/README.md b/README.md index 162c811..dc28f2c 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,27 @@ parameters, and you can document them as you want, but keep in mind to be consis If you set a `formType`, then the bundle automatically extracts parameters based on the given type, and determines for each parameter its data type, and if it's required or not. +You can add an extra option named `description` on each field: + +``` php +add('note', null, array( + 'description' => 'this is a note', + )); + + // ... + } +} +``` + The bundle will also get information from the routing definition (`requirements`, `pattern`, etc), so to get the best out of it you should define strict _method requirements etc. diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 01a4790..fd7c565 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -5,6 +5,7 @@ Nelmio\ApiDocBundle\Extractor\ApiDocExtractor + Nelmio\ApiDocBundle\Form\Extension\DescriptionFieldTypeExtension @@ -12,6 +13,9 @@ + + + diff --git a/Resources/views/method.html.twig b/Resources/views/method.html.twig index 93e17a5..650c658 100644 --- a/Resources/views/method.html.twig +++ b/Resources/views/method.html.twig @@ -69,6 +69,7 @@ Parameter Type Required? + Description @@ -77,6 +78,7 @@ {{ name }} {{ infos.dataType }} {{ infos.required ? 'true' : 'false' }} + {{ infos.description }} {% endfor %}