FormFactory.php 4.0 KB

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