ButtonBuilder.php 16 KB

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