-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] moved data trimming logic of TrimListener into StringUtil #14660
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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,72 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Form\Tests\Util; | ||
|
||
use Symfony\Component\Form\Util\StringUtil; | ||
|
||
class StringUtilTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testTrim() | ||
{ | ||
$data = ' Foo! '; | ||
|
||
$this->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'), | ||
); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 <issei.m7@gmail.com> | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
*/ | ||
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); | ||
} | ||
} |
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.
Private methods should go after public methods.
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.
Not in this case IMHO. Constructor are always first.
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.
Even before static methods?
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.
Yes, that's what we are doing in Symfony.
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.
i think the most important should be first (which are the constructor and public methods). since the constructor is private it doesn't need to be first in this case as this is not what is of interest.
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.
Actually I think private constructor should be after too, but some util classes' constructor is first. (e.g. classes in
Security/Core
,FormUtil
as well).