Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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 7, 2013
1 parent fee1bf5 commit bcc5552
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 67 deletions.
7 changes: 7 additions & 0 deletions UPGRADE-2.2.md
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -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
-----
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Form/Exception/BadMethodCallException.php
@@ -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
{
}
104 changes: 72 additions & 32 deletions src/Symfony/Component/Form/FormBuilder.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
}
Expand All @@ -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]);
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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());
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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();
Expand All @@ -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();
}
}

0 comments on commit bcc5552

Please sign in to comment.