aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php14
-rw-r--r--src/Wallabag/CoreBundle/Tests/Form/DataTransformer/StringToListTransformerTest.php50
2 files changed, 62 insertions, 2 deletions
diff --git a/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php b/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php
index 332a91b8..23488d35 100644
--- a/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php
+++ b/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php
@@ -6,10 +6,20 @@ use Doctrine\Common\Persistence\ObjectManager;
6use Symfony\Component\Form\DataTransformerInterface; 6use Symfony\Component\Form\DataTransformerInterface;
7use Symfony\Component\Form\Exception\TransformationFailedException; 7use Symfony\Component\Form\Exception\TransformationFailedException;
8 8
9/**
10 * Transforms a comma-separated list to a proper PHP array.
11 * Example: the string "foo, bar" will become the array ["foo", "bar"]
12 */
9class StringToListTransformer implements DataTransformerInterface 13class StringToListTransformer implements DataTransformerInterface
10{ 14{
15 /**
16 * @var string
17 */
11 private $separator; 18 private $separator;
12 19
20 /**
21 * @param string $separator The separator used in the list.
22 */
13 public function __construct($separator = ',') 23 public function __construct($separator = ',')
14 { 24 {
15 $this->separator = $separator; 25 $this->separator = $separator;
@@ -40,10 +50,10 @@ class StringToListTransformer implements DataTransformerInterface
40 */ 50 */
41 public function reverseTransform($string) 51 public function reverseTransform($string)
42 { 52 {
43 if (!$string) { 53 if ($string === null) {
44 return null; 54 return null;
45 } 55 }
46 56
47 return array_filter(array_map('trim', explode($this->separator, $string))); 57 return array_values(array_filter(array_map('trim', explode($this->separator, $string))));
48 } 58 }
49} 59}
diff --git a/src/Wallabag/CoreBundle/Tests/Form/DataTransformer/StringToListTransformerTest.php b/src/Wallabag/CoreBundle/Tests/Form/DataTransformer/StringToListTransformerTest.php
new file mode 100644
index 00000000..d114e5f3
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Tests/Form/DataTransformer/StringToListTransformerTest.php
@@ -0,0 +1,50 @@
1<?php
2
3namespace Wallabag\CoreBundle\Tests\Form\DataTransformer;
4
5use Wallabag\CoreBundle\Form\DataTransformer\StringToListTransformer;
6
7class StringToListTransformerTest extends \PHPUnit_Framework_TestCase
8{
9 /**
10 * @dataProvider transformProvider
11 */
12 public function testTransformWithValidData($inputData, $expectedResult)
13 {
14 $transformer = new StringToListTransformer();
15
16 $this->assertSame($expectedResult, $transformer->transform($inputData));
17 }
18
19 public function transformProvider()
20 {
21 return array(
22 array( null, '' ),
23 array( array(), '' ),
24 array( array('single value'), 'single value' ),
25 array( array('first value', 'second value'), 'first value,second value' ),
26 );
27 }
28
29 /**
30 * @dataProvider reverseTransformProvider
31 */
32 public function testReverseTransformWithValidData($inputData, $expectedResult)
33 {
34 $transformer = new StringToListTransformer();
35
36 $this->assertSame($expectedResult, $transformer->reverseTransform($inputData));
37 }
38
39 public function reverseTransformProvider()
40 {
41 return array(
42 array( null, null ),
43 array( '', array() ),
44 array( 'single value', array('single value') ),
45 array( 'first value,second value', array('first value', 'second value') ),
46 array( 'first value, second value', array('first value', 'second value') ),
47 array( 'first value, , second value', array('first value', 'second value') ),
48 );
49 }
50}