Skip to content

Commit f6e82d8

Browse files
committed
[Form] moved data trimming logic of TrimListener into StringUtil
1 parent 9215c22 commit f6e82d8

File tree

4 files changed

+120
-86
lines changed

4 files changed

+120
-86
lines changed

src/Symfony/Component/Form/Extension/Core/EventListener/TrimListener.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Form\FormEvents;
1515
use Symfony\Component\Form\FormEvent;
1616
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17+
use Symfony\Component\Form\Util\StringUtil;
1718

1819
/**
1920
* Trims string data.
@@ -24,17 +25,7 @@ class TrimListener implements EventSubscriberInterface
2425
{
2526
public function preSubmit(FormEvent $event)
2627
{
27-
$data = $event->getData();
28-
29-
if (!is_string($data)) {
30-
return;
31-
}
32-
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-
}
28+
$event->setData(StringUtil::trim($event->getData()));
3829
}
3930

4031
/**

src/Symfony/Component/Form/Tests/Extension/Core/EventListener/TrimListenerTest.php

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -27,79 +27,4 @@ public function testTrim()
2727

2828
$this->assertEquals('Foo!', $event->getData());
2929
}
30-
31-
public function testTrimSkipNonStrings()
32-
{
33-
$data = 1234;
34-
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
35-
$event = new FormEvent($form, $data);
36-
37-
$filter = new TrimListener();
38-
$filter->preSubmit($event);
39-
40-
$this->assertSame(1234, $event->getData());
41-
}
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-
}
10530
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Symfony\Component\Form\Tests\Util;
4+
5+
use Symfony\Component\Form\Util\StringUtil;
6+
7+
/**
8+
* @author Issei Murasawa <issei.m7@gmail.com>
9+
*/
10+
class StringUtilTest extends \PHPUnit_Framework_TestCase
11+
{
12+
public function testTrim()
13+
{
14+
$data = ' Foo! ';
15+
16+
$this->assertEquals('Foo!', StringUtil::trim($data));
17+
}
18+
19+
public function testTrimSkipNonStrings()
20+
{
21+
$data = 1234;
22+
23+
$this->assertSame(1234, StringUtil::trim($data));
24+
}
25+
26+
/**
27+
* @dataProvider spaceProvider
28+
*/
29+
public function testTrimUtf8Separators($hex)
30+
{
31+
if (!function_exists('mb_convert_encoding')) {
32+
$this->markTestSkipped('The "mb_convert_encoding" function is not available');
33+
}
34+
35+
// Convert hexadecimal representation into binary
36+
// H: hex string, high nibble first (UCS-2BE)
37+
// *: repeat until end of string
38+
$binary = pack('H*', $hex);
39+
40+
// Convert UCS-2BE to UTF-8
41+
$symbol = mb_convert_encoding($binary, 'UTF-8', 'UCS-2BE');
42+
$symbol = $symbol."ab\ncd".$symbol;
43+
44+
$this->assertSame("ab\ncd", StringUtil::trim($symbol));
45+
}
46+
47+
public function spaceProvider()
48+
{
49+
return array(
50+
// separators
51+
array('0020'),
52+
array('00A0'),
53+
array('1680'),
54+
// array('180E'),
55+
array('2000'),
56+
array('2001'),
57+
array('2002'),
58+
array('2003'),
59+
array('2004'),
60+
array('2005'),
61+
array('2006'),
62+
array('2007'),
63+
array('2008'),
64+
array('2009'),
65+
array('200A'),
66+
array('2028'),
67+
array('2029'),
68+
array('202F'),
69+
array('205F'),
70+
array('3000'),
71+
// controls
72+
array('0009'),
73+
array('000A'),
74+
array('000B'),
75+
array('000C'),
76+
array('000D'),
77+
array('0085'),
78+
// zero width space
79+
// array('200B'),
80+
);
81+
}
82+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Symfony\Component\Form\Util;
4+
5+
/**
6+
* @author Issei Murasawa <issei.m7@gmail.com>
7+
*/
8+
class StringUtil
9+
{
10+
/**
11+
* This class should not be instantiated.
12+
*/
13+
private function __construct()
14+
{
15+
}
16+
17+
/**
18+
* Returns the trimmed data.
19+
*
20+
* @param mixed $data
21+
*
22+
* @return mixed
23+
*/
24+
public static function trim($data)
25+
{
26+
if (is_string($data)) {
27+
if (null !== $result = @preg_replace('/^[\pZ\p{Cc}]+|[\pZ\p{Cc}]+$/u', '', $data)) {
28+
$data = $result;
29+
} else {
30+
$data = trim($data);
31+
}
32+
}
33+
34+
return $data;
35+
}
36+
}

0 commit comments

Comments
 (0)