-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Add option widget to ChoiceType #15053
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
Closed
Closed
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
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
89 changes: 89 additions & 0 deletions
89
src/Symfony/Component/Form/Extension/Core/DataTransformer/ValuesToStringTransformer.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,89 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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\TransformationFailedException; | ||
|
||
/** | ||
* Converts an array of values to a string with multiple values separated by a delimiter. | ||
* | ||
* @author Bilal Amarni <[email protected]> | ||
*/ | ||
class ValuesToStringTransformer implements DataTransformerInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $delimiter; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $trim; | ||
|
||
/** | ||
* @param string $delimiter | ||
* @param bool $trim | ||
*/ | ||
public function __construct($delimiter, $trim) | ||
{ | ||
$this->delimiter = $delimiter; | ||
$this->trim = $trim; | ||
} | ||
|
||
/** | ||
* @param array $array | ||
* | ||
* @return string | ||
* | ||
* @throws UnexpectedTypeException if the given value is not an array | ||
*/ | ||
public function transform($array) | ||
{ | ||
if (null === $array) { | ||
return ''; | ||
} | ||
|
||
if (!is_array($array)) { | ||
throw new TransformationFailedException('Expected an array'); | ||
} | ||
|
||
return implode($this->delimiter, $array); | ||
} | ||
|
||
/** | ||
* @param string $string | ||
* | ||
* @return array | ||
* | ||
* @throws UnexpectedTypeException if the given value is not a string | ||
*/ | ||
public function reverseTransform($string) | ||
{ | ||
if (empty($string)) { | ||
return array(); | ||
} | ||
|
||
if (!is_string($string)) { | ||
throw new TransformationFailedException('Expected a string'); | ||
} | ||
|
||
$values = explode($this->delimiter, $string); | ||
|
||
if ($this->trim) { | ||
$values = array_map('trim', $values); | ||
} | ||
|
||
return $values; | ||
} | ||
} |
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
71 changes: 71 additions & 0 deletions
71
...ony/Component/Form/Tests/Extension/Core/DataTransformer/ValuesToStringTransformerTest.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,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer; | ||
|
||
use Symfony\Component\Form\Extension\Core\DataTransformer\ValuesToStringTransformer; | ||
|
||
class ValuesToStringTransformerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $transformer; | ||
|
||
protected function setUp() | ||
{ | ||
$this->transformer = new ValuesToStringTransformer(',', true); | ||
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. We should have tests for other delimiters too. |
||
} | ||
|
||
protected function tearDown() | ||
{ | ||
$this->transformer = null; | ||
} | ||
|
||
public function testTransform() | ||
{ | ||
$output = 'a,b,c'; | ||
|
||
$this->assertSame($output, $this->transformer->transform(array('a', 'b', 'c'))); | ||
} | ||
|
||
public function testTransformNull() | ||
{ | ||
$this->assertSame('', $this->transformer->transform(null)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
*/ | ||
public function testReverseTransformRequiresAnArray() | ||
{ | ||
$this->transformer->transform('a, b, c'); | ||
} | ||
|
||
public function testReverseTransform() | ||
{ | ||
$input = 'a, b ,c '; | ||
|
||
$this->assertSame(array('a', 'b', 'c'), $this->transformer->reverseTransform($input)); | ||
} | ||
|
||
public function testReverseTransformEmpty() | ||
{ | ||
$input = ''; | ||
|
||
$this->assertSame(array(), $this->transformer->reverseTransform($input)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
*/ | ||
public function testReverseTransformRequiresAString() | ||
{ | ||
$this->transformer->reverseTransform(array('a', 'b', 'c')); | ||
} | ||
} |
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.
missing deprecation trigger