2012-05-16 18:25:27 +01: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\Form\Exception\UnexpectedTypeException ;
use Symfony\Component\Form\Util\PropertyPath ;
use Symfony\Component\EventDispatcher\EventDispatcherInterface ;
use Symfony\Component\EventDispatcher\EventSubscriberInterface ;
/**
* A basic form configuration .
*
* @ author Bernhard Schussek < bschussek @ gmail . com >
*/
class FormConfig implements FormConfigInterface
{
/**
2012-05-20 10:18:31 +01:00
* @ var EventDispatcherInterface
2012-05-16 18:25:27 +01:00
*/
private $dispatcher ;
/**
* @ var string
*/
private $name ;
2012-05-17 15:09:13 +01:00
/**
* @ var PropertyPath
*/
private $propertyPath ;
/**
* @ var Boolean
*/
private $mapped ;
2012-05-16 18:25:27 +01:00
/**
* @ var array
*/
private $types = array ();
/**
* @ var array
*/
private $clientTransformers = array ();
/**
* @ var array
*/
private $normTransformers = array ();
/**
* @ var DataMapperInterface
*/
private $dataMapper ;
/**
2012-05-20 10:18:31 +01:00
* @ var array
2012-05-16 18:25:27 +01:00
*/
private $validators = array ();
/**
* @ var Boolean
*/
private $required ;
/**
* @ var Boolean
*/
private $disabled ;
/**
* @ var Boolean
*/
private $errorBubbling ;
/**
* @ var mixed
*/
private $emptyData ;
/**
* @ var array
*/
private $attributes = array ();
/**
* @ var mixed
*/
private $data ;
2012-05-17 15:09:13 +01:00
/**
* @ var string
*/
private $dataClass ;
2012-05-16 18:25:27 +01:00
/**
* Creates an empty form configuration .
*
2012-05-17 15:09:13 +01:00
* @ param string $name The form name .
* @ param string $dataClass The class of the form ' s data .
* @ param EventDispatcherInterface $dispatcher The event dispatcher .
*
* @ throws UnexpectedTypeException If the name is not a string .
* @ throws \InvalidArgumentException If the data class is not a valid class or if
* the name contains invalid characters .
2012-05-16 18:25:27 +01:00
*/
2012-05-17 15:09:13 +01:00
public function __construct ( $name , $dataClass , EventDispatcherInterface $dispatcher )
2012-05-16 18:25:27 +01:00
{
$name = ( string ) $name ;
self :: validateName ( $name );
2012-05-17 15:09:13 +01:00
if ( null !== $dataClass && ! class_exists ( $dataClass )) {
throw new \InvalidArgumentException ( sprintf ( 'The data class "%s" is not a valid class.' , $dataClass ));
}
2012-05-16 18:25:27 +01:00
$this -> name = $name ;
2012-05-17 15:09:13 +01:00
$this -> dataClass = $dataClass ;
2012-05-16 18:25:27 +01:00
$this -> dispatcher = $dispatcher ;
}
/**
* Adds an event listener to an event on this form .
*
* @ param string $eventName The name of the event to listen to .
* @ param callable $listener The listener to execute .
* @ param integer $priority The priority of the listener . Listeners
* with a higher priority are called before
* listeners with a lower priority .
*
* @ return self The configuration object .
*/
public function addEventListener ( $eventName , $listener , $priority = 0 )
{
$this -> dispatcher -> addListener ( $eventName , $listener , $priority );
return $this ;
}
/**
* Adds an event subscriber for events on this form .
*
* @ param EventSubscriberInterface $subscriber The subscriber to attach .
*
* @ return self The configuration object .
*/
public function addEventSubscriber ( EventSubscriberInterface $subscriber )
{
$this -> dispatcher -> addSubscriber ( $subscriber );
return $this ;
}
/**
* Adds a validator to the form .
*
* @ param FormValidatorInterface $validator The validator .
*
* @ return self The configuration object .
*
* @ deprecated Deprecated since version 2.1 , to be removed in 2.3 .
*/
public function addValidator ( FormValidatorInterface $validator )
{
$this -> validators [] = $validator ;
return $this ;
}
/**
* Appends a transformer to the client transformer chain
*
* @ param DataTransformerInterface $clientTransformer
*
* @ return self The configuration object .
*/
public function appendClientTransformer ( DataTransformerInterface $clientTransformer )
{
$this -> clientTransformers [] = $clientTransformer ;
return $this ;
}
/**
* Prepends a transformer to the client transformer chain .
*
* @ param DataTransformerInterface $clientTransformer
*
* @ return self The configuration object .
*/
public function prependClientTransformer ( DataTransformerInterface $clientTransformer )
{
array_unshift ( $this -> clientTransformers , $clientTransformer );
return $this ;
}
/**
* Clears the client transformers .
*
* @ return self The configuration object .
*/
public function resetClientTransformers ()
{
$this -> clientTransformers = array ();
return $this ;
}
/**
* Appends a transformer to the normalization transformer chain
*
* @ param DataTransformerInterface $normTransformer
*
* @ return self The configuration object .
*/
public function appendNormTransformer ( DataTransformerInterface $normTransformer )
{
$this -> normTransformers [] = $normTransformer ;
return $this ;
}
/**
* Prepends a transformer to the normalization transformer chain
*
* @ param DataTransformerInterface $normTransformer
*
* @ return self The configuration object .
*/
public function prependNormTransformer ( DataTransformerInterface $normTransformer )
{
array_unshift ( $this -> normTransformers , $normTransformer );
return $this ;
}
/**
* Clears the normalization transformers .
*
* @ return self The configuration object .
*/
public function resetNormTransformers ()
{
$this -> normTransformers = array ();
return $this ;
}
/**
* { @ inheritdoc }
*/
public function getEventDispatcher ()
{
return $this -> dispatcher ;
}
/**
* { @ inheritdoc }
*/
public function getName ()
{
return $this -> name ;
}
2012-05-17 15:09:13 +01:00
/**
* { @ inheritdoc }
*/
public function getPropertyPath ()
{
return $this -> propertyPath ;
}
/**
* { @ inheritdoc }
*/
public function getMapped ()
{
return $this -> mapped ;
}
2012-05-16 18:25:27 +01:00
/**
* { @ inheritdoc }
*/
public function getTypes ()
{
return $this -> types ;
}
/**
* { @ inheritdoc }
*/
public function getClientTransformers ()
{
return $this -> clientTransformers ;
}
/**
* { @ inheritdoc }
*/
public function getNormTransformers ()
{
return $this -> normTransformers ;
}
/**
* Returns the data mapper of the form .
*
* @ return DataMapperInterface The data mapper .
*/
public function getDataMapper ()
{
return $this -> dataMapper ;
}
/**
* { @ inheritdoc }
*/
public function getValidators ()
{
return $this -> validators ;
}
/**
* { @ inheritdoc }
*/
public function getRequired ()
{
return $this -> required ;
}
/**
* { @ inheritdoc }
*/
public function getDisabled ()
{
return $this -> disabled ;
}
/**
* { @ inheritdoc }
*/
public function getErrorBubbling ()
{
return $this -> errorBubbling ;
}
/**
* { @ inheritdoc }
*/
public function getEmptyData ()
{
return $this -> emptyData ;
}
/**
* { @ inheritdoc }
*/
public function getAttributes ()
{
return $this -> attributes ;
}
/**
* { @ inheritdoc }
*/
2012-05-20 10:18:31 +01:00
public function hasAttribute ( $name )
2012-05-16 18:25:27 +01:00
{
return isset ( $this -> attributes [ $name ]);
}
/**
* { @ inheritdoc }
*/
2012-05-20 10:18:31 +01:00
public function getAttribute ( $name )
2012-05-16 18:25:27 +01:00
{
return isset ( $this -> attributes [ $name ]) ? $this -> attributes [ $name ] : null ;
}
/**
* { @ inheritdoc }
*/
public function getData ()
{
return $this -> data ;
}
2012-05-17 15:09:13 +01:00
/**
* { @ inheritdoc }
*/
public function getDataClass ()
{
return $this -> dataClass ;
}
2012-05-16 18:25:27 +01:00
/**
* Sets the value for an attribute .
*
* @ param string $name The name of the attribute
* @ param string $value The value of the attribute
*
* @ return self The configuration object .
*/
public function setAttribute ( $name , $value )
{
$this -> attributes [ $name ] = $value ;
return $this ;
}
/**
* Sets the attributes .
*
* @ param array $attributes The attributes .
*
* @ return self The configuration object .
*/
public function setAttributes ( array $attributes )
{
$this -> attributes = $attributes ;
return $this ;
}
/**
* Sets the data mapper used by the form .
*
* @ param DataMapperInterface $dataMapper
*
* @ return self The configuration object .
*/
public function setDataMapper ( DataMapperInterface $dataMapper = null )
{
$this -> dataMapper = $dataMapper ;
return $this ;
}
/**
* Set whether the form is disabled .
*
* @ param Boolean $disabled Whether the form is disabled
*
* @ return self The configuration object .
*/
public function setDisabled ( $disabled )
{
$this -> disabled = ( Boolean ) $disabled ;
return $this ;
}
/**
* Sets the data used for the client data when no value is bound .
*
* @ param mixed $emptyData The empty data .
*
* @ return self The configuration object .
*/
public function setEmptyData ( $emptyData )
{
$this -> emptyData = $emptyData ;
return $this ;
}
/**
* Sets whether errors bubble up to the parent .
*
* @ param Boolean $errorBubbling
*
* @ return self The configuration object .
*/
public function setErrorBubbling ( $errorBubbling )
{
$this -> errorBubbling = null === $errorBubbling ? null : ( Boolean ) $errorBubbling ;
return $this ;
}
/**
* Sets whether this field is required to be filled out when bound .
*
* @ param Boolean $required
*
* @ return self The configuration object .
*/
public function setRequired ( $required )
{
$this -> required = ( Boolean ) $required ;
return $this ;
}
2012-05-17 15:09:13 +01:00
/**
* Sets the property path that the form should be mapped to .
*
* @ param string | PropertyPath $propertyPath The property path or null if the path
* should be set automatically based on
* the form ' s name .
*
* @ return self The configuration object .
*/
public function setPropertyPath ( $propertyPath )
{
if ( null !== $propertyPath && ! $propertyPath instanceof PropertyPath ) {
$propertyPath = new PropertyPath ( $propertyPath );
}
$this -> propertyPath = $propertyPath ;
return $this ;
}
/**
* Sets whether the form should be mapped to an element of its
* parent ' s data .
*
* @ param Boolean $mapped Whether the form should be mapped .
*
* @ return self The configuration object .
*/
public function setMapped ( $mapped )
{
$this -> mapped = $mapped ;
return $this ;
}
2012-05-16 18:25:27 +01:00
/**
* Set the types .
*
* @ param array $types An array FormTypeInterface
*
* @ return self The configuration object .
*/
public function setTypes ( array $types )
{
$this -> types = $types ;
return $this ;
}
/**
* Sets the initial data of the form .
*
* @ param array $data The data of the form in application format .
*
* @ return self The configuration object .
*/
public function setData ( $data )
{
$this -> data = $data ;
return $this ;
}
/**
* Validates whether the given variable is a valid form name .
*
* @ param string $name The tested form name .
*
2012-05-17 15:09:13 +01:00
* @ throws UnexpectedTypeException If the name is not a string .
2012-05-16 18:25:27 +01:00
* @ throws \InvalidArgumentException If the name contains invalid characters .
*/
static public function validateName ( $name )
{
if ( ! is_string ( $name )) {
throw new UnexpectedTypeException ( $name , 'string' );
}
if ( ! self :: isValidName ( $name )) {
throw new \InvalidArgumentException ( sprintf (
'The name "%s" contains illegal characters. Names should start with a letter, digit or underscore and only contain letters, digits, numbers, underscores ("_"), hyphens ("-") and colons (":").' ,
$name
));
}
}
/**
* Returns whether the given variable contains a valid form name .
*
* A name is accepted if it
*
* * is empty
* * starts with a letter , digit or underscore
* * contains only letters , digits , numbers , underscores ( " _ " ),
* hyphens ( " - " ) and colons ( " : " )
*
* @ param string $name The tested form name .
*
* @ return Boolean Whether the name is valid .
*/
static public function isValidName ( $name )
{
return '' === $name || preg_match ( '/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D' , $name );
}
}