-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Form] Add default transformer to TextType field (and related) #2421
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/Symfony/Component/Form/Extension/Core/DataTransformer/ValueToStringTransformer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?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\Extension\Core\DataTransformer; | ||
|
||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* Transforms between a given value and a string. | ||
* | ||
* @author Joseph Bielawski <stloyd@gmail.com> | ||
*/ | ||
class ValueToStringTransformer implements DataTransformerInterface | ||
{ | ||
/** | ||
* Transforms a value into a string. | ||
* | ||
* @param mixed $value Mixed value. | ||
* | ||
* @return string String value. | ||
* | ||
* @throws UnexpectedTypeException if the given value is not a string or number | ||
*/ | ||
public function transform($value) | ||
{ | ||
if (null === $value) { | ||
return ''; | ||
} | ||
|
||
if (!is_string($value) && !is_numeric($value)) { | ||
throw new UnexpectedTypeException($value, 'string or number'); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* Transforms a value into a string. | ||
* | ||
* @param string $value String value. | ||
* | ||
* @return string String value. | ||
* | ||
* @throws UnexpectedTypeException if the given value is not a string | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
if (null === $value) { | ||
return ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually not correct. The method should return null if $value is null or an empty string. |
||
} | ||
|
||
if (!is_string($value)) { | ||
throw new UnexpectedTypeException($value, 'string'); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...fony/Tests/Component/Form/Extension/Core/DataTransformer/ValueToStringTransformerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?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\Tests\Component\Form\Extension\Core\DataTransformer; | ||
|
||
use Symfony\Component\Form\Extension\Core\DataTransformer\ValueToStringTransformer; | ||
|
||
class ValueToStringTransformerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $transformer; | ||
|
||
protected function setUp() | ||
{ | ||
$this->transformer = new ValueToStringTransformer(); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
$this->transformer = null; | ||
} | ||
|
||
/** | ||
* @dataProvider validDataProvider | ||
*/ | ||
public function testTransform($value, $transformed) | ||
{ | ||
$this->assertEquals($transformed, $this->transformer->transform($value)); | ||
} | ||
|
||
/** | ||
* @dataProvider validDataProvider | ||
*/ | ||
public function testReverseTransform($value, $transformed) | ||
{ | ||
$this->assertEquals($transformed, $this->transformer->reverseTransform($transformed)); | ||
} | ||
|
||
public function validDataProvider() | ||
{ | ||
return array( | ||
array('test', 'test'), | ||
array('', null), | ||
array(null, null), | ||
|
||
array(0, '0'), | ||
array('0', '0'), | ||
array(1, '1'), | ||
array('123', '123'), | ||
array(1.23, '1.23'), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider invalidDataProvider | ||
*/ | ||
public function testTransformExpectsStringOrNumber($value) | ||
{ | ||
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException'); | ||
|
||
$this->transformer->transform($value); | ||
} | ||
|
||
/** | ||
* @dataProvider invalidDataProvider | ||
*/ | ||
public function testReverseTransformExpectsString($value) | ||
{ | ||
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException'); | ||
|
||
$this->transformer->reverseTransform($value); | ||
} | ||
|
||
public function invalidDataProvider() | ||
{ | ||
return array( | ||
array(true), | ||
array(false), | ||
array(new \stdClass), | ||
array(array()), | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't know if that's expected, but there is a little behavior change here.
Before adding this data transformer, empty strings were converted to NULL. It's not the case anymore.
Form::clientToNorm() returns NULL if the value is an empty string, if there is no client transformer.
Now that this client transformer is added by default on TextType, empty strings are not longer transformed to NULL.