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