From d121e28d69485780754ff66b1c65e54352e2f626 Mon Sep 17 00:00:00 2001 From: pedro Date: Fri, 13 Mar 2015 15:33:45 +0100 Subject: [PATCH] Add form options to ApiDoc input Update index.md Remove unused variable. $arguments is now named $options --- Extractor/ApiDocExtractor.php | 7 ++++--- Parser/FormTypeParser.php | 16 ++++++++-------- Resources/doc/index.md | 8 ++++++++ Tests/Parser/FormTypeParserTest.php | 14 ++++++++------ 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index 56d6589..7bb81e7 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -17,11 +17,11 @@ use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Nelmio\ApiDocBundle\DataTypes; use Nelmio\ApiDocBundle\Parser\ParserInterface; use Nelmio\ApiDocBundle\Parser\PostParserInterface; -use Symfony\Component\Routing\Route; -use Symfony\Component\Routing\RouterInterface; +use Nelmio\ApiDocBundle\Util\DocCommentExtractor; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; -use Nelmio\ApiDocBundle\Util\DocCommentExtractor; +use Symfony\Component\Routing\Route; +use Symfony\Component\Routing\RouterInterface; class ApiDocExtractor { @@ -366,6 +366,7 @@ class ApiDocExtractor $defaults = array( 'class' => '', 'groups' => array(), + 'options' => array(), ); // normalize strings diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index 5359aa9..1a3d8b3 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -12,16 +12,16 @@ namespace Nelmio\ApiDocBundle\Parser; use Nelmio\ApiDocBundle\DataTypes; -use Symfony\Component\Form\Exception\UnexpectedTypeException; +use Symfony\Component\Form\Exception\FormException; use Symfony\Component\Form\Exception\InvalidArgumentException; +use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface; use Symfony\Component\Form\Extension\Core\View\ChoiceView; use Symfony\Component\Form\Form; +use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\ResolvedFormTypeInterface; use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; -use Symfony\Component\Form\FormFactoryInterface; -use Symfony\Component\Form\Exception\FormException; class FormTypeParser implements ParserInterface { @@ -63,9 +63,10 @@ class FormTypeParser implements ParserInterface public function supports(array $item) { $className = $item['class']; + $options = $item['options']; try { - if ($this->createForm($className)) { + if ($this->createForm($className, null, $options)) { return true; } } catch (FormException $e) { @@ -82,18 +83,17 @@ class FormTypeParser implements ParserInterface */ public function parse(array $item) { - $type = $item['class']; + $type = $item['class']; + $options = $item['options']; if ($this->implementsType($type)) { $type = $this->getTypeInstance($type); } - $form = $this->formFactory->create($type); + $form = $this->formFactory->create($type, null, $options); $name = array_key_exists('name', $item) ? $item['name'] : $form->getName(); - $options = null; - if (empty($name)) { return $this->parseForm($form); } diff --git a/Resources/doc/index.md b/Resources/doc/index.md index e038589..186a559 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -272,6 +272,14 @@ input = { } ``` +You can also add some options to pass to the form. You just have to use the `options` key: +``` +input = { + "class" = "your_form_type", + "options" = {"method" => "PUT"}, +} +``` + #### Used Parsers By default, all registered parsers are used, but sometimes you may want to diff --git a/Tests/Parser/FormTypeParserTest.php b/Tests/Parser/FormTypeParserTest.php index 5a11485..e91e19a 100644 --- a/Tests/Parser/FormTypeParserTest.php +++ b/Tests/Parser/FormTypeParserTest.php @@ -34,7 +34,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase { return array( array( - array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType'), + array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType', 'options' => array()), array( 'a' => array( 'dataType' => 'string', @@ -75,7 +75,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase ) ), array( - array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CollectionType'), + array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CollectionType', 'options' => array()), array( 'collection_type' => array( 'dataType' => 'object (CollectionType)', @@ -150,6 +150,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase array( 'class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CollectionType', 'name' => '', + 'options' => array(), ), array( 'a' => array( @@ -214,6 +215,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase array( 'class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CollectionType', 'name' => null, + 'options' => array(), ), array( 'a' => array( @@ -275,7 +277,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase ), ), array( - array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\ImprovedTestType'), + array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\ImprovedTestType', 'options' => array()), array( 'dt1' => array( 'dataType' => 'datetime', @@ -386,7 +388,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase ), ), array( - array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CompoundType'), + array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CompoundType', 'options' => array()), array ( 'sub_form' => array ( @@ -475,7 +477,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase ), ), array( - array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\RequireConstructionType'), + array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\RequireConstructionType', 'options' => array()), array( 'require_construction_type' => array( 'dataType' => 'object (RequireConstructionType)', @@ -500,7 +502,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase ), ), array( - array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\DependencyType'), + array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\DependencyType', 'options' => array()), array( 'dependency_type' => array( 'dataType' => 'object (DependencyType)',