4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form
;
14 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
15 use Symfony\Component\Form\Exception\InvalidArgumentException
;
16 use Symfony\Component\Form\Exception\BadMethodCallException
;
19 * A builder for {@link Button} instances.
21 * @author Bernhard Schussek <bschussek@gmail.com>
23 class ButtonBuilder
implements \IteratorAggregate
, FormBuilderInterface
28 protected $locked = false;
36 * @var ResolvedFormTypeInterface
48 private $attributes = array();
56 * Creates a new button builder.
58 * @param string $name The name of the button.
59 * @param array $options The button's options.
61 * @throws InvalidArgumentException If the name is empty.
63 public function __construct($name, array $options)
65 if (empty($name) && 0 != $name) {
66 throw new InvalidArgumentException('Buttons cannot have empty names.');
69 $this->name
= (string) $name;
70 $this->options
= $options;
76 * This method should not be invoked.
78 * @param string|integer|FormBuilderInterface $child
79 * @param string|FormTypeInterface $type
80 * @param array $options
82 * @throws BadMethodCallException
84 public function add($child, $type = null, array $options = array())
86 throw new BadMethodCallException('Buttons cannot have children.');
92 * This method should not be invoked.
95 * @param string|FormTypeInterface $type
96 * @param array $options
98 * @throws BadMethodCallException
100 public function create($name, $type = null, array $options = array())
102 throw new BadMethodCallException('Buttons cannot have children.');
106 * Unsupported method.
108 * This method should not be invoked.
110 * @param string $name
112 * @throws BadMethodCallException
114 public function get($name)
116 throw new BadMethodCallException('Buttons cannot have children.');
120 * Unsupported method.
122 * This method should not be invoked.
124 * @param string $name
126 * @throws BadMethodCallException
128 public function remove($name)
130 throw new BadMethodCallException('Buttons cannot have children.');
134 * Unsupported method.
136 * @param string $name
138 * @return Boolean Always returns false.
140 public function has($name)
146 * Returns the children.
148 * @return array Always returns an empty array.
150 public function all()
156 * Creates the button.
158 * @return Button The button
160 public function getForm()
162 return new Button($this->getFormConfig());
166 * Unsupported method.
168 * This method should not be invoked.
170 * @param string $eventName
171 * @param callable $listener
172 * @param integer $priority
174 * @throws BadMethodCallException
176 public function addEventListener($eventName, $listener, $priority = 0)
178 throw new BadMethodCallException('Buttons do not support event listeners.');
182 * Unsupported method.
184 * This method should not be invoked.
186 * @param EventSubscriberInterface $subscriber
188 * @throws BadMethodCallException
190 public function addEventSubscriber(EventSubscriberInterface
$subscriber)
192 throw new BadMethodCallException('Buttons do not support event subscribers.');
196 * Unsupported method.
198 * This method should not be invoked.
200 * @param DataTransformerInterface $viewTransformer
201 * @param Boolean $forcePrepend
203 * @throws BadMethodCallException
205 public function addViewTransformer(DataTransformerInterface
$viewTransformer, $forcePrepend = false)
207 throw new BadMethodCallException('Buttons do not support data transformers.');
211 * Unsupported method.
213 * This method should not be invoked.
215 * @throws BadMethodCallException
217 public function resetViewTransformers()
219 throw new BadMethodCallException('Buttons do not support data transformers.');
223 * Unsupported method.
225 * This method should not be invoked.
227 * @param DataTransformerInterface $modelTransformer
228 * @param Boolean $forceAppend
230 * @throws BadMethodCallException
232 public function addModelTransformer(DataTransformerInterface
$modelTransformer, $forceAppend = false)
234 throw new BadMethodCallException('Buttons do not support data transformers.');
238 * Unsupported method.
240 * This method should not be invoked.
242 * @throws BadMethodCallException
244 public function resetModelTransformers()
246 throw new BadMethodCallException('Buttons do not support data transformers.');
252 public function setAttribute($name, $value)
254 $this->attributes
[$name] = $value;
260 public function setAttributes(array $attributes)
262 $this->attributes
= $attributes;
266 * Unsupported method.
268 * This method should not be invoked.
270 * @param DataMapperInterface $dataMapper
272 * @throws BadMethodCallException
274 public function setDataMapper(DataMapperInterface
$dataMapper = null)
276 throw new BadMethodCallException('Buttons do not support data mappers.');
280 * Set whether the button is disabled.
282 * @param Boolean $disabled Whether the button is disabled
284 * @return ButtonBuilder The button builder.
286 public function setDisabled($disabled)
288 $this->disabled
= $disabled;
292 * Unsupported method.
294 * This method should not be invoked.
296 * @param mixed $emptyData
298 * @throws BadMethodCallException
300 public function setEmptyData($emptyData)
302 throw new BadMethodCallException('Buttons do not support empty data.');
306 * Unsupported method.
308 * This method should not be invoked.
310 * @param Boolean $errorBubbling
312 * @throws BadMethodCallException
314 public function setErrorBubbling($errorBubbling)
316 throw new BadMethodCallException('Buttons do not support error bubbling.');
320 * Unsupported method.
322 * This method should not be invoked.
324 * @param Boolean $required
326 * @throws BadMethodCallException
328 public function setRequired($required)
330 throw new BadMethodCallException('Buttons cannot be required.');
334 * Unsupported method.
336 * This method should not be invoked.
338 * @param null $propertyPath
340 * @throws BadMethodCallException
342 public function setPropertyPath($propertyPath)
344 throw new BadMethodCallException('Buttons do not support property paths.');
348 * Unsupported method.
350 * This method should not be invoked.
352 * @param Boolean $mapped
354 * @throws BadMethodCallException
356 public function setMapped($mapped)
358 throw new BadMethodCallException('Buttons do not support data mapping.');
362 * Unsupported method.
364 * This method should not be invoked.
366 * @param Boolean $byReference
368 * @throws BadMethodCallException
370 public function setByReference($byReference)
372 throw new BadMethodCallException('Buttons do not support data mapping.');
376 * Unsupported method.
378 * This method should not be invoked.
380 * @param Boolean $virtual
382 * @throws BadMethodCallException
384 public function setVirtual($virtual)
386 throw new BadMethodCallException('Buttons cannot be virtual.');
390 * Unsupported method.
392 * This method should not be invoked.
394 * @param Boolean $compound
396 * @throws BadMethodCallException
398 public function setCompound($compound)
400 throw new BadMethodCallException('Buttons cannot be compound.');
404 * Sets the type of the button.
406 * @param ResolvedFormTypeInterface $type The type of the button.
408 * @return ButtonBuilder The button builder.
410 public function setType(ResolvedFormTypeInterface
$type)
416 * Unsupported method.
418 * This method should not be invoked.
422 * @throws BadMethodCallException
424 public function setData($data)
426 throw new BadMethodCallException('Buttons do not support data.');
430 * Unsupported method.
432 * This method should not be invoked.
434 * @param Boolean $locked
436 * @throws BadMethodCallException
438 public function setDataLocked($locked)
440 throw new BadMethodCallException('Buttons do not support data locking.');
444 * Unsupported method.
446 * This method should not be invoked.
448 * @param FormFactoryInterface $formFactory
452 * @throws BadMethodCallException
454 public function setFormFactory(FormFactoryInterface
$formFactory)
456 throw new BadMethodCallException('Buttons do not support form factories.');
460 * Unsupported method.
462 * @param string $action
464 * @throws BadMethodCallException
466 public function setAction($action)
468 throw new BadMethodCallException('Buttons do not support actions.');
472 * Unsupported method.
474 * @param string $method
476 * @throws BadMethodCallException
478 public function setMethod($method)
480 throw new BadMethodCallException('Buttons do not support methods.');
484 * Unsupported method.
486 * @param RequestHandlerInterface $requestHandler
488 * @throws BadMethodCallException
490 public function setRequestHandler(RequestHandlerInterface
$requestHandler)
492 throw new BadMethodCallException('Buttons do not support form processors.');
496 * Unsupported method.
498 * @param Boolean $initialize
500 * @throws BadMethodCallException
502 public function setAutoInitialize($initialize)
504 if (true === $initialize) {
505 throw new BadMethodCallException('Buttons do not support automatic initialization.');
512 * Unsupported method.
514 * @param Boolean $inheritData
516 * @throws BadMethodCallException
518 public function setInheritData($inheritData)
520 throw new BadMethodCallException('Buttons do not support data inheritance.');
524 * Builds and returns the button configuration.
526 * @return FormConfigInterface
528 public function getFormConfig()
530 // This method should be idempotent, so clone the builder
531 $config = clone $this;
532 $config->locked
= true;
538 * Unsupported method.
540 * @return null Always returns null.
542 public function getEventDispatcher()
550 public function getName()
556 * Unsupported method.
558 * @return null Always returns null.
560 public function getPropertyPath()
566 * Unsupported method.
568 * @return Boolean Always returns false.
570 public function getMapped()
576 * Unsupported method.
578 * @return Boolean Always returns false.
580 public function getByReference()
586 * Unsupported method.
588 * @return Boolean Always returns false.
590 public function getVirtual()
596 * Unsupported method.
598 * @return Boolean Always returns false.
600 public function getCompound()
606 * Returns the form type used to construct the button.
608 * @return ResolvedFormTypeInterface The button's type.
610 public function getType()
616 * Unsupported method.
618 * @return array Always returns an empty array.
620 public function getViewTransformers()
626 * Unsupported method.
628 * @return array Always returns an empty array.
630 public function getModelTransformers()
636 * Unsupported method.
638 * @return null Always returns null.
640 public function getDataMapper()
646 * Unsupported method.
648 * @return Boolean Always returns false.
650 public function getRequired()
656 * Returns whether the button is disabled.
658 * @return Boolean Whether the button is disabled.
660 public function getDisabled()
662 return $this->disabled
;
666 * Unsupported method.
668 * @return Boolean Always returns false.
670 public function getErrorBubbling()
676 * Unsupported method.
678 * @return null Always returns null.
680 public function getEmptyData()
686 * Returns additional attributes of the button.
688 * @return array An array of key-value combinations.
690 public function getAttributes()
692 return $this->attributes
;
696 * Returns whether the attribute with the given name exists.
698 * @param string $name The attribute name.
700 * @return Boolean Whether the attribute exists.
702 public function hasAttribute($name)
704 return array_key_exists($name, $this->attributes
);
708 * Returns the value of the given attribute.
710 * @param string $name The attribute name.
711 * @param mixed $default The value returned if the attribute does not exist.
713 * @return mixed The attribute value.
715 public function getAttribute($name, $default = null)
717 return array_key_exists($name, $this->attributes
) ? $this->attributes
[$name] : $default;
721 * Unsupported method.
723 * @return null Always returns null.
725 public function getData()
731 * Unsupported method.
733 * @return null Always returns null.
735 public function getDataClass()
741 * Unsupported method.
743 * @return Boolean Always returns false.
745 public function getDataLocked()
751 * Unsupported method.
753 * @return null Always returns null.
755 public function getFormFactory()
761 * Unsupported method.
763 * @return null Always returns null.
765 public function getAction()
771 * Unsupported method.
773 * @return null Always returns null.
775 public function getMethod()
781 * Unsupported method.
783 * @return null Always returns null.
785 public function getRequestHandler()
791 * Unsupported method.
793 * @return Boolean Always returns false.
795 public function getAutoInitialize()
801 * Unsupported method.
803 * @return Boolean Always returns false.
805 public function getInheritData()
811 * Returns all options passed during the construction of the button.
813 * @return array The passed options.
815 public function getOptions()
817 return $this->options
;
821 * Returns whether a specific option exists.
823 * @param string $name The option name,
825 * @return Boolean Whether the option exists.
827 public function hasOption($name)
829 return array_key_exists($name, $this->options
);
833 * Returns the value of a specific option.
835 * @param string $name The option name.
836 * @param mixed $default The value returned if the option does not exist.
838 * @return mixed The option value.
840 public function getOption($name, $default = null)
842 return array_key_exists($name, $this->options
) ? $this->options
[$name] : $default;
846 * Unsupported method.
848 * @return integer Always returns 0.
850 public function count()
856 * Unsupported method.
858 * @return \EmptyIterator Always returns an empty iterator.
860 public function getIterator()
862 return new \
EmptyIterator();