ButtonBuilder.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Form\Exception\BadMethodCallException;
  13. use Symfony\Component\Form\Exception\InvalidArgumentException;
  14. /**
  15. * A builder for {@link Button} instances.
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. */
  19. class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
  20. {
  21. /**
  22. * @var bool
  23. */
  24. protected $locked = false;
  25. /**
  26. * @var bool
  27. */
  28. private $disabled;
  29. /**
  30. * @var ResolvedFormTypeInterface
  31. */
  32. private $type;
  33. /**
  34. * @var string
  35. */
  36. private $name;
  37. /**
  38. * @var array
  39. */
  40. private $attributes = array();
  41. /**
  42. * @var array
  43. */
  44. private $options;
  45. /**
  46. * Creates a new button builder.
  47. *
  48. * @param string $name The name of the button
  49. * @param array $options The button's options
  50. *
  51. * @throws InvalidArgumentException if the name is empty
  52. */
  53. public function __construct($name, array $options = array())
  54. {
  55. $name = (string) $name;
  56. if ('' === $name) {
  57. throw new InvalidArgumentException('Buttons cannot have empty names.');
  58. }
  59. $this->name = $name;
  60. $this->options = $options;
  61. }
  62. /**
  63. * Unsupported method.
  64. *
  65. * This method should not be invoked.
  66. *
  67. * @param string|int|FormBuilderInterface $child
  68. * @param string|FormTypeInterface $type
  69. * @param array $options
  70. *
  71. * @throws BadMethodCallException
  72. */
  73. public function add($child, $type = null, array $options = array())
  74. {
  75. throw new BadMethodCallException('Buttons cannot have children.');
  76. }
  77. /**
  78. * Unsupported method.
  79. *
  80. * This method should not be invoked.
  81. *
  82. * @param string $name
  83. * @param string|FormTypeInterface $type
  84. * @param array $options
  85. *
  86. * @throws BadMethodCallException
  87. */
  88. public function create($name, $type = null, array $options = array())
  89. {
  90. throw new BadMethodCallException('Buttons cannot have children.');
  91. }
  92. /**
  93. * Unsupported method.
  94. *
  95. * This method should not be invoked.
  96. *
  97. * @param string $name
  98. *
  99. * @throws BadMethodCallException
  100. */
  101. public function get($name)
  102. {
  103. throw new BadMethodCallException('Buttons cannot have children.');
  104. }
  105. /**
  106. * Unsupported method.
  107. *
  108. * This method should not be invoked.
  109. *
  110. * @param string $name
  111. *
  112. * @throws BadMethodCallException
  113. */
  114. public function remove($name)
  115. {
  116. throw new BadMethodCallException('Buttons cannot have children.');
  117. }
  118. /**
  119. * Unsupported method.
  120. *
  121. * @param string $name
  122. *
  123. * @return bool Always returns false
  124. */
  125. public function has($name)
  126. {
  127. return false;
  128. }
  129. /**
  130. * Returns the children.
  131. *
  132. * @return array Always returns an empty array
  133. */
  134. public function all()
  135. {
  136. return array();
  137. }
  138. /**
  139. * Creates the button.
  140. *
  141. * @return Button The button
  142. */
  143. public function getForm()
  144. {
  145. return new Button($this->getFormConfig());
  146. }
  147. /**
  148. * Unsupported method.
  149. *
  150. * This method should not be invoked.
  151. *
  152. * @param string $eventName
  153. * @param callable $listener
  154. * @param int $priority
  155. *
  156. * @throws BadMethodCallException
  157. */
  158. public function addEventListener($eventName, $listener, $priority = 0)
  159. {
  160. throw new BadMethodCallException('Buttons do not support event listeners.');
  161. }
  162. /**
  163. * Unsupported method.
  164. *
  165. * This method should not be invoked.
  166. *
  167. * @throws BadMethodCallException
  168. */
  169. public function addEventSubscriber(EventSubscriberInterface $subscriber)
  170. {
  171. throw new BadMethodCallException('Buttons do not support event subscribers.');
  172. }
  173. /**
  174. * Unsupported method.
  175. *
  176. * This method should not be invoked.
  177. *
  178. * @param DataTransformerInterface $viewTransformer
  179. * @param bool $forcePrepend
  180. *
  181. * @throws BadMethodCallException
  182. */
  183. public function addViewTransformer(DataTransformerInterface $viewTransformer, $forcePrepend = false)
  184. {
  185. throw new BadMethodCallException('Buttons do not support data transformers.');
  186. }
  187. /**
  188. * Unsupported method.
  189. *
  190. * This method should not be invoked.
  191. *
  192. * @throws BadMethodCallException
  193. */
  194. public function resetViewTransformers()
  195. {
  196. throw new BadMethodCallException('Buttons do not support data transformers.');
  197. }
  198. /**
  199. * Unsupported method.
  200. *
  201. * This method should not be invoked.
  202. *
  203. * @param DataTransformerInterface $modelTransformer
  204. * @param bool $forceAppend
  205. *
  206. * @throws BadMethodCallException
  207. */
  208. public function addModelTransformer(DataTransformerInterface $modelTransformer, $forceAppend = false)
  209. {
  210. throw new BadMethodCallException('Buttons do not support data transformers.');
  211. }
  212. /**
  213. * Unsupported method.
  214. *
  215. * This method should not be invoked.
  216. *
  217. * @throws BadMethodCallException
  218. */
  219. public function resetModelTransformers()
  220. {
  221. throw new BadMethodCallException('Buttons do not support data transformers.');
  222. }
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function setAttribute($name, $value)
  227. {
  228. $this->attributes[$name] = $value;
  229. return $this;
  230. }
  231. /**
  232. * {@inheritdoc}
  233. */
  234. public function setAttributes(array $attributes)
  235. {
  236. $this->attributes = $attributes;
  237. return $this;
  238. }
  239. /**
  240. * Unsupported method.
  241. *
  242. * This method should not be invoked.
  243. *
  244. * @throws BadMethodCallException
  245. */
  246. public function setDataMapper(DataMapperInterface $dataMapper = null)
  247. {
  248. throw new BadMethodCallException('Buttons do not support data mappers.');
  249. }
  250. /**
  251. * Set whether the button is disabled.
  252. *
  253. * @param bool $disabled Whether the button is disabled
  254. *
  255. * @return $this
  256. */
  257. public function setDisabled($disabled)
  258. {
  259. $this->disabled = $disabled;
  260. return $this;
  261. }
  262. /**
  263. * Unsupported method.
  264. *
  265. * This method should not be invoked.
  266. *
  267. * @param mixed $emptyData
  268. *
  269. * @throws BadMethodCallException
  270. */
  271. public function setEmptyData($emptyData)
  272. {
  273. throw new BadMethodCallException('Buttons do not support empty data.');
  274. }
  275. /**
  276. * Unsupported method.
  277. *
  278. * This method should not be invoked.
  279. *
  280. * @param bool $errorBubbling
  281. *
  282. * @throws BadMethodCallException
  283. */
  284. public function setErrorBubbling($errorBubbling)
  285. {
  286. throw new BadMethodCallException('Buttons do not support error bubbling.');
  287. }
  288. /**
  289. * Unsupported method.
  290. *
  291. * This method should not be invoked.
  292. *
  293. * @param bool $required
  294. *
  295. * @throws BadMethodCallException
  296. */
  297. public function setRequired($required)
  298. {
  299. throw new BadMethodCallException('Buttons cannot be required.');
  300. }
  301. /**
  302. * Unsupported method.
  303. *
  304. * This method should not be invoked.
  305. *
  306. * @param null $propertyPath
  307. *
  308. * @throws BadMethodCallException
  309. */
  310. public function setPropertyPath($propertyPath)
  311. {
  312. throw new BadMethodCallException('Buttons do not support property paths.');
  313. }
  314. /**
  315. * Unsupported method.
  316. *
  317. * This method should not be invoked.
  318. *
  319. * @param bool $mapped
  320. *
  321. * @throws BadMethodCallException
  322. */
  323. public function setMapped($mapped)
  324. {
  325. throw new BadMethodCallException('Buttons do not support data mapping.');
  326. }
  327. /**
  328. * Unsupported method.
  329. *
  330. * This method should not be invoked.
  331. *
  332. * @param bool $byReference
  333. *
  334. * @throws BadMethodCallException
  335. */
  336. public function setByReference($byReference)
  337. {
  338. throw new BadMethodCallException('Buttons do not support data mapping.');
  339. }
  340. /**
  341. * Unsupported method.
  342. *
  343. * This method should not be invoked.
  344. *
  345. * @param bool $virtual
  346. *
  347. * @throws BadMethodCallException
  348. *
  349. * @deprecated since version 2.3, to be removed in 3.0. Use
  350. * {@link setInheritData()} instead.
  351. */
  352. public function setVirtual($virtual)
  353. {
  354. throw new BadMethodCallException('Buttons cannot be virtual.');
  355. }
  356. /**
  357. * Unsupported method.
  358. *
  359. * This method should not be invoked.
  360. *
  361. * @param bool $compound
  362. *
  363. * @throws BadMethodCallException
  364. */
  365. public function setCompound($compound)
  366. {
  367. throw new BadMethodCallException('Buttons cannot be compound.');
  368. }
  369. /**
  370. * Sets the type of the button.
  371. *
  372. * @return $this
  373. */
  374. public function setType(ResolvedFormTypeInterface $type)
  375. {
  376. $this->type = $type;
  377. return $this;
  378. }
  379. /**
  380. * Unsupported method.
  381. *
  382. * This method should not be invoked.
  383. *
  384. * @param mixed $data
  385. *
  386. * @throws BadMethodCallException
  387. */
  388. public function setData($data)
  389. {
  390. throw new BadMethodCallException('Buttons do not support data.');
  391. }
  392. /**
  393. * Unsupported method.
  394. *
  395. * This method should not be invoked.
  396. *
  397. * @param bool $locked
  398. *
  399. * @throws BadMethodCallException
  400. */
  401. public function setDataLocked($locked)
  402. {
  403. throw new BadMethodCallException('Buttons do not support data locking.');
  404. }
  405. /**
  406. * Unsupported method.
  407. *
  408. * This method should not be invoked.
  409. *
  410. * @throws BadMethodCallException
  411. */
  412. public function setFormFactory(FormFactoryInterface $formFactory)
  413. {
  414. throw new BadMethodCallException('Buttons do not support form factories.');
  415. }
  416. /**
  417. * Unsupported method.
  418. *
  419. * @param string $action
  420. *
  421. * @throws BadMethodCallException
  422. */
  423. public function setAction($action)
  424. {
  425. throw new BadMethodCallException('Buttons do not support actions.');
  426. }
  427. /**
  428. * Unsupported method.
  429. *
  430. * @param string $method
  431. *
  432. * @throws BadMethodCallException
  433. */
  434. public function setMethod($method)
  435. {
  436. throw new BadMethodCallException('Buttons do not support methods.');
  437. }
  438. /**
  439. * Unsupported method.
  440. *
  441. * @throws BadMethodCallException
  442. */
  443. public function setRequestHandler(RequestHandlerInterface $requestHandler)
  444. {
  445. throw new BadMethodCallException('Buttons do not support request handlers.');
  446. }
  447. /**
  448. * Unsupported method.
  449. *
  450. * @param bool $initialize
  451. *
  452. * @return $this
  453. *
  454. * @throws BadMethodCallException
  455. */
  456. public function setAutoInitialize($initialize)
  457. {
  458. if (true === $initialize) {
  459. throw new BadMethodCallException('Buttons do not support automatic initialization.');
  460. }
  461. return $this;
  462. }
  463. /**
  464. * Unsupported method.
  465. *
  466. * @param bool $inheritData
  467. *
  468. * @throws BadMethodCallException
  469. */
  470. public function setInheritData($inheritData)
  471. {
  472. throw new BadMethodCallException('Buttons do not support data inheritance.');
  473. }
  474. /**
  475. * Builds and returns the button configuration.
  476. *
  477. * @return FormConfigInterface
  478. */
  479. public function getFormConfig()
  480. {
  481. // This method should be idempotent, so clone the builder
  482. $config = clone $this;
  483. $config->locked = true;
  484. return $config;
  485. }
  486. /**
  487. * Unsupported method.
  488. */
  489. public function getEventDispatcher()
  490. {
  491. }
  492. /**
  493. * {@inheritdoc}
  494. */
  495. public function getName()
  496. {
  497. return $this->name;
  498. }
  499. /**
  500. * Unsupported method.
  501. */
  502. public function getPropertyPath()
  503. {
  504. }
  505. /**
  506. * Unsupported method.
  507. *
  508. * @return bool Always returns false
  509. */
  510. public function getMapped()
  511. {
  512. return false;
  513. }
  514. /**
  515. * Unsupported method.
  516. *
  517. * @return bool Always returns false
  518. */
  519. public function getByReference()
  520. {
  521. return false;
  522. }
  523. /**
  524. * Unsupported method.
  525. *
  526. * @return bool Always returns false
  527. *
  528. * @deprecated since version 2.3, to be removed in 3.0.
  529. * Use {@link getInheritData()} instead.
  530. */
  531. public function getVirtual()
  532. {
  533. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.3 and will be removed in 3.0. Use the Symfony\Component\Form\FormConfigBuilder::getInheritData method instead.', E_USER_DEPRECATED);
  534. return false;
  535. }
  536. /**
  537. * Unsupported method.
  538. *
  539. * @return bool Always returns false
  540. */
  541. public function getCompound()
  542. {
  543. return false;
  544. }
  545. /**
  546. * Returns the form type used to construct the button.
  547. *
  548. * @return ResolvedFormTypeInterface The button's type
  549. */
  550. public function getType()
  551. {
  552. return $this->type;
  553. }
  554. /**
  555. * Unsupported method.
  556. *
  557. * @return array Always returns an empty array
  558. */
  559. public function getViewTransformers()
  560. {
  561. return array();
  562. }
  563. /**
  564. * Unsupported method.
  565. *
  566. * @return array Always returns an empty array
  567. */
  568. public function getModelTransformers()
  569. {
  570. return array();
  571. }
  572. /**
  573. * Unsupported method.
  574. */
  575. public function getDataMapper()
  576. {
  577. }
  578. /**
  579. * Unsupported method.
  580. *
  581. * @return bool Always returns false
  582. */
  583. public function getRequired()
  584. {
  585. return false;
  586. }
  587. /**
  588. * Returns whether the button is disabled.
  589. *
  590. * @return bool Whether the button is disabled
  591. */
  592. public function getDisabled()
  593. {
  594. return $this->disabled;
  595. }
  596. /**
  597. * Unsupported method.
  598. *
  599. * @return bool Always returns false
  600. */
  601. public function getErrorBubbling()
  602. {
  603. return false;
  604. }
  605. /**
  606. * Unsupported method.
  607. */
  608. public function getEmptyData()
  609. {
  610. }
  611. /**
  612. * Returns additional attributes of the button.
  613. *
  614. * @return array An array of key-value combinations
  615. */
  616. public function getAttributes()
  617. {
  618. return $this->attributes;
  619. }
  620. /**
  621. * Returns whether the attribute with the given name exists.
  622. *
  623. * @param string $name The attribute name
  624. *
  625. * @return bool Whether the attribute exists
  626. */
  627. public function hasAttribute($name)
  628. {
  629. return array_key_exists($name, $this->attributes);
  630. }
  631. /**
  632. * Returns the value of the given attribute.
  633. *
  634. * @param string $name The attribute name
  635. * @param mixed $default The value returned if the attribute does not exist
  636. *
  637. * @return mixed The attribute value
  638. */
  639. public function getAttribute($name, $default = null)
  640. {
  641. return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
  642. }
  643. /**
  644. * Unsupported method.
  645. */
  646. public function getData()
  647. {
  648. }
  649. /**
  650. * Unsupported method.
  651. */
  652. public function getDataClass()
  653. {
  654. }
  655. /**
  656. * Unsupported method.
  657. *
  658. * @return bool Always returns false
  659. */
  660. public function getDataLocked()
  661. {
  662. return false;
  663. }
  664. /**
  665. * Unsupported method.
  666. */
  667. public function getFormFactory()
  668. {
  669. }
  670. /**
  671. * Unsupported method.
  672. */
  673. public function getAction()
  674. {
  675. }
  676. /**
  677. * Unsupported method.
  678. */
  679. public function getMethod()
  680. {
  681. }
  682. /**
  683. * Unsupported method.
  684. */
  685. public function getRequestHandler()
  686. {
  687. }
  688. /**
  689. * Unsupported method.
  690. *
  691. * @return bool Always returns false
  692. */
  693. public function getAutoInitialize()
  694. {
  695. return false;
  696. }
  697. /**
  698. * Unsupported method.
  699. *
  700. * @return bool Always returns false
  701. */
  702. public function getInheritData()
  703. {
  704. return false;
  705. }
  706. /**
  707. * Returns all options passed during the construction of the button.
  708. *
  709. * @return array The passed options
  710. */
  711. public function getOptions()
  712. {
  713. return $this->options;
  714. }
  715. /**
  716. * Returns whether a specific option exists.
  717. *
  718. * @param string $name The option name,
  719. *
  720. * @return bool Whether the option exists
  721. */
  722. public function hasOption($name)
  723. {
  724. return array_key_exists($name, $this->options);
  725. }
  726. /**
  727. * Returns the value of a specific option.
  728. *
  729. * @param string $name The option name
  730. * @param mixed $default The value returned if the option does not exist
  731. *
  732. * @return mixed The option value
  733. */
  734. public function getOption($name, $default = null)
  735. {
  736. return array_key_exists($name, $this->options) ? $this->options[$name] : $default;
  737. }
  738. /**
  739. * Unsupported method.
  740. *
  741. * @return int Always returns 0
  742. */
  743. public function count()
  744. {
  745. return 0;
  746. }
  747. /**
  748. * Unsupported method.
  749. *
  750. * @return \EmptyIterator Always returns an empty iterator
  751. */
  752. public function getIterator()
  753. {
  754. return new \EmptyIterator();
  755. }
  756. }