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...
1 parent 0d87430 commit bd5c0e05938f9601c3c5c175f13d292450fdf6fd @webmozart webmozart committed Jan 5, 2013
Showing with 135 additions and 67 deletions.
  1. +2 −0 CHANGELOG.md
  2. +21 −0 Exception/BadMethodCallException.php
  3. +72 −32 FormBuilder.php
  4. +40 −35 FormConfigBuilder.php
View
@@ -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
+{
+}
View
@@ -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 bd5c0e0

Please sign in to comment.