8000 merged branch bschussek/issue7558 (PR #7917) · symfony/symfony@0a8c2dd · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a8c2dd

Browse files
committed
merged branch bschussek/issue7558 (PR #7917)
This PR was merged into the 2.1 branch. Discussion ---------- [Form] Fixed transform()/reverseTransform() to always throw TransformationFailedExceptions | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7558 | License | MIT | Doc PR | - Commits ------- bcb5400 [Form] Fixed transform()/reverseTransform() to always throw TransformationFailedExceptions
2 parents 1510faa + bcb5400 commit 0a8c2dd

35 files changed

+164
-169
lines changed

src/Symfony/Bridge/Doctrine/Form/DataTransformer/CollectionToArrayTransformer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Bridge\Doctrine\Form\DataTransformer;
1313

14-
use Symfony\Component\Form\Exception\UnexpectedTypeException;
14+
use Symfony\Component\Form\Exception\TransformationFailedException;
1515
use Symfony\Component\Form\DataTransformerInterface;
1616
use Doctrine\Common\Collections\Collection;
1717
use Doctrine\Common\Collections\ArrayCollection;
@@ -27,6 +27,8 @@ class CollectionToArrayTransformer implements DataTransformerInterface
2727
* @param Collection $collection A collection of entities
2828
*
2929
* @return mixed An array of entities
30+
*
31+
* @throws TransformationFailedException
3032
*/
3133
public function transform($collection)
3234
{
@@ -35,7 +37,7 @@ public function transform($collection)
3537
}
3638

3739
if (!$collection instanceof Collection) {
38-
throw new UnexpectedTypeException($collection, 'Doctrine\Common\Collections\Collection');
40+
throw new TransformationFailedException('Expected a Doctrine\Common\Collections\Collection object.');
3941
}
4042

4143
return $collection->toArray();

src/Symfony/Bridge/Propel1/Form/DataTransformer/CollectionToArrayTransformer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use \PropelObjectCollection;
1515
use Symfony\Component\Form\DataTransformerInterface;
16-
use Symfony\Component\Form\Exception\UnexpectedTypeException;
16+
use Symfony\Component\Form\Exception\TransformationFailedException;
1717

1818
/**
1919
* CollectionToArrayTransformer class.
@@ -30,7 +30,7 @@ public function transform($collection)
3030
}
3131

3232
if (!$collection instanceof PropelObjectCollection) {
33-
throw new UnexpectedTypeException($collection, '\PropelObjectCollection');
33+
throw new TransformationFailedException('Expected a \PropelObjectCollection.');
3434
}
3535

3636
return $collection->getData();
@@ -45,7 +45,7 @@ public function reverseTransform($array)
4545
}
4646

4747
if (!is_array($array)) {
48-
throw new UnexpectedTypeException($array, 'array');
48+
throw new TransformationFailedException('Expected an array.');
4949
}
5050

5151
$collection->setData($array);

src/Symfony/Bridge/Propel1/Tests/Form/DataTransformer/CollectionToArrayTransformerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testTransformWithNull()
4747
}
4848

4949
/**
50-
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
50+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
5151
*/
5252
public function testTransformThrowsExceptionIfNotPropelObjectCollection()
5353
{
@@ -84,7 +84,7 @@ public function testReverseTransformWithEmptyString()
8484
}
8585

8686
/**
87-
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
87+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
8888
*/
8989
public function testReverseTransformThrowsExceptionIfNotArray()
9090
{

src/Symfony/Component/Form/DataTransformerInterface.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Transforms a value between different representations.
1616
*
17-
* @author Bernhard Schussek <bschussek@gmail.com>
17+
* @author Bernhard Schussek <bschussek@gmail.com>
1818
*/
1919
interface DataTransformerInterface
2020
{
@@ -43,8 +43,7 @@ interface DataTransformerInterface
4343
*
4444
* @return mixed The value in the transformed representation
4545
*
46-
* @throws UnexpectedTypeException when the argument is not a string
47-
* @throws TransformationFailedException when the transformation fails
46+
* @throws \Symfony\Component\Form\Exception\TransformationFailedException When the transformation fails.
4847
*/
4948
public function transform($value);
5049

@@ -70,8 +69,7 @@ public function transform($value);
7069
*
7170
* @return mixed The value in the original representation
7271
*
73-
* @throws UnexpectedTypeException when the argument is not of the expected type
74-
* @throws TransformationFailedException when the transformation fails
72+
* @throws \Symfony\Component\Form\Exception\TransformationFailedException When the transformation fails.
7573
*/
7674
public function reverseTransform($value);
7775
}

src/Symfony/Component/Form/Extension/Core/DataTransformer/ArrayToPartsTransformer.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Form\DataTransformerInterface;
1515
use Symfony\Component\Form\Exception\TransformationFailedException;
16-
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1716

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

3635
if (!is_array($array) ) {
37-
throw new UnexpectedTypeException($array, 'array');
36+
throw new TransformationFailedException('Expected an array.');
3837
}
3938

4039
$result = array();
@@ -53,7 +52,7 @@ public function transform($array)
5352
public function reverseTransform($array)
5453
{
5554
if (!is_array($array) ) {
56-
throw new UnexpectedTypeException($array, 'array');
55+
throw new TransformationFailedException('Expected an array.');
5756
}
5857

5958
$result = array();

src/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Form\Extension\Core\DataTransformer;
1313

1414
use Symfony\Component\Form\DataTransformerInterface;
15-
use Symfony\Component\Form\Exception\UnexpectedTypeException;
15+
use Symfony\Component\Form\Exception\TransformationFailedException;
1616

1717
/**
1818
* Transforms between a Boolean and a string.
@@ -45,7 +45,7 @@ public function __construct($trueValue)
4545
*
4646
* @return string String value.
4747
*
48-
* @throws UnexpectedTypeException if the given value is not a Boolean
48+
* @throws TransformationFailedException If the given value is not a Boolean.
4949
*/
5050
public function transform($value)
5151
{
@@ -54,7 +54,7 @@ public function transform($value)
5454
}
5555

5656
if (!is_bool($value)) {
57-
throw new UnexpectedTypeException($value, 'Boolean');
57+
throw new TransformationFailedException('Expected a Boolean.');
5858
}
5959

6060
return true === $value ? $this->trueValue : null;
@@ -67,7 +67,7 @@ public function transform($value)
6767
*
6868
* @return Boolean Boolean value.
6969
*
70-
* @throws UnexpectedTypeException if the given value is not a string
70+
* @throws TransformationFailedException If the given value is not a string.
7171
*/
7272
public function reverseTransform($value)
7373
{
@@ -76,7 +76,7 @@ public function reverseTransform($value)
7676
}
7777

7878
if (!is_string($value)) {
79-
throw new UnexpectedTypeException($value, 'string');
79+
throw new TransformationFailedException('Expected a string.');
8080
}
8181

8282
return true;

src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToBooleanArrayTransformer.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
1515
use Symfony\Component\Form\DataTransformerInterface;
1616
use Symfony\Component\Form\Exception\TransformationFailedException;
17-
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1817

1918
/**
2019
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -42,12 +41,12 @@ public function __construct(ChoiceListInterface $choiceList)
4241
* as select tag, the value is not modified.
4342
*
4443
* @param mixed $choice An array if "multiple" is set to true, a scalar
45-
* value otherwise.
44+
* value otherwise.
4645
*
4746
* @return mixed An array
4847
*
49-
* @throws UnexpectedTypeException if the given value is not scalar
50-
* @throws TransformationFailedException if the choices can not be retrieved
48+
* @throws TransformationFailedException If the given value is not scalar or
49+
* if the choices can not be retrieved.
5150
*/
5251
public function transform($choice)
5352
{
@@ -77,14 +76,15 @@ public function transform($choice)
7776
*
7877
* @return mixed A scalar value
7978
*
80-
* @throws UnexpectedTypeException if the given value is not an array
81-
* @throws TransformationFailedException if the recuperation of the choices fails or
82-
* if some choice can't be found
79+
* @throws TransformationFailedException If the given value is not an array,
80+
* if the recuperation of the choices
81+
* fails or if some choice can't be
82+
* found.
8383
*/
8484
public function reverseTransform($values)
8585
{
8686
if (!is_array($values)) {
87-
throw new UnexpectedTypeException($values, 'array');
87+
throw new TransformationFailedException('Expected an array.');
8888
}
8989

9090
try {

src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToValueTransformer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Form\Extension\Core\DataTransformer;
1313

1414
use Symfony\Component\Form\DataTransformerInterface;
15-
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1615
use Symfony\Component\Form\Exception\TransformationFailedException;
1716
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
1817

@@ -41,7 +40,7 @@ public function transform($choice)
4140
public function reverseTransform($value)
4241
{
4342
if (null !== $value && !is_scalar($value)) {
44-
throw new UnexpectedTypeException($value, 'scalar');
43+
throw new TransformationFailedException('Expected a scalar.');
4544
}
4645

4746
// These are now valid ChoiceList values, so we can return null

src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToBooleanArrayTransformer.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
1515
use Symfony\Component\Form\DataTransformerInterface;
1616
use Symfony\Component\Form\Exception\TransformationFailedException;
17-
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1817

1918
/**
2019
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -40,8 +39,8 @@ public function __construct(ChoiceListInterface $choiceList)
4039
*
4140
* @return mixed An array
4241
*
43-
* @throws UnexpectedTypeException if the given value is not an array
44-
* @throws TransformationFailedException if the choices can not be retrieved
42+
* @throws TransformationFailedException If the given value is not an array
43+
* or if the choices can not be retrieved.
4544
*/
4645
public function transform($array)
4746
{
@@ -50,7 +49,7 @@ public function transform($array)
5049
}
5150

5251
if (!is_array($array)) {
53-
throw new UnexpectedTypeException($array, 'array');
52+
throw new TransformationFailedException('Expected an array.');
5453
}
5554

5655
try {
@@ -79,14 +78,15 @@ public function transform($array)
7978
*
8079
* @return mixed An array
8180
*
82-
* @throws UnexpectedTypeException if the given value is not an array
83-
* @throws TransformationFailedException if the recuperation of the choices fails or
84-
* if some choice can't be found
81+
* @throws TransformationFailedException If the given value is not an array,
82+
* if the recuperation of the choices
83+
* fails or if some choice can't be
84+
* found.
8585
*/
8686
public function reverseTransform($values)
8787
{
8888
if (!is_array($values)) {
89-
throw new UnexpectedTypeException($values, 'array');
89+
throw new TransformationFailedException('Expected an array.');
9090
}
9191

9292
try {
@@ -109,7 +109,9 @@ public function reverseTransform($values)
109109
}
110110

111111
if (count($unknown) > 0) {
112-
throw new TransformationFailedException('The choices "' . implode('", "', $unknown) . '" were not found');
112+
throw new TransformationFailedException(
113+
sprintf('The choices "%s" were not found', implode('", "', $unknown))
114+
);
113115
}
114116

115117
return $result;

src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToValuesTransformer.php

Lines changed: 6 additions & 6 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\Form\Exception\TransformationFailedException;
1515

1616
use Symfony\Component\Form\DataTransformerInterface;
17-
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1817
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
1918

2019
/**
@@ -39,7 +38,7 @@ public function __construct(ChoiceListInterface $choiceList)
3938
*
4039
* @return array
4140
*
42-
* @throws UnexpectedTypeException if the given value is not an array
41+
* @throws TransformationFailedException If the given value is not an array.
4342
*/
4443
public function transform($array)
4544
{
@@ -48,7 +47,7 @@ public function transform($array)
4847
}
4948

5049
if (!is_array($array)) {
51-
throw new UnexpectedTypeException($array, 'array');
50+
throw new TransformationFailedException('Expected an array.');
5251
}
5352

5453
return $this->choiceList->getValuesForChoices($array);
@@ -59,8 +58,9 @@ public function transform($array)
5958
*
6059
* @return array
6160
*
62-
* @throws UnexpectedTypeException if the given value is not an array
63-
* @throws TransformationFailedException if could not find all matching choices for the given values
61+
* @throws TransformationFailedException If the given value is not an array
62+
* or if no matching choice could be
63+
* found for some given value.
6464
*/
6565
public function reverseTransform($array)
6666
{
@@ -69,7 +69,7 @@ public function reverseTransform($array)
6969
}
7070

7171
if (!is_array($array)) {
72-
throw new UnexpectedTypeException($array, 'array');
72+
throw new TransformationFailedException('Expected an array.');
7373
}
7474

7575
$choices = $this->choiceList->getChoicesForValues($array);

src/Symfony/Component/Form/Extension/Core/DataTransformer/DataTransformerChain.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DataTransformerChain implements DataTransformerInterface
2222
{
2323
/**
2424
* The value transformers
25-
* @var array
25+
* @var DataTransformerInterface[]
2626
*/
2727
protected $transformers;
2828

@@ -48,8 +48,7 @@ public function __construct(array $transformers)
4848
*
4949
* @return mixed The transformed value
5050
*
51-
* @throws Symfony\Component\Form\Exception\TransformationFailedException
52-
* @throws Symfony\Component\Form\Exception\UnexpectedTypeException
51+
* @throws \Symfony\Component\Form\Exception\TransformationFailedException
5352
*/
5453
public function transform($value)
5554
{
@@ -73,8 +72,7 @@ public function transform($value)
7372
*
7473
* @return mixed The reverse-transformed value
7574
*
76-
* @throws Symfony\Component\Form\Exception\TransformationFailedException
77-
* @throws Symfony\Component\Form\Exception\UnexpectedTypeException
75+
* @throws \Symfony\Component\Form\Exception\TransformationFailedException
7876
*/
7977
public function reverseTransform($value)
8078
{

0 commit comments

Comments
 (0)
0