8000 [Form] Fixed transform()/reverseTransform() to always throw TransformationFailedExceptions by webmozart · Pull Request #7917 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fixed transform()/reverseTransform() to always throw TransformationFailedExceptions #7917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Bridge\Doctrine\Form\DataTransformer;

use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\DataTransformerInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
Expand All @@ -27,6 +27,8 @@ class CollectionToArrayTransformer implements DataTransformerInterface
* @param Collection $collection A collection of entities
*
* @return mixed An array of entities
*
* @throws TransformationFailedException
*/
public function transform($collection)
{
Expand All @@ -35,7 +37,7 @@ public function transform($collection)
}

if (!$collection instanceof Collection) {
throw new UnexpectedTypeException($collection, 'Doctrine\Common\Collections\Collection');
throw new TransformationFailedException('Expected a Doctrine\Common\Collections\Collection object.');
}

return $collection->toArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use \PropelObjectCollection;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\TransformationFailedException;

/**
* CollectionToArrayTransformer class.
Expand All @@ -30,7 +30,7 @@ public function transform($collection)
}

if (!$collection instanceof PropelObjectCollection) {
throw new UnexpectedTypeException($collection, '\PropelObjectCollection');
throw new TransformationFailedException('Expected a \PropelObjectCollection.');
}

return $collection->getData();
Expand All @@ -45,7 +45,7 @@ public function reverseTransform($array)
}

if (!is_array($array)) {
throw new UnexpectedTypeException($array, 'array');
throw new TransformationFailedException('Expected an array.');
}

$collection->setData($array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testTransformWithNull()
}

/**
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testTransformThrowsExceptionIfNotPropelObjectCollection()
{
Expand Down Expand Up @@ -84,7 +84,7 @@ public function testReverseTransformWithEmptyString()
}

/**
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testReverseTransformThrowsExceptionIfNotArray()
{
Expand Down
8 changes: 3 additions & 5 deletions src/Symfony/Component/Form/DataTransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Transforms a value between different representations.
*
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface DataTransformerInterface
{
Expand Down Expand Up @@ -43,8 +43,7 @@ interface DataTransformerInterface
*
* @return mixed The value in the transformed representation
*
* @throws UnexpectedTypeException when the argument is not a string
* @throws TransformationFailedException when the transformation fails
* @throws \Symfony\Component\Form\Exception\TransformationFailedException When the transformation fails.
*/
public function transform($value);

Expand All @@ -70,8 +69,7 @@ public function transform($value);
*
* @return mixed The value in the original representation
*
* @throws UnexpectedTypeException when the argument is not of the expected type
* @throws TransformationFailedException when the transformation fails
* @throws \Symfony\Component\Form\Exception\TransformationFailedException When the transformation fails.
*/
public function reverseTransform($value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand All @@ -34,7 +33,7 @@ public function transform($array)
}

if (!is_array($array) ) {
throw new UnexpectedTypeException($array, 'array');
throw new TransformationFailedException('Expected an array.');
}

