|
@@ -16,10 +16,22 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
|
|
|
|
|
class FormFactory implements FormFactoryInterface
|
|
|
{
|
|
|
+ /**
|
|
|
+ * Extensions
|
|
|
+ * @var array An array of FormExtensionInterface
|
|
|
+ */
|
|
|
private $extensions = array();
|
|
|
|
|
|
+ /**
|
|
|
+ * All known types (cache)
|
|
|
+ * @var array An array of FormTypeInterface
|
|
|
+ */
|
|
|
private $types = array();
|
|
|
|
|
|
+ /**
|
|
|
+ * The guesser chain
|
|
|
+ * @var FormTypeGuesserChain
|
|
|
+ */
|
|
|
private $guesser;
|
|
|
|
|
|
/**
|
|
@@ -40,6 +52,17 @@ class FormFactory implements FormFactoryInterface
|
|
|
$this->extensions = $extensions;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a type by name.
|
|
|
+ *
|
|
|
+ * This methods registers the type extensions from the form extensions.
|
|
|
+ *
|
|
|
+ * @param string|FormTypeInterface $name The name of the type or a type instance
|
|
|
+ *
|
|
|
+ * @return FormTypeInterface The type
|
|
|
+ *
|
|
|
+ * @throws FormException if the type can not be retrieved from any extension
|
|
|
+ */
|
|
|
public function getType($name)
|
|
|
{
|
|
|
$type = null;
|
|
@@ -80,24 +103,74 @@ class FormFactory implements FormFactoryInterface
|
|
|
return $this->types[$name];
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a form.
|
|
|
+ *
|
|
|
+ * @see createBuilder()
|
|
|
+ *
|
|
|
+ * @param string|FormTypeInterface $type The type of the form
|
|
|
+ * @param mixed $data The initial data
|
|
|
+ * @param array $options The options
|
|
|
+ *
|
|
|
+ * @return Form The form named after the type
|
|
|
+ *
|
|
|
+ * @throws FormException if any given option is not applicable to the given type
|
|
|
+ */
|
|
|
public function create($type, $data = null, array $options = array())
|
|
|
{
|
|
|
return $this->createBuilder($type, $data, $options)->getForm();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a form.
|
|
|
+ *
|
|
|
+ * @see createNamedBuilder()
|
|
|
+ *
|
|
|
+ * @param string|FormTypeInterface $type The type of the form
|
|
|
+ * @param string $name The name of the form
|
|
|
+ * @param mixed $data The initial data
|
|
|
+ * @param array $options The options
|
|
|
+ *
|
|
|
+ * @return Form The form
|
|
|
+ *
|
|
|
+ * @throws FormException if any given option is not applicable to the given type
|
|
|
+ */
|
|
|
public function createNamed($type, $name, $data = null, array $options = array())
|
|
|
{
|
|
|
return $this->createNamedBuilder($type, $name, $data, $options)->getForm();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @inheritDoc
|
|
|
+ * Returns a form for a property of a class.
|
|
|
+ *
|
|
|
+ * @see createBuilderForProperty()
|
|
|
+ *
|
|
|
+ * @param string $class The fully qualified class name
|
|
|
+ * @param string $property The name of the property to guess for
|
|
|
+ * @param mixed $data The initial data
|
|
|
+ * @param array $options The options for the builder
|
|
|
+ *
|
|
|
+ * @return Form The form named after the property
|
|
|
+ *
|
|
|
+ * @throws FormException if any given option is not applicable to the form type
|
|
|
*/
|
|
|
public function createForProperty($class, $property, $data = null, array $options = array())
|
|
|
{
|
|
|
return $this->createBuilderForProperty($class, $property, $data, $options)->getForm();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a form builder
|
|
|
+ *
|
|
|
+ * @param string|FormTypeInterface $type The type of the form
|
|
|
+ * @param string $name The name of the form
|
|
|
+ * @param mixed $data The initial data
|
|
|
+ * @param array $options The options
|
|
|
+ *
|
|
|
+ * @return FormBuilder The form builder
|
|
|
+ *
|
|
|
+ * @throws FormException if any given option is not applicable to the given type
|
|
|
+ */
|
|
|
public function createBuilder($type, $data = null, array $options = array())
|
|
|
{
|
|
|
$name = is_object($type) ? $type->getName() : $type;
|
|
@@ -105,6 +178,18 @@ class FormFactory implements FormFactoryInterface
|
|
|
return $this->createNamedBuilder($type, $name, $data, $options);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a form builder.
|
|
|
+ *
|
|
|
+ * @param string|FormTypeInterface $type The type of the form
|
|
|
+ * @param string $name The name of the form
|
|
|
+ * @param mixed $data The initial data
|
|
|
+ * @param array $options The options
|
|
|
+ *
|
|
|
+ * @return FormBuilder The form builder
|
|
|
+ *
|
|
|
+ * @throws FormException if any given option is not applicable to the given type
|
|
|
+ */
|
|
|
public function createNamedBuilder($type, $name, $data = null, array $options = array())
|
|
|
{
|
|
|
$builder = null;
|
|
@@ -131,10 +216,10 @@ class FormFactory implements FormFactoryInterface
|
|
|
$type = $type->getParent($options);
|
|
|
}
|
|
|
|
|
|
- $diff = array_diff($passedOptions, $knownOptions);
|
|
|
+ $extraOptions = array_diff($passedOptions, $knownOptions);
|
|
|
|
|
|
- if (count($diff) > 0) {
|
|
|
- throw new FormException(sprintf('The options "%s" do not exist', implode('", "', $diff)));
|
|
|
+ if (count($extraOptions) > 0) {
|
|
|
+ throw new FormException(sprintf('The options "%s" do not exist', implode('", "', $extraOptions)));
|
|
|
}
|
|
|
|
|
|
for ($i = 0, $l = count($types); $i < $l && !$builder; ++$i) {
|
|
@@ -156,6 +241,21 @@ class FormFactory implements FormFactoryInterface
|
|
|
return $builder;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a form builder for a property of a class.
|
|
|
+ *
|
|
|
+ * If any of the 'max_length', 'required' and type options can be guessed,
|
|
|
+ * and are not provided in the options argument, the guessed value is used.
|
|
|
+ *
|
|
|
+ * @param string $class The fully qualified class name
|
|
|
+ * @param string $property The name of the property to guess for
|
|
|
+ * @param mixed $data The initial data
|
|
|
+ * @param array $options The options for the builder
|
|
|
+ *
|
|
|
+ * @return FormBuilder The form builder named after the property
|
|
|
+ *
|
|
|
+ * @throws FormException if any given option is not applicable to the form type
|
|
|
+ */
|
|
|
public function createBuilderForProperty($class, $property, $data = null, array $options = array())
|
|
|
{
|
|
|
if (!$this->guesser) {
|
|
@@ -184,6 +284,9 @@ class FormFactory implements FormFactoryInterface
|
|
|
return $this->createNamedBuilder($type, $property, $data, $options);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Initializes the guesser chain.
|
|
|
+ */
|
|
|
private function loadGuesser()
|
|
|
{
|
|
|
$guessers = array();
|