vendor/symfony/form/FormFactory.php line 61

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