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;
|
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
|
|
|
*/
|
2013-04-13 14:16:29 +01:00
|
|
|
public function create($type = 'form', $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
|
|
|
*/
|
2013-04-13 14:16:29 +01:00
|
|
|
public function createNamed($name, $type = 'form', $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
|
|
|
*/
|
2013-04-13 14:16:29 +01:00
|
|
|
public function createBuilder($type = 'form', $data = null, array $options = array())
|
2011-04-22 09:42:21 +01:00
|
|
|
{
|
2012-07-12 18:17:14 +01:00
|
|
|
$name = $type instanceof FormTypeInterface || $type instanceof ResolvedFormTypeInterface
|
|
|
|
? $type->getName()
|
|
|
|
: $type;
|
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
|
|
|
*/
|
2013-04-13 14:16:29 +01:00
|
|
|
public function createNamedBuilder($name, $type = 'form', $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) {
|
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);
|
2012-07-29 18:10:47 +01:00
|
|
|
} elseif (!$type instanceof ResolvedFormTypeInterface) {
|
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-04-13 14:16:29 +01:00
|
|
|
return $type->createBuilder($this, $name, $options);
|
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()) {
|
2013-04-13 14:16:29 +01:00
|
|
|
return $this->createNamedBuilder($property, 'text', $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
|
|
|
|
2011-03-18 15:20:00 +00:00
|
|
|
$type = $typeGuess ? $typeGuess->getType() : 'text';
|
2011-03-02 13:58:19 +00:00
|
|
|
|
2012-03-19 22:57:21 +00:00
|
|
|
$maxLength = $maxLengthGuess ? $maxLengthGuess->getValue() : null;
|
2012-04-13 16:07:09 +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) {
|
|
|
|
$options = array_merge(array('pattern' => $pattern), $options);
|
|
|
|
}
|
|
|
|
|
2012-03-19 22:57:21 +00:00
|
|
|
if (null !== $maxLength) {
|
|
|
|
$options = array_merge(array('max_length' => $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
|
|
|
}
|