[Form] Protected methods in FormConfigBuilder and FormBuilder from being called when it is turned into a FormConfigInterface instance

This commit is contained in:
Bernhard Schussek 2013-01-05 18:40:19 +01:00
parent fee1bf5448
commit bcc5552de4
5 changed files with 143 additions and 68 deletions

View File

@ -57,6 +57,13 @@
* The PasswordType is now not trimmed by default.
* The class FormException is now an interface. The old class is still available
under the name Symfony\Component\Form\Exception\Exception, but will probably
be removed before 2.2. If you created FormException instances manually,
you are now advised to create any of the other exceptions in the
Symfony\Component\Form\Exception namespace or to create custom exception
classes for your purpose.
#### Deprecations
* The methods `getParent()`, `setParent()` and `hasParent()` in

View File

@ -10,6 +10,8 @@ CHANGELOG
* removed special characters between the choice or text fields of DateType unless
the option "format" is set to a custom value
* deprecated FormException and introduced ExceptionInterface instead
* [BC BREAK] FormException is now an interface
* protected FormBuilder methods from being called when it is turned into a FormConfigInterface with getFormConfig()
2.1.0
-----

View File

@ -0,0 +1,21 @@
<?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\Exception;
/**
* Base BadMethodCallException for the Form component.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface
{
}

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Form;
use Symfony\Component\Form\Exception\BadMethodCallException;
use Symfony\Component\Form\Exception\Exception;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@ -65,7 +66,7 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB
public function add($child, $type = null, array $options = array())
{
if ($this->locked) {
throw new Exception('The form builder cannot be modified anymore.');
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if ($child instanceof self) {
@ -102,7 +103,7 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB
public function create($name, $type = null, array $options = array())
{
if ($this->locked) {
throw new Exception('The form builder cannot be modified anymore.');
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if (null === $type && null === $this->getDataClass()) {
@ -121,6 +122,10 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB
*/
public function get($name)
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if (isset($this->unresolvedChildren[$name])) {
return $this->resolveChild($name);
}
@ -138,7 +143,7 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB
public function remove($name)
{
if ($this->locked) {
throw new Exception('The form builder cannot be modified anymore.');
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
unset($this->unresolvedChildren[$name]);
@ -158,6 +163,10 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB
*/
public function has($name)
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if (isset($this->unresolvedChildren[$name])) {
return true;
}
@ -174,6 +183,10 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB
*/
public function all()
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->resolveChildren();
return $this->children;
@ -184,6 +197,10 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB
*/
public function count()
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return count($this->children);
}
@ -192,6 +209,10 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB
*/
public function getForm()
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->resolveChildren();
$form = new Form($this->getFormConfig());
@ -208,6 +229,10 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB
*/
public function getParent()
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->parent;
}
@ -217,7 +242,7 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB
public function setParent(FormBuilderInterface $parent = null)
{
if ($this->locked) {
throw new Exception('The form builder cannot be modified anymore.');
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->parent = $parent;
@ -230,9 +255,53 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB
*/
public function hasParent()
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return null !== $this->parent;
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return new \ArrayIterator($this->children);
}
/**
* Returns the types used by this builder.
*
* @return FormTypeInterface[] An array of FormTypeInterface
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link FormConfigInterface::getType()} instead.
*
* @throws BadMethodCallException If the builder was turned into a {@link FormConfigInterface}
* via {@link getFormConfig()}.
*/
public function getTypes()
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
trigger_error('getTypes() is deprecated since version 2.1 and will be removed in 2.3. Use getConfig() and FormConfigInterface::getType() instead.', E_USER_DEPRECATED);
$types = array();
for ($type = $this->getType(); null !== $type; $type = $type->getParent()) {
array_unshift($types, $type->getInnerType());
}
return $types;
}
/**
* Converts an unresolved child into a {@link FormBuilder} instance.
*
@ -261,33 +330,4 @@ class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormB
$this->unresolvedChildren = array();
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
return new \ArrayIterator($this->children);
}
/**
* Returns the types used by this builder.
*
* @return FormTypeInterface[] An array of FormTypeInterface
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link FormConfigInterface::getType()} instead.
*/
public function getTypes()
{
trigger_error('getTypes() is deprecated since version 2.1 and will be removed in 2.3. Use getConfig() and FormConfigInterface::getType() instead.', E_USER_DEPRECATED);
$types = array();
for ($type = $this->getType(); null !== $type; $type = $type->getParent()) {
array_unshift($types, $type->getInnerType());
}
return $types;
}
}

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Form;
use Symfony\Component\Form\Exception\BadMethodCallException;
use Symfony\Component\Form\Exception\Exception;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Util\PropertyPath;
@ -172,7 +173,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function addEventListener($eventName, $listener, $priority = 0)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->dispatcher->addListener($eventName, $listener, $priority);
@ -186,7 +187,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function addEventSubscriber(EventSubscriberInterface $subscriber)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->dispatcher->addSubscriber($subscriber);
@ -202,7 +203,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
trigger_error('addValidator() is deprecated since version 2.1 and will be removed in 2.3.', E_USER_DEPRECATED);
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->validators[] = $validator;
@ -216,7 +217,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function addViewTransformer(DataTransformerInterface $viewTransformer, $forcePrepend = false)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if ($forcePrepend) {
@ -234,7 +235,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function resetViewTransformers()
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->viewTransformers = array();
@ -249,7 +250,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
*
* @return FormConfigBuilder The configuration object.
*
* @throws Exception if the form configuration is locked
* @throws BadMethodCallException if the form configuration is locked
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link addViewTransformer()} instead.
@ -259,7 +260,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
trigger_error('appendClientTransformer() is deprecated since version 2.1 and will be removed in 2.3. Use addViewTransformer() instead.', E_USER_DEPRECATED);
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->addViewTransformer($viewTransformer);
@ -272,7 +273,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
*
* @return FormConfigBuilder The configuration object.
*
* @throws Exception if the form configuration is locked
* @throws BadMethodCallException if the form configuration is locked
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
@ -281,7 +282,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
trigger_error('prependClientTransformer() is deprecated since version 2.1 and will be removed in 2.3.', E_USER_DEPRECATED);
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->addViewTransformer($viewTransformer, true);
@ -292,7 +293,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
*
* @return FormConfigBuilder The configuration object.
*
* @throws Exception if the form configuration is locked
* @throws BadMethodCallException if the form configuration is locked
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link resetViewTransformers()} instead.
@ -302,7 +303,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
trigger_error('resetClientTransformers() is deprecated since version 2.1 and will be removed in 2.3. Use resetViewTransformers() instead.', E_USER_DEPRECATED);
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->resetViewTransformers();
@ -314,7 +315,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function addModelTransformer(DataTransformerInterface $modelTransformer, $forceAppend = false)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if ($forceAppend) {
@ -332,7 +333,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function resetModelTransformers()
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->modelTransformers = array();
@ -347,7 +348,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
*
* @return FormConfigBuilder The configuration object.
*
* @throws Exception if the form configuration is locked
* @throws BadMethodCallException if the form configuration is locked
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
@ -356,7 +357,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
trigger_error('appendNormTransformer() is deprecated since version 2.1 and will be removed in 2.3.', E_USER_DEPRECATED);
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->addModelTransformer($modelTransformer, true);
@ -369,7 +370,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
*
* @return FormConfigBuilder The configuration object.
*
* @throws Exception if the form configuration is locked
* @throws BadMethodCallException if the form configuration is locked
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link addModelTransformer()} instead.
@ -379,7 +380,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
trigger_error('prependNormTransformer() is deprecated since version 2.1 and will be removed in 2.3. Use addModelTransformer() instead.', E_USER_DEPRECATED);
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->addModelTransformer($modelTransformer);
@ -390,7 +391,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
*
* @return FormConfigBuilder The configuration object.
*
* @throws Exception if the form configuration is locked
* @throws BadMethodCallException if the form configuration is locked
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link resetModelTransformers()} instead.
@ -400,7 +401,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
trigger_error('resetNormTransformers() is deprecated since version 2.1 and will be removed in 2.3. Use resetModelTransformers() instead.', E_USER_DEPRECATED);
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->resetModelTransformers();
@ -652,7 +653,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setAttribute($name, $value)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->attributes[$name] = $value;
@ -666,7 +667,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setAttributes(array $attributes)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->attributes = $attributes;
@ -680,7 +681,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setDataMapper(DataMapperInterface $dataMapper = null)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->dataMapper = $dataMapper;
@ -694,7 +695,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setDisabled($disabled)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->disabled = (Boolean) $disabled;
@ -708,7 +709,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setEmptyData($emptyData)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->emptyData = $emptyData;
@ -722,7 +723,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setErrorBubbling($errorBubbling)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->errorBubbling = null === $errorBubbling ? null : (Boolean) $errorBubbling;
@ -736,7 +737,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setRequired($required)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->required = (Boolean) $required;
@ -750,7 +751,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setPropertyPath($propertyPath)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if (null !== $propertyPath && !$propertyPath instanceof PropertyPathInterface) {
@ -768,7 +769,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setMapped($mapped)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->mapped = $mapped;
@ -782,7 +783,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setByReference($byReference)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->byReference = $byReference;
@ -796,7 +797,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setVirtual($virtual)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->virtual = $virtual;
@ -810,7 +811,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setCompound($compound)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->compound = $compound;
@ -824,7 +825,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setType(ResolvedFormTypeInterface $type)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->type = $type;
@ -838,7 +839,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setData($data)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->data = $data;
@ -852,7 +853,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setDataLocked($locked)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->dataLocked = $locked;
@ -866,7 +867,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
public function setFormFactory(FormFactoryInterface $formFactory)
{
if ($this->locked) {
throw new Exception('The config builder cannot be modified anymore.');
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->formFactory = $formFactory;
@ -879,6 +880,10 @@ class FormConfigBuilder implements FormConfigBuilderInterface
*/
public function getFormConfig()
{
if ($this->locked) {
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
// This method should be idempotent, so clone the builder
$config = clone $this;
$config->locked = true;