vendor/symfony/form/FormFactory.php line 66

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. use Symfony\Component\Form\Extension\Core\Type\FormType;
  13. use Symfony\Component\Form\Extension\Core\Type\TextType;
  14. class FormFactory implements FormFactoryInterface
  15. {
  16.     private $registry;
  17.     public function __construct(FormRegistryInterface $registry)
  18.     {
  19.         $this->registry $registry;
  20.     }
  21.     /**
  22.      * {@inheritdoc}
  23.      */
  24.     public function create($type FormType::class, $data null, array $options = [])
  25.     {
  26.         return $this->createBuilder($type$data$options)->getForm();
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public function createNamed($name$type FormType::class, $data null, array $options = [])
  32.     {
  33.         return $this->createNamedBuilder($name$type$data$options)->getForm();
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function createForProperty($class$property$data null, array $options = [])
  39.     {
  40.         return $this->createBuilderForProperty($class$property$data$options)->getForm();
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function createBuilder($type FormType::class, $data null, array $options = [])
  46.     {
  47.         if (!\is_string($type)) {
  48.             throw new UnexpectedTypeException($type'string');
  49.         }
  50.         return $this->createNamedBuilder($this->registry->getType($type)->getBlockPrefix(), $type$data$options);
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function createNamedBuilder($name$type FormType::class, $data null, array $options = [])
  56.     {
  57.         if (null !== $data && !\array_key_exists('data'$options)) {
  58.             $options['data'] = $data;
  59.         }
  60.         if (!\is_string($type)) {
  61.             throw new UnexpectedTypeException($type'string');
  62.         }
  63.         $type $this->registry->getType($type);
  64.         $builder $type->createBuilder($this, (string) $name$options);
  65.         // Explicitly call buildForm() in order to be able to override either
  66.         // createBuilder() or buildForm() in the resolved form type
  67.         $type->buildForm($builder$builder->getOptions());
  68.         return $builder;
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function createBuilderForProperty($class$property$data null, array $options = [])
  74.     {
  75.         if (null === $guesser $this->registry->getTypeGuesser()) {
  76.             return $this->createNamedBuilder($propertyTextType::class, $data$options);
  77.         }
  78.         $typeGuess $guesser->guessType($class$property);
  79.         $maxLengthGuess $guesser->guessMaxLength($class$property);
  80.         $requiredGuess $guesser->guessRequired($class$property);
  81.         $patternGuess $guesser->guessPattern($class$property);
  82.         $type $typeGuess $typeGuess->getType() : TextType::class;
  83.         $maxLength $maxLengthGuess $maxLengthGuess->getValue() : null;
  84.         $pattern $patternGuess $patternGuess->getValue() : null;
  85.         if (null !== $pattern) {
  86.             $options array_replace_recursive(['attr' => ['pattern' => $pattern]], $options);
  87.         }
  88.         if (null !== $maxLength) {
  89.             $options array_replace_recursive(['attr' => ['maxlength' => $maxLength]], $options);
  90.         }
  91.         if ($requiredGuess) {
  92.             $options array_merge(['required' => $requiredGuess->getValue()], $options);
  93.         }
  94.         // user options may override guessed options
  95.         if ($typeGuess) {
  96.             $attrs = [];
  97.             $typeGuessOptions $typeGuess->getOptions();
  98.             if (isset($typeGuessOptions['attr']) && isset($options['attr'])) {
  99.                 $attrs = ['attr' => array_merge($typeGuessOptions['attr'], $options['attr'])];
  100.             }
  101.             $options array_merge($typeGuessOptions$options$attrs);
  102.         }
  103.         return $this->createNamedBuilder($property$type$data$options);
  104.     }
  105. }