Permalink
Browse files

[Form] Protected methods in FormConfigBuilder and FormBuilder from be…

…ing called when it is turned into a FormConfigInterface instance
  • Loading branch information...
webmozart committed Jan 5, 2013
1 parent fee1bf5 commit bcc5552de4930c3e5d367c5df9a87b93bf5bc70f
View
@@ -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
@@ -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
-----
@@ -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
+{
+}
@@ -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 @@ public function __construct($name, $dataClass, EventDispatcherInterface $dispatc
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 @@ public function add($child, $type = null, array $options = array())
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 @@ public function create($name, $type = null, array $options = array())
*/
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 @@ public function get($name)
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 @@ public function remove($name)
*/
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 @@ public function has($name)
*/
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 @@ public function all()
*/
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 @@ public function count()
*/
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 @@ public function getForm()
*/
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 @@ public function getParent()
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,43 +255,22 @@ public function setParent(FormBuilderInterface $parent = null)
*/
public function hasParent()
{
- return null !== $this->parent;
- }
-
- /**
- * Converts an unresolved child into a {@link FormBuilder} instance.
- *
- * @param string $name The name of the unresolved child.
- *
- * @return FormBuilder The created instance.
- */
- private function resolveChild($name)
- {
- $info = $this->unresolvedChildren[$name];
- $child = $this->create($name, $info['type'], $info['options']);
- $this->children[$name] = $child;
- unset($this->unresolvedChildren[$name]);
-
- return $child;
- }
-
- /**
- * Converts all unresolved children into {@link FormBuilder} instances.
- */
- private function resolveChildren()
- {
- foreach ($this->unresolvedChildren as $name => $info) {
- $this->children[$name] = $this->create($name, $info['type'], $info['options']);
+ if ($this->locked) {
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
- $this->unresolvedChildren = array();
+ 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);
}
@@ -277,9 +281,16 @@ public function getIterator()
*
* @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();
@@ -290,4 +301,33 @@ public function getTypes()
return $types;
}
+
+ /**
+ * Converts an unresolved child into a {@link FormBuilder} instance.
+ *
+ * @param string $name The name of the unresolved child.
+ *
+ * @return FormBuilder The created instance.
+ */
+ private function resolveChild($name)
+ {
+ $info = $this->unresolvedChildren[$name];
+ $child = $this->create($name, $info['type'], $info['options']);
+ $this->children[$name] = $child;
+ unset($this->unresolvedChildren[$name]);
+
+ return $child;
+ }
+
+ /**
+ * Converts all unresolved children into {@link FormBuilder} instances.
+ */
+ private function resolveChildren()
+ {
+ foreach ($this->unresolvedChildren as $name => $info) {
+ $this->children[$name] = $this->create($name, $info['type'], $info['options']);
+ }
+
+ $this->unresolvedChildren = array();
+ }
}
Oops, something went wrong.

0 comments on commit bcc5552

Please sign in to comment.