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 >
*/
2012-05-23 18:34:41 +01:00
class FormConfig implements FormConfigEditorInterface
2012-05-16 18:25:27 +01:00
{
/**
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
*/
2012-05-23 13:08:46 +01:00
private $mapped = true ;
2012-05-17 15:09:13 +01:00
2012-05-23 13:51:26 +01:00
/**
* @ var Boolean
*/
private $byReference = true ;
2012-05-21 10:18:32 +01:00
/**
* @ var Boolean
*/
2012-05-23 13:08:46 +01:00
private $virtual = false ;
2012-05-21 10:18:32 +01:00
2012-07-03 18:09:41 +01:00
/**
* @ var Boolean
*/
2012-07-06 14:33:06 +01:00
private $compound = false ;
2012-07-03 18:09:41 +01:00
2012-05-16 18:25:27 +01:00
/**
* @ var array
*/
private $types = array ();
/**
* @ var array
*/
2012-05-23 19:21:34 +01:00
private $viewTransformers = array ();
2012-05-16 18:25:27 +01:00
/**
* @ var array
*/
2012-05-23 19:21:34 +01:00
private $modelTransformers = array ();
2012-05-16 18:25:27 +01:00
/**
* @ 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
*/
2012-05-23 13:08:46 +01:00
private $required = true ;
2012-05-16 18:25:27 +01:00
/**
* @ var Boolean
*/
2012-05-23 13:08:46 +01:00
private $disabled = false ;
2012-05-16 18:25:27 +01:00
/**
* @ var Boolean
*/
2012-05-23 13:08:46 +01:00
private $errorBubbling = false ;
2012-05-16 18:25:27 +01:00
/**
* @ 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-23 17:47:14 +01:00
/**
* @ var array
*/
private $options ;
2012-05-16 18:25:27 +01:00
/**
* Creates an empty form configuration .
*
2012-05-27 20:41:59 +01:00
* @ param string $name The form name
* @ param string $dataClass The class of the form ' s data
* @ param EventDispatcherInterface $dispatcher The event dispatcher
* @ param array $options The form options
2012-05-17 15:09:13 +01:00
*
* @ 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-23 17:47:14 +01:00
public function __construct ( $name , $dataClass , EventDispatcherInterface $dispatcher , array $options = array ())
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 ;
2012-05-23 17:47:14 +01:00
$this -> options = $options ;
2012-05-16 18:25:27 +01:00
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
public function addEventListener ( $eventName , $listener , $priority = 0 )
{
$this -> dispatcher -> addListener ( $eventName , $listener , $priority );
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
public function addEventSubscriber ( EventSubscriberInterface $subscriber )
{
$this -> dispatcher -> addSubscriber ( $subscriber );
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
public function addValidator ( FormValidatorInterface $validator )
{
$this -> validators [] = $validator ;
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
2012-06-09 13:22:15 +01:00
public function addViewTransformer ( DataTransformerInterface $viewTransformer , $forcePrepend = false )
2012-05-16 18:25:27 +01:00
{
2012-06-09 13:22:15 +01:00
if ( $forcePrepend ) {
array_unshift ( $this -> viewTransformers , $viewTransformer );
} else {
$this -> viewTransformers [] = $viewTransformer ;
}
2012-05-16 18:25:27 +01:00
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
2012-05-23 19:21:34 +01:00
public function resetViewTransformers ()
2012-05-16 18:25:27 +01:00
{
2012-05-23 19:21:34 +01:00
$this -> viewTransformers = array ();
2012-05-16 18:25:27 +01:00
return $this ;
}
/**
2012-05-23 19:21:34 +01:00
* Alias of { @ link addViewTransformer ()} .
*
* @ param DataTransformerInterface $viewTransformer
*
* @ return self The configuration object .
*
* @ deprecated Deprecated since version 2.1 , to be removed in 2.3 . Use
* { @ link addViewTransformer ()} instead .
2012-05-16 18:25:27 +01:00
*/
2012-05-23 19:21:34 +01:00
public function appendClientTransformer ( DataTransformerInterface $viewTransformer )
{
return $this -> addViewTransformer ( $viewTransformer );
}
/**
* Prepends a transformer to the client transformer chain .
*
* @ param DataTransformerInterface $viewTransformer
*
* @ return self The configuration object .
*
* @ deprecated Deprecated since version 2.1 , to be removed in 2.3 .
*/
public function prependClientTransformer ( DataTransformerInterface $viewTransformer )
2012-05-16 18:25:27 +01:00
{
2012-06-09 13:22:15 +01:00
return $this -> addViewTransformer ( $viewTransformer , true );
2012-05-16 18:25:27 +01:00
}
2012-05-23 19:21:34 +01:00
/**
* Alias of { @ link resetViewTransformers ()} .
*
* @ return self The configuration object .
*
* @ deprecated Deprecated since version 2.1 , to be removed in 2.3 . Use
* { @ link resetViewTransformers ()} instead .
*/
public function resetClientTransformers ()
{
return $this -> resetViewTransformers ();
}
2012-05-16 18:25:27 +01:00
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
2012-06-09 13:22:15 +01:00
public function addModelTransformer ( DataTransformerInterface $modelTransformer , $forceAppend = false )
2012-05-16 18:25:27 +01:00
{
2012-06-09 13:22:15 +01:00
if ( $forceAppend ) {
$this -> modelTransformers [] = $modelTransformer ;
} else {
array_unshift ( $this -> modelTransformers , $modelTransformer );
}
2012-05-16 18:25:27 +01:00
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
2012-05-23 19:21:34 +01:00
public function resetModelTransformers ()
2012-05-16 18:25:27 +01:00
{
2012-05-23 19:21:34 +01:00
$this -> modelTransformers = array ();
2012-05-16 18:25:27 +01:00
return $this ;
}
/**
2012-05-23 19:21:34 +01:00
* Appends a transformer to the normalization transformer chain
*
* @ param DataTransformerInterface $modelTransformer
*
* @ return self The configuration object .
*
* @ deprecated Deprecated since version 2.1 , to be removed in 2.3 .
2012-05-16 18:25:27 +01:00
*/
2012-05-23 19:21:34 +01:00
public function appendNormTransformer ( DataTransformerInterface $modelTransformer )
2012-05-16 18:25:27 +01:00
{
2012-06-09 13:22:15 +01:00
return $this -> addModelTransformer ( $modelTransformer , true );
2012-05-16 18:25:27 +01:00
}
2012-05-23 19:21:34 +01:00
/**
* Alias of { @ link addModelTransformer ()} .
*
* @ param DataTransformerInterface $modelTransformer
*
* @ return self The configuration object .
*
* @ deprecated Deprecated since version 2.1 , to be removed in 2.3 . Use
* { @ link addModelTransformer ()} instead .
*/
public function prependNormTransformer ( DataTransformerInterface $modelTransformer )
{
return $this -> addModelTransformer ( $modelTransformer );
}
/**
* Alias of { @ link resetModelTransformers ()} .
*
* @ return self The configuration object .
*
* @ deprecated Deprecated since version 2.1 , to be removed in 2.3 . Use
* { @ link resetModelTransformers ()} instead .
*/
public function resetNormTransformers ()
{
return $this -> resetModelTransformers ();
}
2012-05-16 18:25:27 +01:00
/**
* { @ 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-23 13:51:26 +01:00
/**
* { @ inheritdoc }
*/
public function getByReference ()
{
return $this -> byReference ;
}
2012-05-21 10:18:32 +01:00
/**
* { @ inheritdoc }
*/
public function getVirtual ()
{
return $this -> virtual ;
}
2012-07-03 18:09:41 +01:00
/**
* { @ inheritdoc }
*/
public function getCompound ()
{
return $this -> compound ;
}
2012-05-16 18:25:27 +01:00
/**
* { @ inheritdoc }
*/
public function getTypes ()
{
return $this -> types ;
}
/**
* { @ inheritdoc }
*/
2012-05-23 19:21:34 +01:00
public function getViewTransformers ()
{
return $this -> viewTransformers ;
}
/**
* Alias of { @ link getViewTransformers ()} .
*
* @ return array The view transformers .
*
* @ deprecated Deprecated since version 2.1 , to be removed in 2.3 . Use
* { @ link getViewTransformers ()} instead .
*/
2012-05-16 18:25:27 +01:00
public function getClientTransformers ()
{
2012-05-23 19:21:34 +01:00
return $this -> getViewTransformers ();
2012-05-16 18:25:27 +01:00
}
/**
* { @ inheritdoc }
*/
2012-05-23 19:21:34 +01:00
public function getModelTransformers ()
{
return $this -> modelTransformers ;
}
/**
* Alias of { @ link getModelTransformers ()} .
*
* @ return array The model transformers .
*
* @ deprecated Deprecated since version 2.1 , to be removed in 2.3 . Use
* { @ link getModelTransformers ()} instead .
*/
2012-05-16 18:25:27 +01:00
public function getNormTransformers ()
{
2012-05-23 19:21:34 +01:00
return $this -> getModelTransformers ();
2012-05-16 18:25:27 +01:00
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
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-23 17:47:14 +01:00
public function getAttribute ( $name , $default = null )
2012-05-16 18:25:27 +01:00
{
2012-05-23 17:47:14 +01:00
return isset ( $this -> attributes [ $name ]) ? $this -> attributes [ $name ] : $default ;
2012-05-16 18:25:27 +01:00
}
/**
* { @ inheritdoc }
*/
public function getData ()
{
return $this -> data ;
}
2012-05-17 15:09:13 +01:00
/**
* { @ inheritdoc }
*/
public function getDataClass ()
{
return $this -> dataClass ;
}
2012-05-23 17:47:14 +01:00
/**
* { @ inheritdoc }
*/
public function getOptions ()
{
return $this -> options ;
}
/**
* { @ inheritdoc }
*/
public function hasOption ( $name )
{
return isset ( $this -> options [ $name ]);
}
/**
* { @ inheritdoc }
*/
public function getOption ( $name , $default = null )
{
return isset ( $this -> options [ $name ]) ? $this -> options [ $name ] : $default ;
}
2012-05-16 18:25:27 +01:00
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
public function setAttribute ( $name , $value )
{
$this -> attributes [ $name ] = $value ;
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
public function setAttributes ( array $attributes )
{
$this -> attributes = $attributes ;
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
public function setDataMapper ( DataMapperInterface $dataMapper = null )
{
$this -> dataMapper = $dataMapper ;
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
public function setDisabled ( $disabled )
{
$this -> disabled = ( Boolean ) $disabled ;
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
public function setEmptyData ( $emptyData )
{
$this -> emptyData = $emptyData ;
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
public function setErrorBubbling ( $errorBubbling )
{
$this -> errorBubbling = null === $errorBubbling ? null : ( Boolean ) $errorBubbling ;
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
public function setRequired ( $required )
{
$this -> required = ( Boolean ) $required ;
return $this ;
}
2012-05-17 15:09:13 +01:00
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-17 15:09:13 +01:00
*/
public function setPropertyPath ( $propertyPath )
{
if ( null !== $propertyPath && ! $propertyPath instanceof PropertyPath ) {
$propertyPath = new PropertyPath ( $propertyPath );
}
$this -> propertyPath = $propertyPath ;
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-17 15:09:13 +01:00
*/
public function setMapped ( $mapped )
{
$this -> mapped = $mapped ;
return $this ;
}
2012-05-23 13:51:26 +01:00
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-23 13:51:26 +01:00
*/
public function setByReference ( $byReference )
{
$this -> byReference = $byReference ;
return $this ;
}
2012-05-21 10:18:32 +01:00
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-21 10:18:32 +01:00
*/
public function setVirtual ( $virtual )
{
$this -> virtual = $virtual ;
return $this ;
}
2012-07-03 18:09:41 +01:00
/**
* { @ inheritdoc }
*/
public function setCompound ( $compound )
{
$this -> compound = $compound ;
return $this ;
}
2012-05-16 18:25:27 +01:00
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
public function setTypes ( array $types )
{
$this -> types = $types ;
return $this ;
}
/**
2012-05-23 18:34:41 +01:00
* { @ inheritdoc }
2012-05-16 18:25:27 +01:00
*/
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 .
*/
2012-07-09 13:50:58 +01:00
public static function validateName ( $name )
2012-05-16 18:25:27 +01:00
{
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 .
*/
2012-07-09 13:50:58 +01:00
public static function isValidName ( $name )
2012-05-16 18:25:27 +01:00
{
return '' === $name || preg_match ( '/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D' , $name );
}
}