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 ;
2013-04-11 15:56:20 +01:00
use Symfony\Component\Form\Exception\InvalidArgumentException ;
2013-04-15 21:11:02 +01:00
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 .
*
2013-04-15 21:11:02 +01:00
* @ throws InvalidArgumentException If the name is empty .
2012-12-31 14:29:36 +00:00
*/
2013-11-09 15:25:41 +00:00
public function __construct ( $name , array $options = array ())
2012-12-31 14:29:36 +00:00
{
if ( empty ( $name ) && 0 != $name ) {
2013-04-11 15:56:20 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function add ( $child , $type = null , array $options = array ())
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function create ( $name , $type = null , array $options = array ())
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function get ( $name )
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function remove ( $name )
{
2013-04-15 21:11:02 +01:00
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
2014-04-12 18:54:57 +01:00
* @ param int $priority
2012-12-31 14:29:36 +00:00
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function addEventListener ( $eventName , $listener , $priority = 0 )
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function addEventSubscriber ( EventSubscriberInterface $subscriber )
{
2013-04-15 21:11:02 +01:00
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
2014-04-12 18:54:57 +01:00
* @ param bool $forcePrepend
2012-12-31 14:29:36 +00:00
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function addViewTransformer ( DataTransformerInterface $viewTransformer , $forcePrepend = false )
{
2013-04-15 21:11:02 +01:00
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 .
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function resetViewTransformers ()
{
2013-04-15 21:11:02 +01:00
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
2014-04-12 18:54:57 +01:00
* @ param bool $forceAppend
2012-12-31 14:29:36 +00:00
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function addModelTransformer ( DataTransformerInterface $modelTransformer , $forceAppend = false )
{
2013-04-15 21:11:02 +01:00
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 .
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function resetModelTransformers ()
{
2013-04-15 21:11:02 +01:00
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 ;
2014-03-31 10:59:34 +01:00
return $this ;
2012-12-31 14:29:36 +00:00
}
/**
* { @ inheritdoc }
*/
public function setAttributes ( array $attributes )
{
$this -> attributes = $attributes ;
2014-03-31 10:59:34 +01:00
return $this ;
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method .
*
* This method should not be invoked .
*
* @ param DataMapperInterface $dataMapper
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setDataMapper ( DataMapperInterface $dataMapper = null )
{
2013-04-15 21:11:02 +01:00
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 ;
2014-03-31 10:59:34 +01:00
return $this ;
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method .
*
* This method should not be invoked .
*
* @ param mixed $emptyData
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setEmptyData ( $emptyData )
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setErrorBubbling ( $errorBubbling )
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setRequired ( $required )
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setPropertyPath ( $propertyPath )
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setMapped ( $mapped )
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setByReference ( $byReference )
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2015-01-02 16:09:32 +00:00
*
* @ deprecated since version 2.3 , to be removed in 3.0 . Use
* { @ link setInheritData ()} instead .
2012-12-31 14:29:36 +00:00
*/
public function setVirtual ( $virtual )
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setCompound ( $compound )
{
2013-04-15 21:11:02 +01:00
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 ;
2014-03-31 10:59:34 +01:00
return $this ;
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method .
*
* This method should not be invoked .
*
2014-09-16 10:11:47 +01:00
* @ param mixed $data
2012-12-31 14:29:36 +00:00
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setData ( $data )
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setDataLocked ( $locked )
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setFormFactory ( FormFactoryInterface $formFactory )
{
2013-04-15 21:11:02 +01:00
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
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-30 15:38:36 +00:00
*/
public function setAction ( $action )
{
2013-04-15 21:11:02 +01:00
throw new BadMethodCallException ( 'Buttons do not support actions.' );
2012-12-30 15:38:36 +00:00
}
/**
* Unsupported method .
*
* @ param string $method
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-30 15:38:36 +00:00
*/
public function setMethod ( $method )
{
2013-04-15 21:11:02 +01:00
throw new BadMethodCallException ( 'Buttons do not support methods.' );
2012-12-30 15:38:36 +00:00
}
/**
* Unsupported method .
*
2013-04-20 14:56:42 +01:00
* @ param RequestHandlerInterface $requestHandler
2012-12-30 15:38:36 +00:00
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2012-12-30 15:38:36 +00:00
*/
2013-04-20 14:56:42 +01:00
public function setRequestHandler ( RequestHandlerInterface $requestHandler )
2012-12-30 15:38:36 +00:00
{
2013-04-15 21:11:02 +01:00
throw new BadMethodCallException ( 'Buttons do not support form processors.' );
2012-12-30 15:38:36 +00:00
}
2013-04-29 16:31:30 +01:00
/**
* Unsupported method .
*
2014-07-09 11:03:33 +01:00
* @ param bool $initialize
2013-04-29 16:31:30 +01:00
*
2014-07-26 13:09:47 +01:00
* @ return ButtonBuilder
*
2013-04-29 16:31:30 +01:00
* @ throws BadMethodCallException
*/
public function setAutoInitialize ( $initialize )
{
if ( true === $initialize ) {
throw new BadMethodCallException ( 'Buttons do not support automatic initialization.' );
}
return $this ;
}
2013-01-04 15:34:38 +00:00
/**
* Unsupported method .
*
2014-07-09 11:03:33 +01:00
* @ param bool $inheritData
2013-01-04 15:34:38 +00:00
*
2013-04-15 21:11:02 +01:00
* @ throws BadMethodCallException
2013-01-04 15:34:38 +00:00
*/
public function setInheritData ( $inheritData )
{
2013-04-15 21:11:02 +01:00
throw new BadMethodCallException ( 'Buttons do not support data inheritance.' );
2013-01-04 15:34:38 +00:00
}
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 .
*/
public function getEventDispatcher ()
{
}
/**
* { @ inheritdoc }
*/
public function getName ()
{
return $this -> name ;
}
/**
* Unsupported method .
*/
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 .
2014-11-29 10:23:41 +00:00
*
2015-01-02 16:09:32 +00:00
* @ deprecated since version 2.3 , to be removed in 3.0 . Use
* { @ link getInheritData ()} instead .
2012-12-31 14:29:36 +00:00
*/
public function getVirtual ()
{
2014-12-21 11:39:54 +00:00
trigger_error ( 'The ' . __METHOD__ . ' method is deprecated since version 2.3 and will be removed in 3.0. Use FormConfigBuilder::getInheritData() instead.' , E_USER_DEPRECATED );
2014-11-29 10:23:41 +00:00
2012-12-31 14:29:36 +00:00
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 .
*/
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 .
*/
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 )
{
2013-01-03 18:09:17 +00:00
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 )
{
2013-01-03 18:09:17 +00:00
return array_key_exists ( $name , $this -> attributes ) ? $this -> attributes [ $name ] : $default ;
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method .
*/
public function getData ()
{
}
/**
* Unsupported method .
*/
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 .
*/
public function getFormFactory ()
{
}
2012-12-30 15:38:36 +00:00
/**
* Unsupported method .
*/
public function getAction ()
{
}
/**
* Unsupported method .
*/
public function getMethod ()
{
}
/**
* Unsupported method .
*/
2013-04-20 14:56:42 +01:00
public function getRequestHandler ()
2012-12-30 15:38:36 +00:00
{
}
2013-04-29 16:31:30 +01:00
/**
* Unsupported method .
*
2014-07-09 11:03:33 +01:00
* @ return bool Always returns false .
2013-04-29 16:31:30 +01:00
*/
public function getAutoInitialize ()
{
2013-05-02 08:53:57 +01:00
return false ;
2013-04-29 16:31:30 +01:00
}
2013-01-04 15:34:38 +00:00
/**
* Unsupported method .
*
2014-07-09 11:03:33 +01:00
* @ return bool Always returns false .
2013-01-04 15:34:38 +00:00
*/
public function getInheritData ()
{
2013-05-02 08:53:57 +01:00
return false ;
2013-01-04 15:34:38 +00:00
}
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 )
{
2013-01-03 18:09:17 +00:00
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 )
{
2013-01-03 18:09:17 +00:00
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 ();
}
}