vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 30

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form;
  11. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  12. class FormFactory implements FormFactoryInterface
  13. {
  14.     private $registry;
  15.     public function __construct(FormRegistryInterface $registry)
  16.     {
  17.         $this->registry $registry;
  18.     }
  19.     /**
  20.      * {@inheritdoc}
  21.      */
  22.     public function create($type 'Symfony\Component\Form\Extension\Core\Type\FormType'$data null, array $options = [])
  23.     {
  24.         return $this->createBuilder($type$data$options)->getForm();
  25.     }
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     public function createNamed($name$type 'Symfony\Component\Form\Extension\Core\Type\FormType'$data null, array $options = [])
  30.     {
  31.         return $this->createNamedBuilder($name$type$data$options)->getForm();
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function createForProperty($class$property$data null, array $options = [])
  37.     {
  38.         return $this->createBuilderForProperty($class$property$data$options)->getForm();
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      */
  43.     public function createBuilder($type 'Symfony\Component\Form\Extension\Core\Type\FormType'$data null, array $options = [])
  44.     {
  45.         if (!\is_string($type)) {
  46.             throw new UnexpectedTypeException($type'string');
  47.         }
  48.         return $this->createNamedBuilder($this->registry->getType($type)->getBlockPrefix(), $type$data$options);
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function createNamedBuilder($name$type 'Symfony\Component\Form\Extension\Core\Type\FormType'$data null, array $options = [])
  54.     {
  55.         if (null !== $data && !\array_key_exists('data'$options)) {
  56.             $options['data'] = $data;
  57.         }
  58.         if (!\is_string($type)) {
  59.             throw new UnexpectedTypeException($type'string');
  60.         }
  61.         $type $this->registry->getType($type);
  62.         $builder $type->createBuilder($this$name$options);
  63.         // Explicitly call buildForm() in order to be able to override either
  64.         // createBuilder() or buildForm() in the resolved form type
  65.         $type->buildForm($builder$builder->getOptions());
  66.         return $builder;
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     public function createBuilderForProperty($class$property$data null, array $options = [])
  72.     {
  73.         if (null === $guesser $this->registry->getTypeGuesser()) {
  74.             return $this->createNamedBuilder($property'Symfony\Component\Form\Extension\Core\Type\TextType'$data$options);
  75.         }
  76.         $typeGuess $guesser->guessType($class$property);
  77.         $maxLengthGuess $guesser->guessMaxLength($class$property);
  78.         $requiredGuess $guesser->guessRequired($class$property);
  79.         $patternGuess $guesser->guessPattern($class$property);
  80.         $type $typeGuess $typeGuess->getType() : 'Symfony\Component\Form\Extension\Core\Type\TextType';
  81.         $maxLength $maxLengthGuess $maxLengthGuess->getValue() : null;
  82.         $pattern $patternGuess $patternGuess->getValue() : null;
  83.         if (null !== $pattern) {
  84.             $options array_replace_recursive(['attr' => ['pattern' => $pattern]], $options);
  85.         }
  86.         if (null !== $maxLength) {
  87.             $options array_replace_recursive(['attr' => ['maxlength' => $maxLength]], $options);
  88.         }
  89.         if ($requiredGuess) {
  90.             $options array_merge(['required' => $requiredGuess->getValue()], $options);
  91.         }
  92.         // user options may override guessed options
  93.         if ($typeGuess) {
  94.             $attrs = [];
  95.             $typeGuessOptions $typeGuess->getOptions();
  96.             if (isset($typeGuessOptions['attr']) && isset($options['attr'])) {
  97.                 $attrs = ['attr' => array_merge($typeGuessOptions['attr'], $options['attr'])];
  98.             }
  99.             $options array_merge($typeGuessOptions$options$attrs);
  100.         }
  101.         return $this->createNamedBuilder($property$type$data$options);
  102.     }
  103. }