2011-02-19 12:52:37 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
2011-04-24 12:59:46 +01:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2011-02-19 12:52:37 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Form ;
2011-04-24 20:56:06 +01:00
use Symfony\Component\Form\Exception\UnexpectedTypeException ;
2015-06-23 17:42:21 +01:00
use Symfony\Component\Form\Util\StringUtil ;
2011-02-19 12:52:37 +00:00
2011-03-02 11:20:30 +00:00
class FormFactory implements FormFactoryInterface
2011-02-19 12:52:37 +00:00
{
2011-05-10 17:23:58 +01:00
/**
2012-07-12 18:17:14 +01:00
* @ var FormRegistryInterface
2011-05-10 17:23:58 +01:00
*/
2012-07-12 18:17:14 +01:00
private $registry ;
2011-03-02 13:58:19 +00:00
2012-07-29 18:10:47 +01:00
/**
* @ var ResolvedFormTypeFactoryInterface
*/
private $resolvedTypeFactory ;
public function __construct ( FormRegistryInterface $registry , ResolvedFormTypeFactoryInterface $resolvedTypeFactory )
2011-02-19 12:52:37 +00:00
{
2012-07-12 18:17:14 +01:00
$this -> registry = $registry ;
2012-07-29 18:10:47 +01:00
$this -> resolvedTypeFactory = $resolvedTypeFactory ;
2011-04-22 18:22:26 +01:00
}
2011-05-10 17:23:58 +01:00
/**
2012-05-21 03:26:59 +01:00
* { @ inheritdoc }
2011-05-10 17:23:58 +01:00
*/
2015-06-23 17:42:21 +01:00
public function create ( $type = 'Symfony\Component\Form\Extension\Core\Type\FormType' , $data = null , array $options = array ())
2011-02-19 14:26:07 +00:00
{
2013-04-13 14:16:29 +01:00
return $this -> createBuilder ( $type , $data , $options ) -> getForm ();
2011-04-22 09:42:21 +01:00
}
2011-05-10 17:23:58 +01:00
/**
2012-05-21 03:26:59 +01:00
* { @ inheritdoc }
2011-05-10 17:23:58 +01:00
*/
2015-06-23 17:42:21 +01:00
public function createNamed ( $name , $type = 'Symfony\Component\Form\Extension\Core\Type\FormType' , $data = null , array $options = array ())
2011-04-22 09:42:21 +01:00
{
2013-04-13 14:16:29 +01:00
return $this -> createNamedBuilder ( $name , $type , $data , $options ) -> getForm ();
2011-04-22 09:42:21 +01:00
}
/**
2012-05-21 03:26:59 +01:00
* { @ inheritdoc }
2011-04-22 09:42:21 +01:00
*/
2013-04-13 14:16:29 +01:00
public function createForProperty ( $class , $property , $data = null , array $options = array ())
2011-04-22 09:42:21 +01:00
{
2013-04-13 14:16:29 +01:00
return $this -> createBuilderForProperty ( $class , $property , $data , $options ) -> getForm ();
2011-04-22 09:42:21 +01:00
}
2011-05-10 17:23:58 +01:00
/**
2012-05-21 03:26:59 +01:00
* { @ inheritdoc }
2011-05-10 17:23:58 +01:00
*/
2015-06-23 17:42:21 +01:00
public function createBuilder ( $type = 'Symfony\Component\Form\Extension\Core\Type\FormType' , $data = null , array $options = array ())
2011-04-22 09:42:21 +01:00
{
2015-06-23 17:42:21 +01:00
$name = null ;
$typeName = null ;
if ( $type instanceof ResolvedFormTypeInterface ) {
if ( method_exists ( $type , 'getBlockPrefix' )) {
// As of Symfony 3.0, the block prefix of the type is used as
// default name
$name = $type -> getBlockPrefix ();
} else {
// BC
$typeName = $type -> getName ();
}
} elseif ( $type instanceof FormTypeInterface ) {
2015-11-30 13:33:56 +00:00
if ( method_exists ( $type , 'getBlockPrefix' )) {
// As of Symfony 3.0, the block prefix of the type is used as
// default name
$name = $type -> getBlockPrefix ();
} else {
// BC
$typeName = $type -> getName ();
}
2015-09-17 22:09:48 +01:00
} elseif ( is_string ( $type )) {
2015-11-30 13:33:56 +00:00
$resolvedType = $this -> registry -> getType ( $type );
if ( method_exists ( $resolvedType , 'getBlockPrefix' )) {
// As of Symfony 3.0, the block prefix of the type is used as
// default name
$name = $resolvedType -> getBlockPrefix ();
} else {
// BC
$typeName = $type ;
}
2015-09-17 22:09:48 +01:00
} else {
throw new UnexpectedTypeException ( $type , 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface' );
2015-06-23 17:42:21 +01:00
}
2015-11-30 13:33:56 +00:00
// BC when there is no block prefix
2015-06-23 17:42:21 +01:00
if ( null === $name ) {
if ( false === strpos ( $typeName , '\\' )) {
// No FQCN - leave unchanged for BC
$name = $typeName ;
} else {
// FQCN
$name = StringUtil :: fqcnToBlockPrefix ( $typeName );
}
}
2011-04-22 09:42:21 +01:00
2013-04-13 14:16:29 +01:00
return $this -> createNamedBuilder ( $name , $type , $data , $options );
2011-04-22 09:42:21 +01:00
}
2011-03-04 15:01:50 +00:00
2011-05-10 17:23:58 +01:00
/**
2012-05-21 03:26:59 +01:00
* { @ inheritdoc }
2011-05-10 17:23:58 +01:00
*/
2015-06-23 17:42:21 +01:00
public function createNamedBuilder ( $name , $type = 'Symfony\Component\Form\Extension\Core\Type\FormType' , $data = null , array $options = array ())
2011-04-22 09:42:21 +01:00
{
2012-07-13 11:12:25 +01:00
if ( null !== $data && ! array_key_exists ( 'data' , $options )) {
2011-04-24 12:32:29 +01:00
$options [ 'data' ] = $data ;
}
2012-07-29 18:10:47 +01:00
if ( $type instanceof FormTypeInterface ) {
2015-06-23 17:42:21 +01:00
@ trigger_error ( 'Passing type instances to FormBuilder::add(), Form::add() or the FormFactory is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead.' , E_USER_DEPRECATED );
2012-08-22 15:00:43 +01:00
$type = $this -> resolveType ( $type );
2012-07-12 18:17:14 +01:00
} elseif ( is_string ( $type )) {
$type = $this -> registry -> getType ( $type );
2015-06-23 17:42:21 +01:00
} elseif ( $type instanceof ResolvedFormTypeInterface ) {
@ trigger_error ( 'Passing type instances to FormBuilder::add(), Form::add() or the FormFactory is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead.' , E_USER_DEPRECATED );
} else {
2012-07-12 18:17:14 +01:00
throw new UnexpectedTypeException ( $type , 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface' );
2011-05-13 17:29:18 +01:00
}
2011-02-19 16:30:19 +00:00
2013-09-17 10:39:17 +01:00
$builder = $type -> createBuilder ( $this , $name , $options );
// Explicitly call buildForm() in order to be able to override either
// createBuilder() or buildForm() in the resolved form type
$type -> buildForm ( $builder , $builder -> getOptions ());
return $builder ;
2011-03-18 11:50:26 +00:00
}
2011-05-10 17:23:58 +01:00
/**
2012-05-21 03:26:59 +01:00
* { @ inheritdoc }
2011-05-10 17:23:58 +01:00
*/
2013-04-13 14:16:29 +01:00
public function createBuilderForProperty ( $class , $property , $data = null , array $options = array ())
2011-03-02 13:58:19 +00:00
{
2012-11-08 18:49:46 +00:00
if ( null === $guesser = $this -> registry -> getTypeGuesser ()) {
2015-06-23 17:42:21 +01:00
return $this -> createNamedBuilder ( $property , 'Symfony\Component\Form\Extension\Core\Type\TextType' , $data , $options );
2012-11-08 18:49:46 +00:00
}
2012-07-12 18:17:14 +01:00
$typeGuess = $guesser -> guessType ( $class , $property );
$maxLengthGuess = $guesser -> guessMaxLength ( $class , $property );
$requiredGuess = $guesser -> guessRequired ( $class , $property );
$patternGuess = $guesser -> guessPattern ( $class , $property );
2011-04-22 16:41:21 +01:00
2015-06-23 17:42:21 +01:00
$type = $typeGuess ? $typeGuess -> getType () : 'Symfony\Component\Form\Extension\Core\Type\TextType' ;
2011-03-02 13:58:19 +00:00
2012-03-19 22:57:21 +00:00
$maxLength = $maxLengthGuess ? $maxLengthGuess -> getValue () : null ;
2014-10-22 19:27:13 +01:00
$pattern = $patternGuess ? $patternGuess -> getValue () : null ;
2012-03-19 22:57:21 +00:00
2012-04-23 14:55:54 +01:00
if ( null !== $pattern ) {
2014-11-23 03:56:17 +00:00
$options = array_replace_recursive ( array ( 'attr' => array ( 'pattern' => $pattern )), $options );
2012-04-23 14:55:54 +01:00
}
2012-03-19 22:57:21 +00:00
if ( null !== $maxLength ) {
2014-11-23 03:56:17 +00:00
$options = array_replace_recursive ( array ( 'attr' => array ( 'maxlength' => $maxLength )), $options );
2011-03-02 13:58:19 +00:00
}
2012-04-23 14:55:54 +01:00
if ( $requiredGuess ) {
$options = array_merge ( array ( 'required' => $requiredGuess -> getValue ()), $options );
2012-04-13 16:07:09 +01:00
}
2011-03-02 13:58:19 +00:00
// user options may override guessed options
2011-03-18 15:20:00 +00:00
if ( $typeGuess ) {
$options = array_merge ( $typeGuess -> getOptions (), $options );
2011-03-18 13:37:54 +00:00
}
2011-03-02 13:58:19 +00:00
2013-04-13 14:16:29 +01:00
return $this -> createNamedBuilder ( $property , $type , $data , $options );
2011-03-02 13:58:19 +00:00
}
2011-05-06 11:48:43 +01:00
2012-08-22 15:00:43 +01:00
/**
* Wraps a type into a ResolvedFormTypeInterface implementation and connects
* it with its parent type .
*
* @ param FormTypeInterface $type The type to resolve .
*
* @ return ResolvedFormTypeInterface The resolved type .
*/
private function resolveType ( FormTypeInterface $type )
{
$parentType = $type -> getParent ();
if ( $parentType instanceof FormTypeInterface ) {
$parentType = $this -> resolveType ( $parentType );
} elseif ( null !== $parentType ) {
$parentType = $this -> registry -> getType ( $parentType );
}
return $this -> resolvedTypeFactory -> createResolvedType (
$type ,
// Type extensions are not supported for unregistered type instances,
// i.e. type instances that are passed to the FormFactory directly,
// nor for their parents, if getParent() also returns a type instance.
array (),
$parentType
);
}
2011-03-24 21:20:54 +00:00
}