File tree Expand file tree Collapse file tree 5 files changed +117
-68
lines changed
Extension/Core/EventListener
Extension/Core/EventListener Expand file tree Collapse file tree 5 files changed +117
-68
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ CHANGELOG
88 * added the html5 "range" FormType
99 * deprecated the "cascade_validation" option in favor of setting "constraints"
1010 with the Valid constraint
11+ * moved data trimming logic of TrimListener into StringUtil
1112
12132.7.0
1314-----
Original file line number Diff line number Diff line change 1414use Symfony \Component \Form \FormEvents ;
1515use Symfony \Component \Form \FormEvent ;
1616use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
17+ use Symfony \Component \Form \Util \StringUtil ;
1718
1819/**
1920 * Trims string data.
@@ -30,11 +31,7 @@ public function preSubmit(FormEvent $event)
3031 return ;
3132 }
3233
33- if (null !== $ result = @preg_replace ('/^[\pZ\p{Cc}]+|[\pZ\p{Cc}]+$/u ' , '' , $ data )) {
34- $ event ->setData ($ result );
35- } else {
36- $ event ->setData (trim ($ data ));
37- }
34+ $ event ->setData (StringUtil::trim ($ data ));
3835 }
3936
4037 /**
Original file line number Diff line number Diff line change @@ -39,67 +39,4 @@ public function testTrimSkipNonStrings()
3939
4040 $ this ->assertSame (1234 , $ event ->getData ());
4141 }
42-
43- /**
44- * @dataProvider spaceProvider
45- */
46- public function testTrimUtf8Separators ($ hex )
47- {
48- if (!function_exists ('mb_convert_encoding ' )) {
49- $ this ->markTestSkipped ('The "mb_convert_encoding" function is not available ' );
50- }
51-
52- // Convert hexadecimal representation into binary
53- // H: hex string, high nibble first (UCS-2BE)
54- // *: repeat until end of string
55- $ binary = pack ('H* ' , $ hex );
56-
57- // Convert UCS-2BE to UTF-8
58- $ symbol = mb_convert_encoding ($ binary , 'UTF-8 ' , 'UCS-2BE ' );
59- $ symbol = $ symbol ."ab \ncd " .$ symbol ;
60-
61- $ form = $ this ->getMock ('Symfony\Component\Form\Test\FormInterface ' );
62- $ event = new FormEvent ($ form , $ symbol );
63-
64- $ filter = new TrimListener ();
65- $ filter ->preSubmit ($ event );
66-
67- $ this ->assertSame ("ab \ncd " , $ event ->getData ());
68- }
69-
70- public function spaceProvider ()
71- {
72- return array (
73- // separators
74- array ('0020 ' ),
75- array ('00A0 ' ),
76- array ('1680 ' ),
77- // array('180E'),
78- array ('2000 ' ),
79- array ('2001 ' ),
80- array ('2002 ' ),
81- array ('2003 ' ),
82- array ('2004 ' ),
83- array ('2005 ' ),
84- array ('2006 ' ),
85- array ('2007 ' ),
86- array ('2008 ' ),
87- array ('2009 ' ),
88- array ('200A ' ),
89- array ('2028 ' ),
90- array ('2029 ' ),
91- array ('202F ' ),
92- array ('205F ' ),
93- array ('3000 ' ),
94- // controls
95- array ('0009 ' ),
96- array ('000A ' ),
97- array ('000B ' ),
98- array ('000C ' ),
99- array ('000D ' ),
100- array ('0085 ' ),
101- // zero width space
102- // array('200B'),
103- );
104- }
10542}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Symfony \Component \Form \Tests \Util ;
4+
5+ use Symfony \Component \Form \Util \StringUtil ;
6+
7+ class StringUtilTest extends \PHPUnit_Framework_TestCase
8+ {
9+ public function testTrim ()
10+ {
11+ $ data = ' Foo! ' ;
12+
13+ $ this ->assertEquals ('Foo! ' , StringUtil::trim ($ data ));
14+ }
15+
16+ /**
17+ * @dataProvider spaceProvider
18+ */
19+ public function testTrimUtf8Separators ($ hex )
20+ {
21+ if (!function_exists ('mb_convert_encoding ' )) {
22+ $ this ->markTestSkipped ('The "mb_convert_encoding" function is not available ' );
23+ }
24+
25+ // Convert hexadecimal representation into binary
26+ // H: hex string, high nibble first (UCS-2BE)
27+ // *: repeat until end of string
28+ $ binary = pack ('H* ' , $ hex );
29+
30+ // Convert UCS-2BE to UTF-8
31+ $ symbol = mb_convert_encoding ($ binary , 'UTF-8 ' , 'UCS-2BE ' );
32+ $ symbol = $ symbol ."ab \ncd " .$ symbol ;
33+
34+ $ this ->assertSame ("ab \ncd " , StringUtil::trim ($ symbol ));
35+ }
36+
37+ public function spaceProvider ()
38+ {
39+ return array (
40+ // separators
41+ array ('0020 ' ),
42+ array ('00A0 ' ),
43+ array ('1680 ' ),
44+ // array('180E'),
45+ array ('2000 ' ),
46+ array ('2001 ' ),
47+ array ('2002 ' ),
48+ array ('2003 ' ),
49+ array ('2004 ' ),
50+ array ('2005 ' ),
51+ array ('2006 ' ),
52+ array ('2007 ' ),
53+ array ('2008 ' ),
54+ array ('2009 ' ),
55+ array ('200A ' ),
56+ array ('2028 ' ),
57+ array ('2029 ' ),
58+ array ('202F ' ),
59+ array ('205F ' ),
60+ array ('3000 ' ),
61+ // controls
62+ array ('0009 ' ),
63+ array ('000A ' ),
64+ array ('000B ' ),
65+ array ('000C ' ),
66+ array ('000D ' ),
67+ array ('0085 ' ),
68+ // zero width space
69+ // array('200B'),
70+ );
71+ }
72+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the Symfony package.
5+ *
6+ * (c) Fabien Potencier <fabien@symfony.com>
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ namespace Symfony \Component \Form \Util ;
13+
14+ /**
15+ * @author Issei Murasawa <issei.m7@gmail.com>
16+ * @author Bernhard Schussek <bschussek@gmail.com>
17+ */
18+ class StringUtil
19+ {
20+ /**
21+ * This class should not be instantiated.
22+ */
23+ private function __construct ()
24+ {
25+ }
26+
27+ /**
28+ * Returns the trimmed data.
29+ *
30+ * @param string $string
31+ *
32+ * @return string
33+ */
34+ public static function trim ($ string )
35+ {
36+ if (null !== $ result = @preg_replace ('/^[\pZ\p{Cc}]+|[\pZ\p{Cc}]+$/u ' , '' , $ string )) {
37+ return $ result ;
38+ }
39+
40+ return trim ($ string );
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments