PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/RestAPI/vendor/symfony/form/ResolvedFormType.php

https://gitlab.com/martinstti/silex-microframework-rest
PHP | 329 lines | 165 code | 51 blank | 113 comment | 23 complexity | e7e9460beb070e351b78571ed2055ba5 MD5 | raw file
  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\InvalidArgumentException;
  12. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  13. use Symfony\Component\EventDispatcher\EventDispatcher;
  14. use Symfony\Component\Form\Util\StringUtil;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. /**
  17. * A wrapper for a form type and its extensions.
  18. *
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. */
  21. class ResolvedFormType implements ResolvedFormTypeInterface
  22. {
  23. /**
  24. * @var string
  25. */
  26. private $name;
  27. /**
  28. * @var string
  29. */
  30. private $blockPrefix;
  31. /**
  32. * @var FormTypeInterface
  33. */
  34. private $innerType;
  35. /**
  36. * @var FormTypeExtensionInterface[]
  37. */
  38. private $typeExtensions;
  39. /**
  40. * @var ResolvedFormTypeInterface|null
  41. */
  42. private $parent;
  43. /**
  44. * @var OptionsResolver
  45. */
  46. private $optionsResolver;
  47. public function __construct(FormTypeInterface $innerType, array $typeExtensions = array(), ResolvedFormTypeInterface $parent = null)
  48. {
  49. $fqcn = get_class($innerType);
  50. $name = $innerType->getName();
  51. $hasCustomName = $name !== $fqcn;
  52. if (method_exists($innerType, 'getBlockPrefix')) {
  53. $reflector = new \ReflectionMethod($innerType, 'getName');
  54. $isOldOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Form\AbstractType';
  55. $reflector = new \ReflectionMethod($innerType, 'getBlockPrefix');
  56. $isNewOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Form\AbstractType';
  57. // Bundles compatible with both 2.3 and 2.8 should implement both methods
  58. // Anyone else should only override getBlockPrefix() if they actually
  59. // want to have a different block prefix than the default one
  60. if ($isOldOverwritten && !$isNewOverwritten) {
  61. @trigger_error(get_class($innerType).': The FormTypeInterface::getName() method is deprecated since version 2.8 and will be removed in 3.0. Remove it from your classes. Use getBlockPrefix() if you want to customize the template block prefix. This method will be added to the FormTypeInterface with Symfony 3.0.', E_USER_DEPRECATED);
  62. }
  63. $blockPrefix = $innerType->getBlockPrefix();
  64. } else {
  65. @trigger_error(get_class($innerType).': The FormTypeInterface::getBlockPrefix() method will be added in version 3.0. You should extend AbstractType or add it to your implementation.', E_USER_DEPRECATED);
  66. // Deal with classes that don't extend AbstractType
  67. // Calculate block prefix from the FQCN by default
  68. $blockPrefix = $hasCustomName ? $name : StringUtil::fqcnToBlockPrefix($fqcn);
  69. }
  70. // As of Symfony 2.8, getName() returns the FQCN by default
  71. // Otherwise check that the name matches the old naming restrictions
  72. if ($hasCustomName && !preg_match('/^[a-z0-9_]*$/i', $name)) {
  73. throw new InvalidArgumentException(sprintf(
  74. 'The "%s" form type name ("%s") is not valid. Names must only contain letters, numbers, and "_".',
  75. get_class($innerType),
  76. $name
  77. ));
  78. }
  79. foreach ($typeExtensions as $extension) {
  80. if (!$extension instanceof FormTypeExtensionInterface) {
  81. throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
  82. }
  83. }
  84. $this->name = $name;
  85. $this->blockPrefix = $blockPrefix;
  86. $this->innerType = $innerType;
  87. $this->typeExtensions = $typeExtensions;
  88. $this->parent = $parent;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getName()
  94. {
  95. return $this->name;
  96. }
  97. /**
  98. * Returns the prefix of the template block name for this type.
  99. *
  100. * @return string The prefix of the template block name
  101. */
  102. public function getBlockPrefix()
  103. {
  104. return $this->blockPrefix;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getParent()
  110. {
  111. return $this->parent;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function getInnerType()
  117. {
  118. return $this->innerType;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function getTypeExtensions()
  124. {
  125. return $this->typeExtensions;
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function createBuilder(FormFactoryInterface $factory, $name, array $options = array())
  131. {
  132. $options = $this->getOptionsResolver()->resolve($options);
  133. // Should be decoupled from the specific option at some point
  134. $dataClass = isset($options['data_class']) ? $options['data_class'] : null;
  135. $builder = $this->newBuilder($name, $dataClass, $factory, $options);
  136. $builder->setType($this);
  137. return $builder;
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function createView(FormInterface $form, FormView $parent = null)
  143. {
  144. return $this->newView($parent);
  145. }
  146. /**
  147. * Configures a form builder for the type hierarchy.
  148. *
  149. * @param FormBuilderInterface $builder The builder to configure.
  150. * @param array $options The options used for the configuration.
  151. */
  152. public function buildForm(FormBuilderInterface $builder, array $options)
  153. {
  154. if (null !== $this->parent) {
  155. $this->parent->buildForm($builder, $options);
  156. }
  157. $this->innerType->buildForm($builder, $options);
  158. foreach ($this->typeExtensions as $extension) {
  159. $extension->buildForm($builder, $options);
  160. }
  161. }
  162. /**
  163. * Configures a form view for the type hierarchy.
  164. *
  165. * This method is called before the children of the view are built.
  166. *
  167. * @param FormView $view The form view to configure.
  168. * @param FormInterface $form The form corresponding to the view.
  169. * @param array $options The options used for the configuration.
  170. */
  171. public function buildView(FormView $view, FormInterface $form, array $options)
  172. {
  173. if (null !== $this->parent) {
  174. $this->parent->buildView($view, $form, $options);
  175. }
  176. $this->innerType->buildView($view, $form, $options);
  177. foreach ($this->typeExtensions as $extension) {
  178. $extension->buildView($view, $form, $options);
  179. }
  180. }
  181. /**
  182. * Finishes a form view for the type hierarchy.
  183. *
  184. * This method is called after the children of the view have been built.
  185. *
  186. * @param FormView $view The form view to configure.
  187. * @param FormInterface $form The form corresponding to the view.
  188. * @param array $options The options used for the configuration.
  189. */
  190. public function finishView(FormView $view, FormInterface $form, array $options)
  191. {
  192. if (null !== $this->parent) {
  193. $this->parent->finishView($view, $form, $options);
  194. }
  195. $this->innerType->finishView($view, $form, $options);
  196. foreach ($this->typeExtensions as $extension) {
  197. /* @var FormTypeExtensionInterface $extension */
  198. $extension->finishView($view, $form, $options);
  199. }
  200. }
  201. /**
  202. * Returns the configured options resolver used for this type.
  203. *
  204. * @return \Symfony\Component\OptionsResolver\OptionsResolverInterface The options resolver.
  205. */
  206. public function getOptionsResolver()
  207. {
  208. if (null === $this->optionsResolver) {
  209. if (null !== $this->parent) {
  210. $this->optionsResolver = clone $this->parent->getOptionsResolver();
  211. } else {
  212. $this->optionsResolver = new OptionsResolver();
  213. }
  214. $this->innerType->setDefaultOptions($this->optionsResolver);
  215. if (method_exists($this->innerType, 'configureOptions')) {
  216. $reflector = new \ReflectionMethod($this->innerType, 'setDefaultOptions');
  217. $isOldOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Form\AbstractType';
  218. $reflector = new \ReflectionMethod($this->innerType, 'configureOptions');
  219. $isNewOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Form\AbstractType';
  220. if ($isOldOverwritten && !$isNewOverwritten) {
  221. @trigger_error(get_class($this->innerType).': The FormTypeInterface::setDefaultOptions() method is deprecated since version 2.7 and will be removed in 3.0. Use configureOptions() instead. This method will be added to the FormTypeInterface with Symfony 3.0.', E_USER_DEPRECATED);
  222. }
  223. } else {
  224. @trigger_error(get_class($this->innerType).': The FormTypeInterface::configureOptions() method will be added in Symfony 3.0. You should extend AbstractType or implement it in your classes.', E_USER_DEPRECATED);
  225. }
  226. foreach ($this->typeExtensions as $extension) {
  227. $extension->setDefaultOptions($this->optionsResolver);
  228. if (method_exists($extension, 'configureOptions')) {
  229. $reflector = new \ReflectionMethod($extension, 'setDefaultOptions');
  230. $isOldOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Form\AbstractTypeExtension';
  231. $reflector = new \ReflectionMethod($extension, 'configureOptions');
  232. $isNewOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Form\AbstractTypeExtension';
  233. if ($isOldOverwritten && !$isNewOverwritten) {
  234. @trigger_error(get_class($extension).': The FormTypeExtensionInterface::setDefaultOptions() method is deprecated since version 2.7 and will be removed in 3.0. Use configureOptions() instead. This method will be added to the FormTypeExtensionInterface with Symfony 3.0.', E_USER_DEPRECATED);
  235. }
  236. } else {
  237. @trigger_error(get_class($this->innerType).': The FormTypeExtensionInterface::configureOptions() method will be added in Symfony 3.0. You should extend AbstractTypeExtension or implement it in your classes.', E_USER_DEPRECATED);
  238. }
  239. }
  240. }
  241. return $this->optionsResolver;
  242. }
  243. /**
  244. * Creates a new builder instance.
  245. *
  246. * Override this method if you want to customize the builder class.
  247. *
  248. * @param string $name The name of the builder.
  249. * @param string $dataClass The data class.
  250. * @param FormFactoryInterface $factory The current form factory.
  251. * @param array $options The builder options.
  252. *
  253. * @return FormBuilderInterface The new builder instance.
  254. */
  255. protected function newBuilder($name, $dataClass, FormFactoryInterface $factory, array $options)
  256. {
  257. if ($this->innerType instanceof ButtonTypeInterface) {
  258. return new ButtonBuilder($name, $options);
  259. }
  260. if ($this->innerType instanceof SubmitButtonTypeInterface) {
  261. return new SubmitButtonBuilder($name, $options);
  262. }
  263. return new FormBuilder($name, $dataClass, new EventDispatcher(), $factory, $options);
  264. }
  265. /**
  266. * Creates a new view instance.
  267. *
  268. * Override this method if you want to customize the view class.
  269. *
  270. * @param FormView|null $parent The parent view, if available.
  271. *
  272. * @return FormView A new view instance.
  273. */
  274. protected function newView(FormView $parent = null)
  275. {
  276. return new FormView($parent);
  277. }
  278. }