$result = array();
Expand All @@ -53,7 +52,7 @@ public function transform($array)
public function reverseTransform($array)
{
if (!is_array($array) ) {
throw new UnexpectedTypeException($array, 'array');
throw new TransformationFailedException('Expected an array.');
}

$result = array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\TransformationFailedException;

/**
* Transforms between a Boolean and a string.
Expand Down Expand Up @@ -45,7 +45,7 @@ public function __construct($trueValue)
*
* @return string String value.
*
* @throws UnexpectedTypeException if the given value is not a Boolean
* @throws TransformationFailedException If the given value is not a Boolean.
*/
public function transform($value)
{
Expand All @@ -54,7 +54,7 @@ public function transform($value)
}

if (!is_bool($value)) {
throw new UnexpectedTypeException($value, 'Boolean');
throw new TransformationFailedException('Expected a Boolean.');
}

return true === $value ? $this->trueValue : null;
Expand All @@ -67,7 +67,7 @@ public function transform($value)
*
* @return Boolean Boolean value.
*
* @throws UnexpectedTypeException if the given value is not a string
* @throws TransformationFailedException If the given value is not a string.
*/
public function reverseTransform($value)
{
Expand All @@ -76,7 +76,7 @@ public function reverseTransform($value)
}

if (!is_string($value)) {
throw new UnexpectedTypeException($value, 'string');
throw new TransformationFailedException('Expected a string.');
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -42,12 +41,12 @@ public function __construct(ChoiceListInterface $choiceList)
* as select tag, the value is not modified.
*
* @param mixed $choice An array if "multiple" is set to true, a scalar
* value otherwise.
* value otherwise.
*
* @return mixed An array
*
* @throws UnexpectedTypeException if the given value is not scalar
* @throws TransformationFailedException if the choices can not be retrieved
* @throws TransformationFailedException If the given value is not scalar or
* if the choices can not be retrieved.
*/
public function transform($choice)
{
Expand Down Expand Up @@ -77,14 +76,15 @@ public function transform($choice)
*
* @return mixed A scalar value
*
* @throws UnexpectedTypeException if the given value is not an array
* @throws TransformationFailedException if the recuperation of the choices fails or
* if some choice can't be found
* @throws TransformationFailedException If the given value is not an array,
* if the recuperation of the choices
* fails or if some choice can't be
* found.
*/
public function reverseTransform($values)
{
if (!is_array($values)) {
throw new UnexpectedTypeException($values, 'array');
throw new TransformationFailedException('Expected an array.');
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Form\Extension\Core\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;

Expand Down Expand Up @@ -41,7 +40,7 @@ public function transform($choice)
public function reverseTransform($value)
{
if (null !== $value && !is_scalar($value)) {
throw new UnexpectedTypeException($value, 'scalar');
throw new TransformationFailedException('Expected a scalar.');
}

// These are now valid ChoiceList values, so we can return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand All @@ -40,8 +39,8 @@ public function __construct(ChoiceListInterface $choiceList)
*
* @return mixed An array
*
* @throws UnexpectedTypeException if the given value is not an array
* @throws TransformationFailedException if the choices can not be retrieved
* @throws TransformationFailedException If the given value is not an array
* or if the choices can not be retrieved.
*/
public function transform($array)
{
Expand All @@ -50,7 +49,7 @@ public function transform($array)
}

if (!is_array($array)) {
throw new UnexpectedTypeException($array, 'array');
throw new TransformationFailedException('Expected an array.');
}

try {
Expand Down Expand Up @@ -79,14 +78,15 @@ public function transform($array)
*
* @return mixed An array
*
* @throws UnexpectedTypeException if the given value is not an array
* @throws TransformationFailedException if the recuperation of the choices fails or
* if some choice can't be found
* @throws TransformationFailedException If the given value is not an array,
* if the recuperation of the choices
* fails or if some choice can't be
* found.
*/
public function reverseTransform($values)
{
if (!is_array($values)) {
throw new UnexpectedTypeException($values, 'array');
throw new TransformationFailedException('Expected an array.');
}

try {
Expand All @@ -109,7 +109,9 @@ public function reverseTransform($values)
}

if (count($unknown) > 0) {
throw new TransformationFailedException('The choices "' . implode('", "', $unknown) . '" were not found');
throw new TransformationFailedException(
sprintf('The choices "%s" were not found', implode('", "', $unknown))
);
}

return $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\Form\Exception\TransformationFailedException;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;

/**
Expand All @@ -39,7 +38,7 @@ public function __construct(ChoiceListInterface $choiceList)
*
* @return array
*
* @throws UnexpectedTypeException if the given value is not an array
* @throws TransformationFailedException If the given value is not an array.
*/
public function transform($array)
{
Expand All @@ -48,7 +47,7 @@ public function transform($array)
}

if (!is_array($array)) {
throw new UnexpectedTypeException($array, 'array');
throw new TransformationFailedException('Expected an array.');
}

return $this->choiceList->getValuesForChoices($array);
Expand All @@ -59,8 +58,9 @@ public function transform($array)
*
* @return array
*
* @throws UnexpectedTypeException if the given value is not an array
* @throws TransformationFailedException if could not find all matching choices for the given values
* @throws TransformationFailedException If the given value is not an array
* or if no matching choice could be
* found for some given value.
*/
public function reverseTransform($array)
{
Expand All @@ -69,7 +69,7 @@ public function reverseTransform($array)
}

if (!is_array($array)) {
throw new UnexpectedTypeException($array, 'array');
throw new TransformationFailedException('Expected an array.');
}

$choices = $this->choiceList->getChoicesForValues($array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DataTransformerChain implements DataTransformerInterface
{
/**
* The value transformers
* @var array
* @var DataTransformerInterface[]
*/
protected $transformers;

Expand All @@ -48,8 +48,7 @@ public function __construct(array $transformers)
*
* @return mixed The transformed value
*
* @throws Symfony\Component\Form\Exception\TransformationFailedException
* @throws Symfony\Component\Form\Exception\UnexpectedTypeException
* @throws \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function transform($value)
{
Expand All @@ -73,8 +72,7 @@ public function transform($value)
*
* @return mixed The reverse-transformed value
*
* @throws Symfony\Component\Form\Exception\TransformationFailedException
* @throws Symfony\Component\Form\Exception\UnexpectedTypeException
* @throws \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function reverseTransform($value)
{
Expand Down
Loading
0