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 ;
2018-07-26 10:03:18 +01:00
use Symfony\Component\EventDispatcher\EventDispatcherInterface ;
use Symfony\Component\EventDispatcher\EventSubscriberInterface ;
use Symfony\Component\EventDispatcher\ImmutableEventDispatcher ;
2013-01-05 17:40:19 +00:00
use Symfony\Component\Form\Exception\BadMethodCallException ;
2012-12-30 15:38:36 +00:00
use Symfony\Component\Form\Exception\InvalidArgumentException ;
2012-05-16 18:25:27 +01:00
use Symfony\Component\Form\Exception\UnexpectedTypeException ;
2013-01-07 08:19:31 +00:00
use Symfony\Component\PropertyAccess\PropertyPath ;
use Symfony\Component\PropertyAccess\PropertyPathInterface ;
2012-05-16 18:25:27 +01:00
/**
* A basic form configuration .
*
* @ author Bernhard Schussek < bschussek @ gmail . com >
*/
2012-07-28 09:59:23 +01:00
class FormConfigBuilder implements FormConfigBuilderInterface
2012-05-16 18:25:27 +01:00
{
2012-12-30 15:38:36 +00:00
/**
2013-04-20 14:56:42 +01:00
* Caches a globally unique { @ link NativeRequestHandler } instance .
2012-12-30 15:38:36 +00:00
*
2013-04-20 14:56:42 +01:00
* @ var NativeRequestHandler
2012-12-30 15:38:36 +00:00
*/
2017-11-20 06:44:43 +00:00
private static $nativeRequestHandler ;
2012-12-30 15:38:36 +00:00
/**
* The accepted request methods .
*
* @ var array
*/
2019-01-16 09:39:14 +00:00
private static $allowedMethods = [
2012-12-30 15:38:36 +00:00
'GET' ,
'PUT' ,
'POST' ,
'DELETE' ,
2014-09-21 19:53:12 +01:00
'PATCH' ,
2019-01-16 09:39:14 +00:00
];
2012-12-30 15:38:36 +00:00
2012-07-28 09:59:23 +01:00
protected $locked = false ;
2012-05-16 18:25:27 +01:00
private $dispatcher ;
private $name ;
2012-05-17 15:09:13 +01:00
/**
2019-03-12 20:16:42 +00:00
* @ var PropertyPathInterface | string | null
2012-05-17 15:09:13 +01:00
*/
private $propertyPath ;
2012-05-23 13:08:46 +01:00
private $mapped = true ;
2012-05-23 13:51:26 +01:00
private $byReference = true ;
2013-01-04 15:34:38 +00:00
private $inheritData = false ;
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
/**
2012-07-12 18:17:14 +01:00
* @ var ResolvedFormTypeInterface
2012-05-16 18:25:27 +01:00
*/
2012-07-12 18:17:14 +01:00
private $type ;
2012-05-16 18:25:27 +01:00
2019-01-16 09:39:14 +00:00
private $viewTransformers = [];
private $modelTransformers = [];
2012-05-16 18:25:27 +01:00
/**
2019-03-12 20:16:42 +00:00
* @ var DataMapperInterface | null
2012-05-16 18:25:27 +01:00
*/
private $dataMapper ;
2012-05-23 13:08:46 +01:00
private $required = true ;
private $disabled = false ;
private $errorBubbling = false ;
2012-05-16 18:25:27 +01:00
/**
* @ var mixed
*/
private $emptyData ;
2019-01-16 09:39:14 +00:00
private $attributes = [];
2012-05-16 18:25:27 +01:00
/**
* @ var mixed
*/
private $data ;
2012-05-17 15:09:13 +01:00
/**
2018-07-14 15:36:53 +01:00
* @ var string | null
2012-05-17 15:09:13 +01:00
*/
private $dataClass ;
2019-03-12 20:16:42 +00:00
private $dataLocked = false ;
2012-07-09 20:04:24 +01:00
2012-12-14 15:05:05 +00:00
/**
2019-03-12 20:16:42 +00:00
* @ var FormFactoryInterface | null
2012-12-14 15:05:05 +00:00
*/
private $formFactory ;
2012-12-30 15:38:36 +00:00
/**
2019-03-12 20:16:42 +00:00
* @ var string | null
2012-12-30 15:38:36 +00:00
*/
private $action ;
private $method = 'POST' ;
/**
2019-03-12 20:16:42 +00:00
* @ var RequestHandlerInterface | null
2012-12-30 15:38:36 +00:00
*/
2013-04-20 14:56:42 +01:00
private $requestHandler ;
2012-12-30 15:38:36 +00:00
2013-04-29 16:31:30 +01:00
private $autoInitialize = false ;
2012-05-23 17:47:14 +01:00
private $options ;
2012-05-16 18:25:27 +01:00
/**
* Creates an empty form configuration .
*
2019-07-31 12:39:02 +01:00
* @ param string $name The form name
* @ param string | null $dataClass The class of the form ' s data
2012-05-17 15:09:13 +01:00
*
2017-09-11 10:28:55 +01:00
* @ 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
*/
2019-01-16 09:39:14 +00:00
public function __construct ( $name , $dataClass , EventDispatcherInterface $dispatcher , array $options = [])
2012-05-16 18:25:27 +01:00
{
self :: validateName ( $name );
2013-10-01 04:14:37 +01:00
if ( null !== $dataClass && ! class_exists ( $dataClass ) && ! interface_exists ( $dataClass )) {
2015-01-16 15:35:16 +00:00
throw new InvalidArgumentException ( sprintf ( 'Class "%s" not found. Is the "data_class" form option set correctly?' , $dataClass ));
2012-05-17 15:09:13 +01:00
}
2012-12-14 18:27:28 +00:00
$this -> name = ( string ) $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 )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2012-05-16 18:25:27 +01:00
$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 )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2012-05-16 18:25:27 +01:00
$this -> dispatcher -> addSubscriber ( $subscriber );
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-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +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-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2019-01-16 09:39:14 +00:00
$this -> viewTransformers = [];
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-06-09 13:22:15 +01:00
public function addModelTransformer ( DataTransformerInterface $modelTransformer , $forceAppend = false )
2012-05-16 18:25:27 +01:00
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +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-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2019-01-16 09:39:14 +00:00
$this -> modelTransformers = [];
2012-05-16 18:25:27 +01:00
return $this ;
}
/**
* { @ inheritdoc }
*/
public function getEventDispatcher ()
{
2012-12-30 15:38:36 +00:00
if ( $this -> locked && ! $this -> dispatcher instanceof ImmutableEventDispatcher ) {
$this -> dispatcher = new ImmutableEventDispatcher ( $this -> dispatcher );
}
2012-05-16 18:25:27 +01:00
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 }
*/
2013-01-04 15:34:38 +00:00
public function getInheritData ()
{
return $this -> inheritData ;
}
2012-07-03 18:09:41 +01:00
/**
* { @ inheritdoc }
*/
public function getCompound ()
{
return $this -> compound ;
}
2012-05-16 18:25:27 +01:00
/**
* { @ inheritdoc }
*/
2012-07-12 18:17:14 +01:00
public function getType ()
2012-05-16 18:25:27 +01:00
{
2012-07-12 18:17:14 +01:00
return $this -> type ;
2012-05-16 18:25:27 +01:00
}
/**
* { @ inheritdoc }
*/
2012-05-23 19:21:34 +01:00
public function getViewTransformers ()
{
return $this -> viewTransformers ;
}
2012-05-16 18:25:27 +01:00
/**
* { @ inheritdoc }
*/
2012-05-23 19:21:34 +01:00
public function getModelTransformers ()
{
return $this -> modelTransformers ;
}
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 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
{
2019-02-23 15:06:07 +00:00
return \array_key_exists ( $name , $this -> attributes );
2012-05-16 18:25:27 +01:00
}
/**
* { @ inheritdoc }
*/
2012-05-23 17:47:14 +01:00
public function getAttribute ( $name , $default = null )
2012-05-16 18:25:27 +01:00
{
2019-02-23 15:06:07 +00:00
return \array_key_exists ( $name , $this -> attributes ) ? $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-07-09 20:04:24 +01:00
/**
* { @ inheritdoc }
*/
public function getDataLocked ()
{
return $this -> dataLocked ;
}
2012-12-14 15:05:05 +00:00
/**
* { @ inheritdoc }
*/
public function getFormFactory ()
{
return $this -> formFactory ;
}
2012-12-30 15:38:36 +00:00
/**
* { @ inheritdoc }
*/
public function getAction ()
{
return $this -> action ;
}
/**
* { @ inheritdoc }
*/
public function getMethod ()
{
return $this -> method ;
}
/**
* { @ inheritdoc }
*/
2013-04-20 14:56:42 +01:00
public function getRequestHandler ()
2012-12-30 15:38:36 +00:00
{
2013-04-20 14:56:42 +01:00
if ( null === $this -> requestHandler ) {
2017-11-20 06:44:43 +00:00
if ( null === self :: $nativeRequestHandler ) {
self :: $nativeRequestHandler = new NativeRequestHandler ();
2012-12-30 15:38:36 +00:00
}
2017-11-20 06:44:43 +00:00
$this -> requestHandler = self :: $nativeRequestHandler ;
2012-12-30 15:38:36 +00:00
}
2013-04-20 14:56:42 +01:00
return $this -> requestHandler ;
2012-12-30 15:38:36 +00:00
}
2013-04-29 16:31:30 +01:00
/**
* { @ inheritdoc }
*/
public function getAutoInitialize ()
{
return $this -> autoInitialize ;
}
2012-05-23 17:47:14 +01:00
/**
* { @ inheritdoc }
*/
public function getOptions ()
{
return $this -> options ;
}
/**
* { @ inheritdoc }
*/
public function hasOption ( $name )
{
2019-02-23 15:06:07 +00:00
return \array_key_exists ( $name , $this -> options );
2012-05-23 17:47:14 +01:00
}
/**
* { @ inheritdoc }
*/
public function getOption ( $name , $default = null )
{
2019-02-23 15:06:07 +00:00
return \array_key_exists ( $name , $this -> options ) ? $this -> options [ $name ] : $default ;
2012-05-23 17:47:14 +01:00
}
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 )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2012-05-16 18:25:27 +01:00
$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 )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2012-05-16 18:25:27 +01:00
$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 )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2012-05-16 18:25:27 +01:00
$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 )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2014-04-12 18:44:00 +01:00
$this -> disabled = ( bool ) $disabled ;
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
*/
public function setEmptyData ( $emptyData )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2012-05-16 18:25:27 +01:00
$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 )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2019-03-12 20:16:42 +00:00
$this -> errorBubbling = ( bool ) $errorBubbling ;
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
*/
public function setRequired ( $required )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2014-04-12 18:44:00 +01:00
$this -> required = ( bool ) $required ;
2012-05-16 18:25:27 +01:00
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 )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2012-08-26 07:29:26 +01:00
if ( null !== $propertyPath && ! $propertyPath instanceof PropertyPathInterface ) {
2012-05-17 15:09:13 +01:00
$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 )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2019-03-12 20:16:42 +00:00
$this -> mapped = ( bool ) $mapped ;
2012-05-17 15:09:13 +01:00
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 )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2019-03-12 20:16:42 +00:00
$this -> byReference = ( bool ) $byReference ;
2012-05-23 13:51:26 +01:00
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
*/
2013-01-04 15:34:38 +00:00
public function setInheritData ( $inheritData )
2012-05-21 10:18:32 +01:00
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2019-03-12 20:16:42 +00:00
$this -> inheritData = ( bool ) $inheritData ;
2012-05-21 10:18:32 +01:00
return $this ;
}
2012-07-03 18:09:41 +01:00
/**
* { @ inheritdoc }
*/
public function setCompound ( $compound )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2019-03-12 20:16:42 +00:00
$this -> compound = ( bool ) $compound ;
2012-07-03 18:09:41 +01:00
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
*/
2012-07-12 18:17:14 +01:00
public function setType ( ResolvedFormTypeInterface $type )
2012-05-16 18:25:27 +01:00
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2012-07-12 18:17:14 +01:00
$this -> type = $type ;
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
*/
public function setData ( $data )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2012-05-16 18:25:27 +01:00
$this -> data = $data ;
return $this ;
}
2012-07-09 20:04:24 +01:00
/**
* { @ inheritdoc }
*/
public function setDataLocked ( $locked )
{
2012-07-28 09:59:23 +01:00
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-07-28 09:59:23 +01:00
}
2019-03-12 20:16:42 +00:00
$this -> dataLocked = ( bool ) $locked ;
2012-07-09 20:04:24 +01:00
return $this ;
}
2012-12-14 15:05:05 +00:00
/**
* { @ inheritdoc }
*/
public function setFormFactory ( FormFactoryInterface $formFactory )
{
if ( $this -> locked ) {
2013-01-05 17:40:19 +00:00
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
2012-12-14 15:05:05 +00:00
}
$this -> formFactory = $formFactory ;
return $this ;
}
2012-12-30 15:38:36 +00:00
/**
* { @ inheritdoc }
*/
public function setAction ( $action )
{
if ( $this -> locked ) {
throw new BadMethodCallException ( 'The config builder cannot be modified anymore.' );
}
2019-03-12 20:16:42 +00:00
$this -> action = ( string ) $action ;
2012-12-30 15:38:36 +00:00
return $this ;
}
/**
* { @ inheritdoc }
*/
public function setMethod ( $method )
{
if ( $this -> locked ) {
throw new BadMethodCallException ( 'The config builder cannot be modified anymore.' );
}
$upperCaseMethod = strtoupper ( $method );
2019-03-12 20:16:42 +00:00
if ( ! \in_array ( $upperCaseMethod , self :: $allowedMethods , true )) {
2018-09-08 13:44:02 +01:00
throw new InvalidArgumentException ( sprintf ( 'The form method is "%s", but should be one of "%s".' , $method , implode ( '", "' , self :: $allowedMethods )));
2012-12-30 15:38:36 +00:00
}
$this -> method = $upperCaseMethod ;
return $this ;
}
/**
* { @ inheritdoc }
*/
2013-04-20 14:56:42 +01:00
public function setRequestHandler ( RequestHandlerInterface $requestHandler )
2012-12-30 15:38:36 +00:00
{
if ( $this -> locked ) {
throw new BadMethodCallException ( 'The config builder cannot be modified anymore.' );
}
2013-04-20 14:56:42 +01:00
$this -> requestHandler = $requestHandler ;
2012-12-30 15:38:36 +00:00
return $this ;
}
2013-04-29 16:31:30 +01:00
/**
* { @ inheritdoc }
*/
public function setAutoInitialize ( $initialize )
{
2015-09-03 14:22:43 +01:00
if ( $this -> locked ) {
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
}
2014-04-12 18:44:00 +01:00
$this -> autoInitialize = ( bool ) $initialize ;
2013-04-29 16:31:30 +01:00
return $this ;
}
2012-07-28 09:59:23 +01:00
/**
* { @ inheritdoc }
*/
public function getFormConfig ()
{
2013-01-05 17:40:19 +00:00
if ( $this -> locked ) {
throw new BadMethodCallException ( 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.' );
}
2012-07-28 09:59:23 +01:00
// This method should be idempotent, so clone the builder
$config = clone $this ;
$config -> locked = true ;
return $config ;
}
2012-05-16 18:25:27 +01:00
/**
* Validates whether the given variable is a valid form name .
*
2019-07-31 12:39:02 +01:00
* @ param string | null $name The tested form name
2012-05-16 18:25:27 +01:00
*
2017-09-11 10:28:55 +01:00
* @ throws UnexpectedTypeException if the name is not a string or an integer
* @ throws InvalidArgumentException if the name contains invalid characters
2012-05-16 18:25:27 +01:00
*/
2012-07-09 13:50:58 +01:00
public static function validateName ( $name )
2012-05-16 18:25:27 +01:00
{
2018-07-05 12:24:53 +01:00
if ( null !== $name && ! \is_string ( $name ) && ! \is_int ( $name )) {
2019-07-31 12:39:02 +01:00
throw new UnexpectedTypeException ( $name , 'string or null' );
2012-05-16 18:25:27 +01:00
}
if ( ! self :: isValidName ( $name )) {
2018-09-08 13:44:02 +01:00
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 ));
2012-05-16 18:25:27 +01:00
}
}
/**
* 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 ( " : " )
*
2019-03-12 20:16:42 +00:00
* @ param string | null $name The tested form name
2012-05-16 18:25:27 +01:00
*
2016-06-28 06:50:50 +01:00
* @ return bool Whether the name is valid
2012-05-16 18:25:27 +01:00
*/
2012-07-09 13:50:58 +01:00
public static function isValidName ( $name )
2012-05-16 18:25:27 +01:00
{
2012-12-14 18:27:28 +00:00
return '' === $name || null === $name || preg_match ( '/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D' , $name );
2012-05-16 18:25:27 +01:00
}
}