This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Form/ButtonBuilder.php

863 lines
18 KiB
PHP
Raw Normal View History

2012-12-31 14:29:36 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\Exception\BadMethodCallException;
2012-12-31 14:29:36 +00:00
/**
* A builder for {@link Button} instances.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
{
/**
2014-04-16 11:30:19 +01:00
* @var bool
2012-12-31 14:29:36 +00:00
*/
protected $locked = false;
/**
2014-04-16 11:30:19 +01:00
* @var bool
2012-12-31 14:29:36 +00:00
*/
private $disabled;
/**
* @var ResolvedFormTypeInterface
*/
private $type;
/**
* @var string
*/
private $name;
/**
* @var array
*/
private $attributes = array();
/**
* @var array
*/
private $options;
/**
* Creates a new button builder.
*
* @param string $name The name of the button.
* @param array $options The button's options.
*
* @throws InvalidArgumentException If the name is empty.
2012-12-31 14:29:36 +00:00
*/
public function __construct($name, array $options = array())
2012-12-31 14:29:36 +00:00
{
if (empty($name) && 0 != $name) {
throw new InvalidArgumentException('Buttons cannot have empty names.');
2012-12-31 14:29:36 +00:00
}
$this->name = (string) $name;
$this->options = $options;
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
2014-07-09 11:03:33 +01:00
* @param string|int|FormBuilderInterface $child
* @param string|FormTypeInterface $type
* @param array $options
2012-12-31 14:29:36 +00:00
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function add($child, $type = null, array $options = array())
{
throw new BadMethodCallException('Buttons cannot have children.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param string $name
* @param string|FormTypeInterface $type
* @param array $options
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function create($name, $type = null, array $options = array())
{
throw new BadMethodCallException('Buttons cannot have children.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param string $name
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function get($name)
{
throw new BadMethodCallException('Buttons cannot have children.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param string $name
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function remove($name)
{
throw new BadMethodCallException('Buttons cannot have children.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* @param string $name
*
2014-07-09 11:03:33 +01:00
* @return bool Always returns false.
2012-12-31 14:29:36 +00:00
*/
public function has($name)
{
return false;
}
/**
* Returns the children.
*
* @return array Always returns an empty array.
*/
public function all()
{
return array();
}
/**
* Creates the button.
*
* @return Button The button
*/
public function getForm()
{
return new Button($this->getFormConfig());
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param string $eventName
* @param callable $listener
* @param int $priority
2012-12-31 14:29:36 +00:00
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function addEventListener($eventName, $listener, $priority = 0)
{
throw new BadMethodCallException('Buttons do not support event listeners.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param EventSubscriberInterface $subscriber
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function addEventSubscriber(EventSubscriberInterface $subscriber)
{
throw new BadMethodCallException('Buttons do not support event subscribers.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param DataTransformerInterface $viewTransformer
* @param bool $forcePrepend
2012-12-31 14:29:36 +00:00
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function addViewTransformer(DataTransformerInterface $viewTransformer, $forcePrepend = false)
{
throw new BadMethodCallException('Buttons do not support data transformers.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function resetViewTransformers()
{
throw new BadMethodCallException('Buttons do not support data transformers.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param DataTransformerInterface $modelTransformer
* @param bool $forceAppend
2012-12-31 14:29:36 +00:00
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function addModelTransformer(DataTransformerInterface $modelTransformer, $forceAppend = false)
{
throw new BadMethodCallException('Buttons do not support data transformers.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function resetModelTransformers()
{
throw new BadMethodCallException('Buttons do not support data transformers.');
2012-12-31 14:29:36 +00:00
}
/**
* {@inheritdoc}
*/
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
return $this;
2012-12-31 14:29:36 +00:00
}
/**
* {@inheritdoc}
*/
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
return $this;
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param DataMapperInterface $dataMapper
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setDataMapper(DataMapperInterface $dataMapper = null)
{
throw new BadMethodCallException('Buttons do not support data mappers.');
2012-12-31 14:29:36 +00:00
}
/**
* Set whether the button is disabled.
*
2014-07-09 11:03:33 +01:00
* @param bool $disabled Whether the button is disabled
2012-12-31 14:29:36 +00:00
*
* @return ButtonBuilder The button builder.
*/
public function setDisabled($disabled)
{
$this->disabled = $disabled;
return $this;
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param mixed $emptyData
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setEmptyData($emptyData)
{
throw new BadMethodCallException('Buttons do not support empty data.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
2014-07-09 11:03:33 +01:00
* @param bool $errorBubbling
2012-12-31 14:29:36 +00:00
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setErrorBubbling($errorBubbling)
{
throw new BadMethodCallException('Buttons do not support error bubbling.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
2014-07-09 11:03:33 +01:00
* @param bool $required
2012-12-31 14:29:36 +00:00
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setRequired($required)
{
throw new BadMethodCallException('Buttons cannot be required.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param null $propertyPath
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setPropertyPath($propertyPath)
{
throw new BadMethodCallException('Buttons do not support property paths.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
2014-07-09 11:03:33 +01:00
* @param bool $mapped
2012-12-31 14:29:36 +00:00
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setMapped($mapped)
{
throw new BadMethodCallException('Buttons do not support data mapping.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
2014-07-09 11:03:33 +01:00
* @param bool $byReference
2012-12-31 14:29:36 +00:00
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setByReference($byReference)
{
throw new BadMethodCallException('Buttons do not support data mapping.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
2014-07-09 11:03:33 +01:00
* @param bool $virtual
2012-12-31 14:29:36 +00:00
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setVirtual($virtual)
{
throw new BadMethodCallException('Buttons cannot be virtual.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
2014-07-09 11:03:33 +01:00
* @param bool $compound
2012-12-31 14:29:36 +00:00
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setCompound($compound)
{
throw new BadMethodCallException('Buttons cannot be compound.');
2012-12-31 14:29:36 +00:00
}
/**
* Sets the type of the button.
*
* @param ResolvedFormTypeInterface $type The type of the button.
*
* @return ButtonBuilder The button builder.
*/
public function setType(ResolvedFormTypeInterface $type)
{
$this->type = $type;
return $this;
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param array $data
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setData($data)
{
throw new BadMethodCallException('Buttons do not support data.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
2014-07-09 11:03:33 +01:00
* @param bool $locked
2012-12-31 14:29:36 +00:00
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setDataLocked($locked)
{
throw new BadMethodCallException('Buttons do not support data locking.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param FormFactoryInterface $formFactory
2013-07-01 13:24:43 +01:00
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setFormFactory(FormFactoryInterface $formFactory)
{
throw new BadMethodCallException('Buttons do not support form factories.');
2012-12-31 14:29:36 +00:00
}
2012-12-30 15:38:36 +00:00
/**
* Unsupported method.
*
* @param string $action
*
* @throws BadMethodCallException
2012-12-30 15:38:36 +00:00
*/
public function setAction($action)
{
throw new BadMethodCallException('Buttons do not support actions.');
2012-12-30 15:38:36 +00:00
}
/**
* Unsupported method.
*
* @param string $method
*
* @throws BadMethodCallException
2012-12-30 15:38:36 +00:00
*/
public function setMethod($method)
{
throw new BadMethodCallException('Buttons do not support methods.');
2012-12-30 15:38:36 +00:00
}
/**
* Unsupported method.
*
* @param RequestHandlerInterface $requestHandler
2012-12-30 15:38:36 +00:00
*
* @throws BadMethodCallException
2012-12-30 15:38:36 +00:00
*/
public function setRequestHandler(RequestHandlerInterface $requestHandler)
2012-12-30 15:38:36 +00:00
{
throw new BadMethodCallException('Buttons do not support form processors.');
2012-12-30 15:38:36 +00:00
}
/**
* Unsupported method.
*
2014-07-09 11:03:33 +01:00
* @param bool $initialize
*
2014-07-26 13:09:47 +01:00
* @return ButtonBuilder
*
* @throws BadMethodCallException
*/
public function setAutoInitialize($initialize)
{
if (true === $initialize) {
throw new BadMethodCallException('Buttons do not support automatic initialization.');
}
return $this;
}
/**
* Unsupported method.
*
2014-07-09 11:03:33 +01:00
* @param bool $inheritData
*
* @throws BadMethodCallException
*/
public function setInheritData($inheritData)
{
throw new BadMethodCallException('Buttons do not support data inheritance.');
}
2012-12-31 14:29:36 +00:00
/**
* Builds and returns the button configuration.
*
* @return FormConfigInterface
*/
public function getFormConfig()
{
// This method should be idempotent, so clone the builder
$config = clone $this;
$config->locked = true;
return $config;
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getEventDispatcher()
{
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getPropertyPath()
{
}
/**
* Unsupported method.
*
2014-07-09 11:03:33 +01:00
* @return bool Always returns false.
2012-12-31 14:29:36 +00:00
*/
public function getMapped()
{
return false;
}
/**
* Unsupported method.
*
2014-07-09 11:03:33 +01:00
* @return bool Always returns false.
2012-12-31 14:29:36 +00:00
*/
public function getByReference()
{
return false;
}
/**
* Unsupported method.
*
2014-07-09 11:03:33 +01:00
* @return bool Always returns false.
2012-12-31 14:29:36 +00:00
*/
public function getVirtual()
{
return false;
}
/**
* Unsupported method.
*
2014-07-09 11:03:33 +01:00
* @return bool Always returns false.
2012-12-31 14:29:36 +00:00
*/
public function getCompound()
{
return false;
}
/**
* Returns the form type used to construct the button.
*
* @return ResolvedFormTypeInterface The button's type.
*/
public function getType()
{
return $this->type;
}
/**
* Unsupported method.
*
* @return array Always returns an empty array.
*/
public function getViewTransformers()
{
return array();
}
/**
* Unsupported method.
*
* @return array Always returns an empty array.
*/
public function getModelTransformers()
{
return array();
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getDataMapper()
{
}
/**
* Unsupported method.
*
2014-07-09 11:03:33 +01:00
* @return bool Always returns false.
2012-12-31 14:29:36 +00:00
*/
public function getRequired()
{
return false;
}
/**
* Returns whether the button is disabled.
*
2014-07-09 11:03:33 +01:00
* @return bool Whether the button is disabled.
2012-12-31 14:29:36 +00:00
*/
public function getDisabled()
{
return $this->disabled;
}
/**
* Unsupported method.
*
2014-07-09 11:03:33 +01:00
* @return bool Always returns false.
2012-12-31 14:29:36 +00:00
*/
public function getErrorBubbling()
{
return false;
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getEmptyData()
{
}
/**
* Returns additional attributes of the button.
*
* @return array An array of key-value combinations.
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* Returns whether the attribute with the given name exists.
*
* @param string $name The attribute name.
*
2014-07-09 11:03:33 +01:00
* @return bool Whether the attribute exists.
2012-12-31 14:29:36 +00:00
*/
public function hasAttribute($name)
{
return array_key_exists($name, $this->attributes);
2012-12-31 14:29:36 +00:00
}
/**
* Returns the value of the given attribute.
*
* @param string $name The attribute name.
* @param mixed $default The value returned if the attribute does not exist.
*
* @return mixed The attribute value.
*/
public function getAttribute($name, $default = null)
{
return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getData()
{
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getDataClass()
{
}
/**
* Unsupported method.
*
2014-07-09 11:03:33 +01:00
* @return bool Always returns false.
2012-12-31 14:29:36 +00:00
*/
public function getDataLocked()
{
return false;
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getFormFactory()
{
}
2012-12-30 15:38:36 +00:00
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getAction()
{
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getMethod()
{
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getRequestHandler()
2012-12-30 15:38:36 +00:00
{
}
/**
* Unsupported method.
*
2014-07-09 11:03:33 +01:00
* @return bool Always returns false.
*/
public function getAutoInitialize()
{
2013-05-02 08:53:57 +01:00
return false;
}
/**
* Unsupported method.
*
2014-07-09 11:03:33 +01:00
* @return bool Always returns false.
*/
public function getInheritData()
{
2013-05-02 08:53:57 +01:00
return false;
}
2012-12-31 14:29:36 +00:00
/**
* Returns all options passed during the construction of the button.
*
* @return array The passed options.
*/
public function getOptions()
{
return $this->options;
}
/**
* Returns whether a specific option exists.
*
* @param string $name The option name,
*
2014-07-09 11:03:33 +01:00
* @return bool Whether the option exists.
2012-12-31 14:29:36 +00:00
*/
public function hasOption($name)
{
return array_key_exists($name, $this->options);
2012-12-31 14:29:36 +00:00
}
/**
* Returns the value of a specific option.
*
* @param string $name The option name.
* @param mixed $default The value returned if the option does not exist.
*
* @return mixed The option value.
*/
public function getOption($name, $default = null)
{
return array_key_exists($name, $this->options) ? $this->options[$name] : $default;
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
2014-07-09 11:03:33 +01:00
* @return int Always returns 0.
2012-12-31 14:29:36 +00:00
*/
public function count()
{
return 0;
}
/**
* Unsupported method.
*
* @return \EmptyIterator Always returns an empty iterator.
*/
public function getIterator()
{
return new \EmptyIterator();
}
}