From f42c77710b84bc2b06886df18a89b0862d6aa057 Mon Sep 17 00:00:00 2001 From: "Issei.M" Date: Sat, 20 Jun 2015 17:41:53 +0900 Subject: [PATCH] [Form] moved data trimming logic of TrimListener into StringUtil --- src/Symfony/Component/Form/CHANGELOG.md | 1 + .../Core/EventListener/TrimListener.php | 7 +- .../Core/EventListener/TrimListenerTest.php | 63 ---------------- .../Form/Tests/Util/StringUtilTest.php | 72 +++++++++++++++++++ .../Component/Form/Util/StringUtil.php | 42 +++++++++++ 5 files changed, 117 insertions(+), 68 deletions(-) create mode 100644 src/Symfony/Component/Form/Tests/Util/StringUtilTest.php create mode 100644 src/Symfony/Component/Form/Util/StringUtil.php diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 330f0b1417e2..76b2da1da319 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -8,6 +8,7 @@ CHANGELOG * added the html5 "range" FormType * deprecated the "cascade_validation" option in favor of setting "constraints" with the Valid constraint + * moved data trimming logic of TrimListener into StringUtil 2.7.0 ----- diff --git a/src/Symfony/Component/Form/Extension/Core/EventListener/TrimListener.php b/src/Symfony/Component/Form/Extension/Core/EventListener/TrimListener.php index db291e0e5b66..260e2699f56f 100644 --- a/src/Symfony/Component/Form/Extension/Core/EventListener/TrimListener.php +++ b/src/Symfony/Component/Form/Extension/Core/EventListener/TrimListener.php @@ -14,6 +14,7 @@ use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Form\Util\StringUtil; /** * Trims string data. @@ -30,11 +31,7 @@ public function preSubmit(FormEvent $event) return; } - if (null !== $result = @preg_replace('/^[\pZ\p{Cc}]+|[\pZ\p{Cc}]+$/u', '', $data)) { - $event->setData($result); - } else { - $event->setData(trim($data)); - } + $event->setData(StringUtil::trim($data)); } /** diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/TrimListenerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/TrimListenerTest.php index 3818c7861f23..959eb928d20e 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/TrimListenerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/TrimListenerTest.php @@ -39,67 +39,4 @@ public function testTrimSkipNonStrings() $this->assertSame(1234, $event->getData()); } - - /** - * @dataProvider spaceProvider - */ - public function testTrimUtf8Separators($hex) - { - if (!function_exists('mb_convert_encoding')) { - $this->markTestSkipped('The "mb_convert_encoding" function is not available'); - } - - // Convert hexadecimal representation into binary - // H: hex string, high nibble first (UCS-2BE) - // *: repeat until end of string - $binary = pack('H*', $hex); - - // Convert UCS-2BE to UTF-8 - $symbol = mb_convert_encoding($binary, 'UTF-8', 'UCS-2BE'); - $symbol = $symbol."ab\ncd".$symbol; - - $form = $this->getMock('Symfony\Component\Form\Test\FormInterface'); - $event = new FormEvent($form, $symbol); - - $filter = new TrimListener(); - $filter->preSubmit($event); - - $this->assertSame("ab\ncd", $event->getData()); - } - - public function spaceProvider() - { - return array( - // separators - array('0020'), - array('00A0'), - array('1680'), -// array('180E'), - array('2000'), - array('2001'), - array('2002'), - array('2003'), - array('2004'), - array('2005'), - array('2006'), - array('2007'), - array('2008'), - array('2009'), - array('200A'), - array('2028'), - array('2029'), - array('202F'), - array('205F'), - array('3000'), - // controls - array('0009'), - array('000A'), - array('000B'), - array('000C'), - array('000D'), - array('0085'), - // zero width space -// array('200B'), - ); - } } diff --git a/src/Symfony/Component/Form/Tests/Util/StringUtilTest.php b/src/Symfony/Component/Form/Tests/Util/StringUtilTest.php new file mode 100644 index 000000000000..7836852100b3 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Util/StringUtilTest.php @@ -0,0 +1,72 @@ +assertEquals('Foo!', StringUtil::trim($data)); + } + + /** + * @dataProvider spaceProvider + */ + public function testTrimUtf8Separators($hex) + { + if (!function_exists('mb_convert_encoding')) { + $this->markTestSkipped('The "mb_convert_encoding" function is not available'); + } + + // Convert hexadecimal representation into binary + // H: hex string, high nibble first (UCS-2BE) + // *: repeat until end of string + $binary = pack('H*', $hex); + + // Convert UCS-2BE to UTF-8 + $symbol = mb_convert_encoding($binary, 'UTF-8', 'UCS-2BE'); + $symbol = $symbol."ab\ncd".$symbol; + + $this->assertSame("ab\ncd", StringUtil::trim($symbol)); + } + + public function spaceProvider() + { + return array( + // separators + array('0020'), + array('00A0'), + array('1680'), +// array('180E'), + array('2000'), + array('2001'), + array('2002'), + array('2003'), + array('2004'), + array('2005'), + array('2006'), + array('2007'), + array('2008'), + array('2009'), + array('200A'), + array('2028'), + array('2029'), + array('202F'), + array('205F'), + array('3000'), + // controls + array('0009'), + array('000A'), + array('000B'), + array('000C'), + array('000D'), + array('0085'), + // zero width space +// array('200B'), + ); + } +} diff --git a/src/Symfony/Component/Form/Util/StringUtil.php b/src/Symfony/Component/Form/Util/StringUtil.php new file mode 100644 index 000000000000..2f51e74afcaa --- /dev/null +++ b/src/Symfony/Component/Form/Util/StringUtil.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Util; + +/** + * @author Issei Murasawa + * @author Bernhard Schussek + */ +class StringUtil +{ + /** + * This class should not be instantiated. + */ + private function __construct() + { + } + + /** + * Returns the trimmed data. + * + * @param string $string + * + * @return string + */ + public static function trim($string) + { + if (null !== $result = @preg_replace('/^[\pZ\p{Cc}]+|[\pZ\p{Cc}]+$/u', '', $string)) { + return $result; + } + + return trim($string); + } +}