ButtonBuilder.php 18 KB

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