FormFactory.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Type\FormTypeInterface;
  12. use Symfony\Component\Form\Type\Loader\TypeLoaderInterface;
  13. use Symfony\Component\Form\Type\Guesser\TypeGuesserInterface;
  14. use Symfony\Component\Form\Type\Guesser\Guess;
  15. use Symfony\Component\Form\Exception\FormException;
  16. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  17. class FormFactory implements FormFactoryInterface
  18. {
  19. private $typeLoader;
  20. private $guessers = array();
  21. public function __construct(TypeLoaderInterface $typeLoader, array $guessers = array())
  22. {
  23. foreach ($guessers as $guesser) {
  24. if (!$guesser instanceof TypeGuesserInterface) {
  25. throw new UnexpectedTypeException($guesser, 'Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  26. }
  27. }
  28. $this->typeLoader = $typeLoader;
  29. $this->guessers = $guessers;
  30. }
  31. public function createBuilder($type, $name = null, array $options = array())
  32. {
  33. // TODO $type can be FQN of a type class
  34. $builder = null;
  35. $types = array();
  36. $knownOptions = array();
  37. $passedOptions = array_keys($options);
  38. // TESTME
  39. if (null === $name && preg_match('/\w+$/', $type, $matches)) {
  40. $name = $matches[0];
  41. }
  42. while (null !== $type) {
  43. // TODO check if type exists
  44. $type = $this->typeLoader->getType($type);
  45. array_unshift($types, $type);
  46. $defaultOptions = $type->getDefaultOptions($options);
  47. $options = array_merge($defaultOptions, $options);
  48. $knownOptions = array_merge($knownOptions, array_keys($defaultOptions));
  49. $type = $type->getParent($options);
  50. }
  51. $diff = array_diff($passedOptions, $knownOptions);
  52. if (count($diff) > 0) {
  53. throw new FormException(sprintf('The options "%s" do not exist', implode('", "', $diff)));
  54. }
  55. for ($i = 0, $l = count($types); $i < $l && !$builder; ++$i) {
  56. $builder = $types[$i]->createBuilder($options);
  57. }
  58. // TODO check if instance exists
  59. $builder->setName($name);
  60. $builder->setTypes($types);
  61. $builder->setFormFactory($this);
  62. foreach ($types as $type) {
  63. $type->configure($builder, $options);
  64. }
  65. return $builder;
  66. }
  67. public function create($type, $name = null, array $options = array())
  68. {
  69. return $this->createBuilder($type, $name, $options)->getForm();
  70. }
  71. public function createBuilderForProperty($class, $property, array $options = array())
  72. {
  73. // guess field class and options
  74. $typeGuess = $this->guess(function ($guesser) use ($class, $property) {
  75. return $guesser->guessType($class, $property);
  76. });
  77. // guess maximum length
  78. $maxLengthGuess = $this->guess(function ($guesser) use ($class, $property) {
  79. return $guesser->guessMaxLength($class, $property);
  80. });
  81. // guess whether field is required
  82. $requiredGuess = $this->guess(function ($guesser) use ($class, $property) {
  83. return $guesser->guessRequired($class, $property);
  84. });
  85. // construct field
  86. $type = $typeGuess ? $typeGuess->getType() : 'text';
  87. if ($maxLengthGuess) {
  88. $options = array_merge(array('max_length' => $maxLengthGuess->getValue()), $options);
  89. }
  90. if ($requiredGuess) {
  91. $options = array_merge(array('required' => $requiredGuess->getValue()), $options);
  92. }
  93. // user options may override guessed options
  94. if ($typeGuess) {
  95. $options = array_merge($typeGuess->getOptions(), $options);
  96. }
  97. return $this->createBuilder($type, $property, $options);
  98. }
  99. /**
  100. * @inheritDoc
  101. */
  102. public function createForProperty($class, $property, array $options = array())
  103. {
  104. return $this->createBuilderForProperty($class, $property, $options)->getForm();
  105. }
  106. /**
  107. * Executes a closure for each guesser and returns the best guess from the
  108. * return values
  109. *
  110. * @param \Closure $closure The closure to execute. Accepts a guesser as
  111. * argument and should return a FieldFactoryGuess
  112. * instance
  113. * @return FieldFactoryGuess The guess with the highest confidence
  114. */
  115. protected function guess(\Closure $closure)
  116. {
  117. $guesses = array();
  118. foreach ($this->guessers as $guesser) {
  119. if ($guess = $closure($guesser)) {
  120. $guesses[] = $guess;
  121. }
  122. }
  123. return Guess::getBestGuess($guesses);
  124. }
  125. }