From a595d49f3284a535e33ed2c15703459bc222b05b Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Fri, 15 Jun 2012 23:33:54 +0200 Subject: [PATCH 01/51] initial StringUtils --- .../StringAdapter/AbstractStringAdapter.php | 138 ++++++++++++++ library/Zend/Stdlib/StringAdapter/Iconv.php | 52 ++++++ .../Zend/Stdlib/StringAdapter/MbString.php | 54 ++++++ library/Zend/Stdlib/StringAdapter/Native.php | 175 ++++++++++++++++++ .../StringAdapter/StringAdapterInterface.php | 41 ++++ library/Zend/Stdlib/StringUtils.php | 76 ++++++++ 6 files changed, 536 insertions(+) create mode 100644 library/Zend/Stdlib/StringAdapter/AbstractStringAdapter.php create mode 100644 library/Zend/Stdlib/StringAdapter/Iconv.php create mode 100644 library/Zend/Stdlib/StringAdapter/MbString.php create mode 100644 library/Zend/Stdlib/StringAdapter/Native.php create mode 100644 library/Zend/Stdlib/StringAdapter/StringAdapterInterface.php create mode 100644 library/Zend/Stdlib/StringUtils.php diff --git a/library/Zend/Stdlib/StringAdapter/AbstractStringAdapter.php b/library/Zend/Stdlib/StringAdapter/AbstractStringAdapter.php new file mode 100644 index 00000000000..a0a01a93526 --- /dev/null +++ b/library/Zend/Stdlib/StringAdapter/AbstractStringAdapter.php @@ -0,0 +1,138 @@ +strlen($string, $charset); + $breakWidth = $this->strlen($break, $charset); + + $result = ''; + $lastStart = $lastSpace = 0; + + for ($current = 0; $current < $stringWidth; $current++) { + $char = $this->substr($string, $current, 1, $charset); + + $possibleBreak = $char; + if ($breakWidth !== 1) { + $possibleBreak = $this->substr($string, $current, $breakWidth, $charset); + } + + if ($possibleBreak === $break) { + $result .= $this->substr($string, $lastStart, $current - $lastStart + $breakWidth, $charset); + $current += $breakWidth - 1; + $lastStart = $lastSpace = $current + 1; + continue; + } + + if ($char === ' ') { + if ($current - $lastStart >= $width) { + $result .= $this->substr($string, $lastStart, $current - $lastStart, $charset) . $break; + $lastStart = $current + 1; + } + + $lastSpace = $current; + continue; + } + + if ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) { + $result .= $this->substr($string, $lastStart, $current - $lastStart, $charset) . $break; + $lastStart = $lastSpace = $current; + continue; + } + + if ($current - $lastStart >= $width && $lastStart < $lastSpace) { + $result .= $this->substr($string, $lastStart, $lastSpace - $lastStart, $charset) . $break; + $lastStart = $lastSpace = $lastSpace + 1; + continue; + } + } + + if ($lastStart !== $current) { + $result .= $this->substr($string, $lastStart, $current - $lastStart, $charset); + } + + return $result; + } + + /** + * String padding + * + * @param string $input + * @param integer $padLength + * @param string $padString + * @param integer $padType + * @param string $charset + * @return string + */ + public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $charset = 'UTF-8') + { + $charset = strtoupper($charset); + $return = ''; + $lengthOfPadding = $padLength - $this->strlen($input, $charset); + $padStringLength = $this->strlen($padString, $charset); + + if ($padStringLength === 0 || $lengthOfPadding <= 0) { + $return = $input; + } else { + $repeatCount = floor($lengthOfPadding / $padStringLength); + + if ($padType === \STR_PAD_BOTH) { + $lastStringLeft = ''; + $lastStringRight = ''; + $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2; + + $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength; + $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2); + $lastStringRightLength += $lastStringLength % 2; + + $lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength, $charset); + $lastStringRight = $this->substr($padString, 0, $lastStringRightLength, $charset); + + $return = str_repeat($padString, $repeatCountLeft) . $lastStringLeft + . $input + . str_repeat($padString, $repeatCountRight) . $lastStringRight; + } else { + $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength, $charset); + + if ($padType === \STR_PAD_LEFT) { + $return = str_repeat($padString, $repeatCount) . $lastString . $input; + } else { + $return = $input . str_repeat($padString, $repeatCount) . $lastString; + } + } + } + + return $return; + } +} diff --git a/library/Zend/Stdlib/StringAdapter/Iconv.php b/library/Zend/Stdlib/StringAdapter/Iconv.php new file mode 100644 index 00000000000..896bd210aae --- /dev/null +++ b/library/Zend/Stdlib/StringAdapter/Iconv.php @@ -0,0 +1,52 @@ +$method($str); + } + + return false; + } + + public function convertAsciiToUtf8($str) + { + return $str; + } + + public function convertAsciiToUtf16($str) + { + return preg_replace_callback("/./", function ($char) { + return "\0" . $char; + }, $str); + } + + public function convertAsciiToUcs2($str) + { + return $this->convertAsciiToUtf16($str); + } + + public function convertAsciiToUtf32($str) + { + return preg_replace_callback("/./", function ($char) { + return "\0\0\0" . $char; + }, $str); + } + + public function convertAsciiToUcs4($str) + { + return $this->convertAsciiToUtf32($str); + } + + public function convertUtf8ToAscii($str) + { + // TODO + return $str; + } + + public function convertUtf8ToUtf16($str) + { + // TODO + return $str; + } + + public function convertUtf8ToUcs2($str) + { + return $this->convertUtf8ToUtf16($str); + } + + public function convertUtf8ToUtf32($str) + { + // TODO + return $str; + } + + public function convertUtf8ToUcs4($str) + { + return $this->convertUtf8ToUtf32($str); + } +} diff --git a/library/Zend/Stdlib/StringAdapter/StringAdapterInterface.php b/library/Zend/Stdlib/StringAdapter/StringAdapterInterface.php new file mode 100644 index 00000000000..4b15ef5dfb1 --- /dev/null +++ b/library/Zend/Stdlib/StringAdapter/StringAdapterInterface.php @@ -0,0 +1,41 @@ +register('mbstring', new MbStringAdapter()); + } + + if (extension_loaded('iconv')) { + $broker->register('iconv', new IconvAdapter()); + } + + $broker->register('native', new NativeAdapter()); + + static::setBroker($broker); + } + return static::$broker; + } + + public static function setBroker(Broker $broker) + { + static::$broker = $broker; + } + + public static function resetBroker() + { + static::$broker = null; + } + + public static function getAdapterByCharset($charset = 'UTF-8') + { + $broker = static::getBroker(); + foreach ($broker->getPlugins() as $adapter) { + if ($adapter->isCharsetSupported($charset)) { + return $adapter; + } + } + + throw new Exception\RuntimeException("No string adapter found for charset '{$charset}'"); + } + + public static function isSingleByteCharset($charset) + { + return in_array(strtoupper($charset), static::$singleByteCharsets); + } +} From 75f96ee817b64ac4cf9534c08020954449d52966 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 8 Jul 2012 18:05:32 +0200 Subject: [PATCH 02/51] Native string adapter don't need ext/mbstring --- library/Zend/Stdlib/StringAdapter/Iconv.php | 5 +++++ library/Zend/Stdlib/StringAdapter/MbString.php | 5 +++++ library/Zend/Stdlib/StringAdapter/Native.php | 9 --------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/library/Zend/Stdlib/StringAdapter/Iconv.php b/library/Zend/Stdlib/StringAdapter/Iconv.php index 896bd210aae..314e6711501 100644 --- a/library/Zend/Stdlib/StringAdapter/Iconv.php +++ b/library/Zend/Stdlib/StringAdapter/Iconv.php @@ -15,6 +15,11 @@ class Iconv extends AbstractStringAdapter 'UTF-8', // TODO ); + /** + * Constructor + * + * @throws Exception\ExtensionNotLoadedException + */ public function __construct() { if (!extension_loaded('iconv')) { diff --git a/library/Zend/Stdlib/StringAdapter/MbString.php b/library/Zend/Stdlib/StringAdapter/MbString.php index e3432530f98..2e156680147 100644 --- a/library/Zend/Stdlib/StringAdapter/MbString.php +++ b/library/Zend/Stdlib/StringAdapter/MbString.php @@ -17,6 +17,11 @@ class MbString extends AbstractStringAdapter 'UTF-8', // TODO ); + /** + * Constructor + * + * @throws Exception\ExtensionNotLoadedException + */ public function __construct() { if (!extension_loaded('mbstring')) { diff --git a/library/Zend/Stdlib/StringAdapter/Native.php b/library/Zend/Stdlib/StringAdapter/Native.php index fa5f940db79..32707abe311 100644 --- a/library/Zend/Stdlib/StringAdapter/Native.php +++ b/library/Zend/Stdlib/StringAdapter/Native.php @@ -20,15 +20,6 @@ class Native extends AbstractStringAdapter 'UCS-4', 'UCS-4BE', 'UCS-4LE', ); - public function __construct() - { - if (!extension_loaded('mbstring')) { - throw new Exception\ExtensionNotLoadedException( - 'PHP extension "mbstring" is required for this adapter' - ); - } - } - public function isCharsetSupported($charset) { $charset = strtoupper($charset); From 800aad1e80379294e492b7f14bdfb9d319d3b1eb Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 8 Jul 2012 22:09:09 +0200 Subject: [PATCH 03/51] StringUtils: tests, no component deps --- .../StringAdapter/AbstractStringAdapter.php | 18 ++++ library/Zend/Stdlib/StringAdapter/Iconv.php | 6 -- .../Zend/Stdlib/StringAdapter/MbString.php | 6 -- library/Zend/Stdlib/StringAdapter/Native.php | 6 -- .../StringAdapter/StringAdapterInterface.php | 2 + library/Zend/Stdlib/StringUtils.php | 40 ++++---- tests/Zend/Stdlib/StringUtilsTest.php | 96 +++++++++++++++++++ 7 files changed, 138 insertions(+), 36 deletions(-) create mode 100644 tests/Zend/Stdlib/StringUtilsTest.php diff --git a/library/Zend/Stdlib/StringAdapter/AbstractStringAdapter.php b/library/Zend/Stdlib/StringAdapter/AbstractStringAdapter.php index a0a01a93526..01987b0f464 100644 --- a/library/Zend/Stdlib/StringAdapter/AbstractStringAdapter.php +++ b/library/Zend/Stdlib/StringAdapter/AbstractStringAdapter.php @@ -5,6 +5,24 @@ abstract class AbstractStringAdapter implements StringAdapterInterface { + /** + * List of supported character sets (upper case) + * + * @var string[] + */ + protected static $charsets = array(); + + public function isCharsetSupported($charset) + { + $charset = strtoupper($charset); + return in_array($charset, static::$charsets); + } + + public function getSupportedCharsets() + { + return static::$charsets; + } + /** * Word wrap * diff --git a/library/Zend/Stdlib/StringAdapter/Iconv.php b/library/Zend/Stdlib/StringAdapter/Iconv.php index 314e6711501..3baf002c068 100644 --- a/library/Zend/Stdlib/StringAdapter/Iconv.php +++ b/library/Zend/Stdlib/StringAdapter/Iconv.php @@ -29,12 +29,6 @@ public function __construct() } } - public function isCharsetSupported($charset) - { - $charset = strtoupper($charset); - return in_array($charset, static::$charsets); - } - public function strlen($str, $charset = 'UTF-8') { return iconv_strlen($str, $charset); diff --git a/library/Zend/Stdlib/StringAdapter/MbString.php b/library/Zend/Stdlib/StringAdapter/MbString.php index 2e156680147..54036733be6 100644 --- a/library/Zend/Stdlib/StringAdapter/MbString.php +++ b/library/Zend/Stdlib/StringAdapter/MbString.php @@ -31,12 +31,6 @@ public function __construct() } } - public function isCharsetSupported($charset) - { - $charset = strtoupper($charset); - return in_array($charset, static::$charsets); - } - public function strlen($str, $charset = 'UTF-8') { return mb_strlen($str, $charset); diff --git a/library/Zend/Stdlib/StringAdapter/Native.php b/library/Zend/Stdlib/StringAdapter/Native.php index 32707abe311..d4efbe1f31e 100644 --- a/library/Zend/Stdlib/StringAdapter/Native.php +++ b/library/Zend/Stdlib/StringAdapter/Native.php @@ -20,12 +20,6 @@ class Native extends AbstractStringAdapter 'UCS-4', 'UCS-4BE', 'UCS-4LE', ); - public function isCharsetSupported($charset) - { - $charset = strtoupper($charset); - return in_array($charset, static::$charsets); - } - public function strlen($str, $charset = 'UTF-8') { if (StringUtils::isSingleByteCharset($charset)) { diff --git a/library/Zend/Stdlib/StringAdapter/StringAdapterInterface.php b/library/Zend/Stdlib/StringAdapter/StringAdapterInterface.php index 4b15ef5dfb1..7b8c30efc38 100644 --- a/library/Zend/Stdlib/StringAdapter/StringAdapterInterface.php +++ b/library/Zend/Stdlib/StringAdapter/StringAdapterInterface.php @@ -7,6 +7,8 @@ interface StringAdapterInterface public function isCharsetSupported($charset); + public function getSupportedCharsets(); + public function strlen($str, $charset = 'UTF-8'); public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8'); diff --git a/library/Zend/Stdlib/StringUtils.php b/library/Zend/Stdlib/StringUtils.php index a10f0ef9d59..10d71a49be2 100644 --- a/library/Zend/Stdlib/StringUtils.php +++ b/library/Zend/Stdlib/StringUtils.php @@ -4,6 +4,7 @@ use Zend\Loader\Broker, Zend\Loader\PluginBroker, + Zend\Stdlib\StringAdapter\StringAdapterInterface, Zend\Stdlib\StringAdapter\MbString as MbStringAdapter, Zend\Stdlib\StringAdapter\Iconv as IconvAdapter, Zend\Stdlib\StringAdapter\Native as NativeAdapter; @@ -11,7 +12,7 @@ class StringUtils { - protected static $broker; + protected static $adapterRegistry; protected static $singleByteCharsets = array( 'ASCII', '7BIT', '8BIT', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', @@ -23,44 +24,47 @@ class StringUtils ); /** - * Get broker + * Get registered string adapters * - * @return Zend\Loader\Broker + * @return Zend\Stdlib\StringAdapter\StringAdapterInterface[] */ - public static function getBroker() + public static function getRegisteredAdapters() { - if (static::$broker === null) { - $broker = new PluginBroker(); + if (static::$adapterRegistry === null) { + static::$adapterRegistry = array(); if (extension_loaded('mbstring')) { - $broker->register('mbstring', new MbStringAdapter()); + static::$adapterRegistry[] = new MbStringAdapter(); } if (extension_loaded('iconv')) { - $broker->register('iconv', new IconvAdapter()); + static::$adapterRegistry[] = new IconvAdapter(); } - $broker->register('native', new NativeAdapter()); - - static::setBroker($broker); + static::$adapterRegistry[] = new NativeAdapter(); } - return static::$broker; + + return static::$adapterRegistry; } - public static function setBroker(Broker $broker) + public static function registerAdapter(StringAdapterInterface $adapter) { - static::$broker = $broker; + if (!in_array($adapter, static::$adapterRegistry, true)) { + static::$adapterRegistry[] = $adapter; + } } - public static function resetBroker() + public static function unregisterAdapter(StringAdapterInterface $adapter) { - static::$broker = null; + $index = array_search($adapter, static::$adapterRegistry, true); + if ($index !== false) { + unset(static::$adapterRegistry[$index]); + } } public static function getAdapterByCharset($charset = 'UTF-8') { - $broker = static::getBroker(); - foreach ($broker->getPlugins() as $adapter) { + foreach (static::getRegisteredAdapters() as $adapter) { if ($adapter->isCharsetSupported($charset)) { return $adapter; } diff --git a/tests/Zend/Stdlib/StringUtilsTest.php b/tests/Zend/Stdlib/StringUtilsTest.php new file mode 100644 index 00000000000..63c84d2f0de --- /dev/null +++ b/tests/Zend/Stdlib/StringUtilsTest.php @@ -0,0 +1,96 @@ +bufferedAdapters = StringUtils::getRegisteredAdapters(); + } + + public function tearDown() + { + // reset registered adapters + foreach (StringUtils::getRegisteredAdapters() as $adapter) { + StringUtils::unregisterAdapter($adapter); + } + foreach ($this->bufferedAdapters as $adapter) { + StringUtils::registerAdapter($adapter); + } + + } + + public function singleByCharsets() + { + return array( + array('AscII'), + array('ISo-8859-1'), + ); + } + + public function nonSingleByteCharsets() + { + return array( + array('UTf-8'), + array('usC-2') + ); + } + + /** + * @dataProvider singleByCharsets + * @param string $charset + */ + public function testIsSingleByteCharsetReturnsTrue($charset) + { + $this->assertTrue(StringUtils::isSingleByteCharset($charset)); + } + + /** + * @dataProvider nonSingleByteCharsets + * @param string $charset + */ + public function testIsSingleByteCharsetReturnsFalse($charset) + { + $this->assertFalse(StringUtils::isSingleByteCharset($charset)); + } + + public function testGetAdapterByCharset() + { + $adapter = StringUtils::getAdapterByCharset('UTF-8'); + + if (extension_loaded('mbstring')) { + $this->assertInstanceOf('Zend\Stdlib\StringAdapter\MbString', $adapter); + } elseif (extension_loaded('iconv')) { + $this->assertInstanceOf('Zend\Stdlib\StringAdapter\Iconv', $adapter); + } else { + $this->assertInstanceOf('Zend\Stdlib\StringAdapter\Native', $adapter); + } + } +} From 9b8c6b70a6048250bc08ef944269053316b5bf60 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 8 Jul 2012 23:35:55 +0200 Subject: [PATCH 04/51] adapter -> wrapper --- library/Zend/Stdlib/StringUtils.php | 50 +++++++++---------- .../AbstractStringWrapper.php} | 4 +- .../Iconv.php | 4 +- .../MbString.php | 4 +- .../Native.php | 4 +- .../StringWrapperInterface.php} | 4 +- tests/Zend/Stdlib/StringUtilsTest.php | 24 ++++----- 7 files changed, 47 insertions(+), 47 deletions(-) rename library/Zend/Stdlib/{StringAdapter/AbstractStringAdapter.php => StringWrapper/AbstractStringWrapper.php} (98%) rename library/Zend/Stdlib/{StringAdapter => StringWrapper}/Iconv.php (93%) rename library/Zend/Stdlib/{StringAdapter => StringWrapper}/MbString.php (93%) rename library/Zend/Stdlib/{StringAdapter => StringWrapper}/Native.php (98%) rename library/Zend/Stdlib/{StringAdapter/StringAdapterInterface.php => StringWrapper/StringWrapperInterface.php} (93%) diff --git a/library/Zend/Stdlib/StringUtils.php b/library/Zend/Stdlib/StringUtils.php index 10d71a49be2..d88544e8900 100644 --- a/library/Zend/Stdlib/StringUtils.php +++ b/library/Zend/Stdlib/StringUtils.php @@ -4,15 +4,15 @@ use Zend\Loader\Broker, Zend\Loader\PluginBroker, - Zend\Stdlib\StringAdapter\StringAdapterInterface, - Zend\Stdlib\StringAdapter\MbString as MbStringAdapter, - Zend\Stdlib\StringAdapter\Iconv as IconvAdapter, - Zend\Stdlib\StringAdapter\Native as NativeAdapter; + Zend\Stdlib\StringWrapper\StringWrapperInterface, + Zend\Stdlib\StringWrapper\MbString as MbStringWrapper, + Zend\Stdlib\StringWrapper\Iconv as IconvWrapper, + Zend\Stdlib\StringWrapper\Native as NativeWrapper; class StringUtils { - protected static $adapterRegistry; + protected static $wrapperRegistry; protected static $singleByteCharsets = array( 'ASCII', '7BIT', '8BIT', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', @@ -24,53 +24,53 @@ class StringUtils ); /** - * Get registered string adapters + * Get registered wrappers * - * @return Zend\Stdlib\StringAdapter\StringAdapterInterface[] + * @return Zend\Stdlib\StringWrapper\StringWrapperInterface[] */ - public static function getRegisteredAdapters() + public static function getRegisteredWrappers() { - if (static::$adapterRegistry === null) { - static::$adapterRegistry = array(); + if (static::$wrapperRegistry === null) { + static::$wrapperRegistry = array(); if (extension_loaded('mbstring')) { - static::$adapterRegistry[] = new MbStringAdapter(); + static::$wrapperRegistry[] = new MbStringWrapper(); } if (extension_loaded('iconv')) { - static::$adapterRegistry[] = new IconvAdapter(); + static::$wrapperRegistry[] = new IconvWrapper(); } - static::$adapterRegistry[] = new NativeAdapter(); + static::$wrapperRegistry[] = new NativeWrapper(); } - return static::$adapterRegistry; + return static::$wrapperRegistry; } - public static function registerAdapter(StringAdapterInterface $adapter) + public static function registerWrapper(StringWrapperInterface $wrapper) { - if (!in_array($adapter, static::$adapterRegistry, true)) { - static::$adapterRegistry[] = $adapter; + if (!in_array($wrapper, static::$wrapperRegistry, true)) { + static::$wrapperRegistry[] = $wrapper; } } - public static function unregisterAdapter(StringAdapterInterface $adapter) + public static function unregisterWrapper(StringWrapperInterface $wrapper) { - $index = array_search($adapter, static::$adapterRegistry, true); + $index = array_search($wrapper, static::$wrapperRegistry, true); if ($index !== false) { - unset(static::$adapterRegistry[$index]); + unset(static::$wrapperRegistry[$index]); } } - public static function getAdapterByCharset($charset = 'UTF-8') + public static function getWrapper($charset = 'UTF-8') { - foreach (static::getRegisteredAdapters() as $adapter) { - if ($adapter->isCharsetSupported($charset)) { - return $adapter; + foreach (static::getRegisteredWrappers() as $wrapper) { + if ($wrapper->isCharsetSupported($charset)) { + return $wrapper; } } - throw new Exception\RuntimeException("No string adapter found for charset '{$charset}'"); + throw new Exception\RuntimeException("No wrapper found for charset '{$charset}'"); } public static function isSingleByteCharset($charset) diff --git a/library/Zend/Stdlib/StringAdapter/AbstractStringAdapter.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php similarity index 98% rename from library/Zend/Stdlib/StringAdapter/AbstractStringAdapter.php rename to library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 01987b0f464..d3dda48a3fb 100644 --- a/library/Zend/Stdlib/StringAdapter/AbstractStringAdapter.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -1,8 +1,8 @@ bufferedAdapters = StringUtils::getRegisteredAdapters(); + $this->bufferedWrappers = StringUtils::getRegisteredWrappers(); } public function tearDown() { - // reset registered adapters - foreach (StringUtils::getRegisteredAdapters() as $adapter) { - StringUtils::unregisterAdapter($adapter); + // reset registered wrappers + foreach (StringUtils::getRegisteredWrappers() as $wrapper) { + StringUtils::unregisterWrapper($wrapper); } - foreach ($this->bufferedAdapters as $adapter) { - StringUtils::registerAdapter($adapter); + foreach ($this->bufferedWrappers as $wrapper) { + StringUtils::registerWrapper($wrapper); } } @@ -81,16 +81,16 @@ public function testIsSingleByteCharsetReturnsFalse($charset) $this->assertFalse(StringUtils::isSingleByteCharset($charset)); } - public function testGetAdapterByCharset() + public function testGetWrapper() { - $adapter = StringUtils::getAdapterByCharset('UTF-8'); + $wrapper = StringUtils::getWrapper('UTF-8'); if (extension_loaded('mbstring')) { - $this->assertInstanceOf('Zend\Stdlib\StringAdapter\MbString', $adapter); + $this->assertInstanceOf('Zend\Stdlib\StringWrapper\MbString', $wrapper); } elseif (extension_loaded('iconv')) { - $this->assertInstanceOf('Zend\Stdlib\StringAdapter\Iconv', $adapter); + $this->assertInstanceOf('Zend\Stdlib\StringWrapper\Iconv', $wrapper); } else { - $this->assertInstanceOf('Zend\Stdlib\StringAdapter\Native', $adapter); + $this->assertInstanceOf('Zend\Stdlib\StringWrapper\Native', $wrapper); } } } From 9c53ecc96844aeda77566dba2940edd7352cd799 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Fri, 13 Jul 2012 08:39:35 +0200 Subject: [PATCH 05/51] intl string wrapper and some small other changes --- .../Exception/ExtensionNotLoadedException.php | 7 + .../Stdlib/Exception/RuntimeException.php | 7 + library/Zend/Stdlib/StringUtils.php | 27 +++- .../StringWrapper/AbstractStringWrapper.php | 6 +- library/Zend/Stdlib/StringWrapper/Iconv.php | 8 +- library/Zend/Stdlib/StringWrapper/Intl.php | 68 +++++++++ .../Zend/Stdlib/StringWrapper/MbString.php | 8 +- library/Zend/Stdlib/StringWrapper/Native.php | 141 +----------------- tests/Zend/Stdlib/StringUtilsTest.php | 35 ++++- 9 files changed, 161 insertions(+), 146 deletions(-) create mode 100644 library/Zend/Stdlib/Exception/ExtensionNotLoadedException.php create mode 100644 library/Zend/Stdlib/Exception/RuntimeException.php create mode 100644 library/Zend/Stdlib/StringWrapper/Intl.php diff --git a/library/Zend/Stdlib/Exception/ExtensionNotLoadedException.php b/library/Zend/Stdlib/Exception/ExtensionNotLoadedException.php new file mode 100644 index 00000000000..40ceaf4ee32 --- /dev/null +++ b/library/Zend/Stdlib/Exception/ExtensionNotLoadedException.php @@ -0,0 +1,7 @@ +isCharsetSupported($charset)) { - return $wrapper; + foreach ($charsets as $charset) { + if (!$wrapper->isCharsetSupported($charset)) { + continue 2; + } } + + return $wrapper; } - throw new Exception\RuntimeException("No wrapper found for charset '{$charset}'"); + throw new Exception\RuntimeException('No wrapper found supporting charset(s) ' . implode(', ', $charsets)); + } + + public static function getSingleByteCharsets() + { + return static::$singleByteCharsets; } public static function isSingleByteCharset($charset) { return in_array(strtoupper($charset), static::$singleByteCharsets); } + + public static function isValidUtf8($string) + { + return ($string === '' || preg_match('/^./su', $string) == 1); + } } diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index d3dda48a3fb..8394385a3f6 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -10,17 +10,17 @@ abstract class AbstractStringWrapper implements StringWrapperInterface * * @var string[] */ - protected static $charsets = array(); + protected $charsets = array(); public function isCharsetSupported($charset) { $charset = strtoupper($charset); - return in_array($charset, static::$charsets); + return in_array($charset, $this->charsets); } public function getSupportedCharsets() { - return static::$charsets; + return $this->$charsets; } /** diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index 625285c186e..4c99be6283a 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -11,7 +11,11 @@ class Iconv extends AbstractStringWrapper * @var string[] * @link http://php.net/manual/mbstring.supported-encodings.php */ - protected static $charsets = array( + protected $charsets = array( + 'ASCII', '7BIT', '8BIT', + 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', + 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', 'ISO-8859-10', + 'ISO-8859-11', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16', 'UTF-8', // TODO ); @@ -24,7 +28,7 @@ public function __construct() { if (!extension_loaded('iconv')) { throw new Exception\ExtensionNotLoadedException( - 'PHP extension "iconv" is required for this adapter' + 'PHP extension "iconv" is required for this wrapper' ); } } diff --git a/library/Zend/Stdlib/StringWrapper/Intl.php b/library/Zend/Stdlib/StringWrapper/Intl.php new file mode 100644 index 00000000000..4a631f0b866 --- /dev/null +++ b/library/Zend/Stdlib/StringWrapper/Intl.php @@ -0,0 +1,68 @@ +charsets = StringUtils::getSingleByteCharsets(); + } public function strlen($str, $charset = 'UTF-8') { - if (StringUtils::isSingleByteCharset($charset)) { - return strlen($str); - } - - $charset = strtoupper($charset); - if ($charset == 'UTF-8') { - // replace multibyte characters with 1 byte and count bytes - return strlen(preg_replace('/(' - . '[\xc0-\xdf][\x80-\xbf]' // 2 bytes (110xxxxx 10xxxxxx) - . '|[\xe0-\xef][\x80-\xbf]{2}' // 3 bytes (1110xxxx [10xxxxxx, ...]) - . '|[\xf0-\xf7][\x80-\xbf]{3}' // 4 bytes (11110xxx [10xxxxxx, ...]) - . '|[\xf8-\xfb][\x80-\xbf]{4}' // 5 bytes (111110xx [10xxxxxx, ...]) - . '|[\xfd-\xfe][\x80-\xbf]{5}' // 6 bytes (1111110x [10xxxxxx, ...]) - . '|\xfe[\x80-\xbf]{6}' // 7 bytes (11111110 [10xxxxxx, ...]) - . ')/s', ' ', $str)); - } elseif ($charset == 'UTF-7') { - // TODO - } elseif ($charset == 'UTF-16' || $charset == 'UCS-2' || $charset == 'UCS-2BE' || $charset == 'UCS-2LE') { - return ceil(strlen($str) / 2); - } elseif ($charset == 'UTF-32' || $charset == 'UCS-4' || $charset == 'UCS-4BE' || $charset == 'UCS-4LE') { - return ceil(strlen($str) / 4); - } - - return false; + return strlen($str); } public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') { - if (StringUtils::isSingleByteCharset($charset)) { - return substr($str, $offset, $length); - } - - $charset = strtoupper($charset); - if ($charset == 'UTF-8') { - // TODO - } elseif ($charset == 'UTF-7') { - // TODO - } elseif ($charset == 'UTF-16' || $charset == 'UCS-2') { - return substr($str, $offset * 2, $length * 2); - } elseif ($charset == 'UTF-32' || $charset == 'UCS-4') { - return substr($str, $offset * 4, $length * 4); - } - - return false; + return substr($str, $offset, $length); } public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') { - if (StringUtils::isSingleByteCharset($charset)) { - return strpos($haystack, $needle, $offset); - } - - $charset = strtoupper($charset); - if ($charset == 'UTF-8') { - // TODO - } elseif ($charset == 'UTF-7') { - // TODO - } elseif ($charset == 'UTF-16' || $charset == 'UCS-2') { - // TODO - } elseif ($charset == 'UTF-32' || $charset == 'UCS-4') { - // TODO - } - - return false; + return strpos($haystack, $needle, $offset); } public function convert($str, $toCharset, $fromCharset = 'UTF-8') { - $fromName = str_replace('-', '', strtolower($fromCharset)); - $toName = str_replace('-', '', strtolower($toCharset)); - $method = 'convert' . $fromName . 'To' . $toName; - - if (method_exists($this, $method)) { - return $this->$method($str); - } - return false; } - - public function convertAsciiToUtf8($str) - { - return $str; - } - - public function convertAsciiToUtf16($str) - { - return preg_replace_callback("/./", function ($char) { - return "\0" . $char; - }, $str); - } - - public function convertAsciiToUcs2($str) - { - return $this->convertAsciiToUtf16($str); - } - - public function convertAsciiToUtf32($str) - { - return preg_replace_callback("/./", function ($char) { - return "\0\0\0" . $char; - }, $str); - } - - public function convertAsciiToUcs4($str) - { - return $this->convertAsciiToUtf32($str); - } - - public function convertUtf8ToAscii($str) - { - // TODO - return $str; - } - - public function convertUtf8ToUtf16($str) - { - // TODO - return $str; - } - - public function convertUtf8ToUcs2($str) - { - return $this->convertUtf8ToUtf16($str); - } - - public function convertUtf8ToUtf32($str) - { - // TODO - return $str; - } - - public function convertUtf8ToUcs4($str) - { - return $this->convertUtf8ToUtf32($str); - } } diff --git a/tests/Zend/Stdlib/StringUtilsTest.php b/tests/Zend/Stdlib/StringUtilsTest.php index b351aec9a9d..c38e6f221e4 100644 --- a/tests/Zend/Stdlib/StringUtilsTest.php +++ b/tests/Zend/Stdlib/StringUtilsTest.php @@ -83,8 +83,7 @@ public function testIsSingleByteCharsetReturnsFalse($charset) public function testGetWrapper() { - $wrapper = StringUtils::getWrapper('UTF-8'); - + $wrapper = StringUtils::getWrapper('ISO-8859-1'); if (extension_loaded('mbstring')) { $this->assertInstanceOf('Zend\Stdlib\StringWrapper\MbString', $wrapper); } elseif (extension_loaded('iconv')) { @@ -92,5 +91,37 @@ public function testGetWrapper() } else { $this->assertInstanceOf('Zend\Stdlib\StringWrapper\Native', $wrapper); } + + try { + $wrapper = StringUtils::getWrapper('UTF-8'); + if (extension_loaded('intl')) { + $this->assertInstanceOf('Zend\Stdlib\StringWrapper\Intl', $wrapper); + } elseif (extension_loaded('mbstring')) { + $this->assertInstanceOf('Zend\Stdlib\StringWrapper\MbString', $wrapper); + } elseif (extension_loaded('iconv')) { + $this->assertInstanceOf('Zend\Stdlib\StringWrapper\Iconv', $wrapper); + } + } catch (Exception $e) { + if (extension_loaded('intl') + || extension_loaded('mbstring') + || extension_loaded('iconv') + ) { + $this->fail("Failed to get intl, mbstring or iconv wrapper for UTF-8"); + } + } + + try { + $wrapper = StringUtils::getWrapper('UTF-8', 'ISO-8859-1'); + if (extension_loaded('mbstring')) { + $this->assertInstanceOf('Zend\Stdlib\StringWrapper\MbString', $wrapper); + } elseif (extension_loaded('iconv')) { + $this->assertInstanceOf('Zend\Stdlib\StringWrapper\Iconv', $wrapper); + } + } catch (Exception $e) { + if (extension_loaded('mbstring') || extension_loaded('iconv') + ) { + $this->fail("Failed to get mbstring or iconv wrapper for UTF-8 and ISO-8859-1"); + } + } } } From b357c972a0a2bc75cb9fd25a77fde3c613d083e3 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Mon, 26 Nov 2012 16:45:36 +0100 Subject: [PATCH 06/51] ZendTest namespace --- tests/{Zend => ZendTest}/Stdlib/StringUtilsTest.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{Zend => ZendTest}/Stdlib/StringUtilsTest.php (100%) diff --git a/tests/Zend/Stdlib/StringUtilsTest.php b/tests/ZendTest/Stdlib/StringUtilsTest.php similarity index 100% rename from tests/Zend/Stdlib/StringUtilsTest.php rename to tests/ZendTest/Stdlib/StringUtilsTest.php From 36d47de1e3de87295635b2fcf8d35effc576e857 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Mon, 26 Nov 2012 22:32:42 +0100 Subject: [PATCH 07/51] StringUtils: phpdoc + cs --- library/Zend/Stdlib/StringUtils.php | 92 ++++++++++++++++--- .../StringWrapper/AbstractStringWrapper.php | 13 +++ library/Zend/Stdlib/StringWrapper/Iconv.php | 13 +++ library/Zend/Stdlib/StringWrapper/Intl.php | 13 +++ .../Zend/Stdlib/StringWrapper/MbString.php | 13 +++ library/Zend/Stdlib/StringWrapper/Native.php | 13 +++ .../StringWrapper/StringWrapperInterface.php | 61 +++++++++++- 7 files changed, 203 insertions(+), 15 deletions(-) diff --git a/library/Zend/Stdlib/StringUtils.php b/library/Zend/Stdlib/StringUtils.php index 4aa7da8c712..2af69f43e68 100644 --- a/library/Zend/Stdlib/StringUtils.php +++ b/library/Zend/Stdlib/StringUtils.php @@ -1,19 +1,45 @@ Date: Tue, 27 Nov 2012 11:38:07 +0100 Subject: [PATCH 08/51] StringUtils: phpdoc + cs --- .../StringWrapper/AbstractStringWrapper.php | 18 +++++++-- library/Zend/Stdlib/StringWrapper/Iconv.php | 34 +++++++++++++++++ library/Zend/Stdlib/StringWrapper/Intl.php | 34 +++++++++++++++++ .../Zend/Stdlib/StringWrapper/MbString.php | 34 +++++++++++++++++ library/Zend/Stdlib/StringWrapper/Native.php | 38 +++++++++++++++++++ .../StringWrapper/StringWrapperInterface.php | 3 +- 6 files changed, 157 insertions(+), 4 deletions(-) diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index ae849c8a8fb..9eb122ea6d7 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -25,21 +25,32 @@ abstract class AbstractStringWrapper implements StringWrapperInterface */ protected $charsets = array(); + /** + * Check if the given charset is supported + * + * @param string $charset + * @return boolean + */ public function isCharsetSupported($charset) { $charset = strtoupper($charset); return in_array($charset, $this->charsets); } + /** + * Get a list of supported charsets + * + * @return string[] + */ public function getSupportedCharsets() { return $this->$charsets; } /** - * Word wrap + * Wraps a string to a given number of characters * - * @param string $string + * @param string $str * @param integer $width * @param string $break * @param boolean $cut @@ -117,7 +128,7 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $cha } /** - * String padding + * Pad a string to a certain length with another string * * @param string $input * @param integer $padLength @@ -167,3 +178,4 @@ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD return $return; } } + diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index 56b1f131b7c..73ba6dcc3aa 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -46,23 +46,57 @@ public function __construct() } } + /** + * Returns the length of the given string + * + * @param string $str + * @param string $charset + * @return int|false + */ public function strlen($str, $charset = 'UTF-8') { return iconv_strlen($str, $charset); } + /** + * Returns the portion of string specified by the start and length parameters + * + * @param string $str + * @param int $offset + * @param int|null $length + * @param string $charset + * @return string|false + */ public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') { return iconv_substr($str, $offset, $length, $charset); } + /** + * Find the position of the first occurrence of a substring in a string + * + * @param string $haystack + * @param string $needle + * @param int $offset + * @param string $charset + * @return int|false + */ public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') { return iconv_strpos($haystack, $needle, $offset, $charset); } + /** + * Convert a string from one character encoding to another + * + * @param string $str + * @param string $toCharset + * @param string $fromCharset + * @return string|false + */ public function convert($str, $toCharset, $fromCharset = 'UTF-8') { return iconv($fromCharset, $toCharset, $str); } } + diff --git a/library/Zend/Stdlib/StringWrapper/Intl.php b/library/Zend/Stdlib/StringWrapper/Intl.php index d0a486b0cd3..e7e789bc56a 100644 --- a/library/Zend/Stdlib/StringWrapper/Intl.php +++ b/library/Zend/Stdlib/StringWrapper/Intl.php @@ -39,6 +39,13 @@ public function __construct() } } + /** + * Returns the length of the given string + * + * @param string $str + * @param string $charset + * @return int|false + */ public function strlen($str, $charset = 'UTF-8') { if (strcasecmp($charset, 'UTF-8') != 0) { @@ -49,6 +56,15 @@ public function strlen($str, $charset = 'UTF-8') return grapheme_strlen($str); } + /** + * Returns the portion of string specified by the start and length parameters + * + * @param string $str + * @param int $offset + * @param int|null $length + * @param string $charset + * @return string|false + */ public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') { if (strcasecmp($charset, 'UTF-8') != 0) { @@ -59,6 +75,15 @@ public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') return grapheme_substr($str, $offset, $length); } + /** + * Find the position of the first occurrence of a substring in a string + * + * @param string $haystack + * @param string $needle + * @param int $offset + * @param string $charset + * @return int|false + */ public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') { if (strcasecmp($charset, 'UTF-8') != 0) { @@ -69,6 +94,14 @@ public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') return grapheme_strpos($haystack, $needle, $offset); } + /** + * Convert a string from one character encoding to another + * + * @param string $str + * @param string $toCharset + * @param string $fromCharset + * @return string|false + */ public function convert($str, $toCharset, $fromCharset = 'UTF-8') { if (strcasecmp($toCharset, $fromCharset) != 0) { @@ -79,3 +112,4 @@ public function convert($str, $toCharset, $fromCharset = 'UTF-8') return true; } } + diff --git a/library/Zend/Stdlib/StringWrapper/MbString.php b/library/Zend/Stdlib/StringWrapper/MbString.php index 97fcda5039c..08119bf4327 100644 --- a/library/Zend/Stdlib/StringWrapper/MbString.php +++ b/library/Zend/Stdlib/StringWrapper/MbString.php @@ -48,23 +48,57 @@ public function __construct() } } + /** + * Returns the length of the given string + * + * @param string $str + * @param string $charset + * @return int|false + */ public function strlen($str, $charset = 'UTF-8') { return mb_strlen($str, $charset); } + /** + * Returns the portion of string specified by the start and length parameters + * + * @param string $str + * @param int $offset + * @param int|null $length + * @param string $charset + * @return string|false + */ public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') { return mb_substr($str, $offset, $length, $charset); } + /** + * Find the position of the first occurrence of a substring in a string + * + * @param string $haystack + * @param string $needle + * @param int $offset + * @param string $charset + * @return int|false + */ public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') { return mb_strpos($haystack, $needle, $offset, $charset); } + /** + * Convert a string from one character encoding to another + * + * @param string $str + * @param string $toCharset + * @param string $fromCharset + * @return string|false + */ public function convert($str, $toCharset, $fromCharset = 'UTF-8') { return mb_convert_encoding($str, $toCharset, $fromCharset); } } + diff --git a/library/Zend/Stdlib/StringWrapper/Native.php b/library/Zend/Stdlib/StringWrapper/Native.php index b24b2f36961..eb6a03ebcd0 100644 --- a/library/Zend/Stdlib/StringWrapper/Native.php +++ b/library/Zend/Stdlib/StringWrapper/Native.php @@ -19,28 +19,66 @@ */ class Native extends AbstractStringWrapper { + + /** + * Constructor + */ public function __construct() { $this->charsets = StringUtils::getSingleByteCharsets(); } + /** + * Returns the length of the given string + * + * @param string $str + * @param string $charset + * @return int|false + */ public function strlen($str, $charset = 'UTF-8') { return strlen($str); } + /** + * Returns the portion of string specified by the start and length parameters + * + * @param string $str + * @param int $offset + * @param int|null $length + * @param string $charset + * @return string|false + */ public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') { return substr($str, $offset, $length); } + /** + * Find the position of the first occurrence of a substring in a string + * + * @param string $haystack + * @param string $needle + * @param int $offset + * @param string $charset + * @return int|false + */ public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') { return strpos($haystack, $needle, $offset); } + /** + * Convert a string from one character encoding to another + * + * @param string $str + * @param string $toCharset + * @param string $fromCharset + * @return string|false + */ public function convert($str, $toCharset, $fromCharset = 'UTF-8') { return false; } } + diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index 6a05b1a1048..eb7b80bbbdd 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -38,7 +38,7 @@ public function getSupportedCharsets(); * * @param string $str * @param string $charset - * @return int + * @return int|false */ public function strlen($str, $charset = 'UTF-8'); @@ -98,3 +98,4 @@ public function wordWrap($str, $width = 75, $break = "\n", $cut = false, $charse */ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $charset = 'UTF-8'); } + From 714521a725cfc378b07e337432bcd239f145cf34 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Tue, 27 Nov 2012 12:32:42 +0100 Subject: [PATCH 09/51] StringUtils: added tests --- tests/ZendTest/Stdlib/StringUtilsTest.php | 79 ++++++++++++++++++----- 1 file changed, 64 insertions(+), 15 deletions(-) diff --git a/tests/ZendTest/Stdlib/StringUtilsTest.php b/tests/ZendTest/Stdlib/StringUtilsTest.php index c38e6f221e4..ea75946790b 100644 --- a/tests/ZendTest/Stdlib/StringUtilsTest.php +++ b/tests/ZendTest/Stdlib/StringUtilsTest.php @@ -22,8 +22,8 @@ namespace ZendTest\Stdlib; -use PHPUnit_Framework_TestCase as TestCase, - Zend\Stdlib\StringUtils; +use PHPUnit_Framework_TestCase as TestCase; +use Zend\Stdlib\StringUtils; class StringUtilsTest extends TestCase { @@ -47,24 +47,32 @@ public function tearDown() } - public function singleByCharsets() + public function getSingleByCharsets() { return array( array('AscII'), + array('7bit'), + array('8bit'), array('ISo-8859-1'), - ); - } - - public function nonSingleByteCharsets() - { - return array( - array('UTf-8'), - array('usC-2') + array('ISo-8859-2'), + array('ISo-8859-3'), + array('ISo-8859-4'), + array('ISo-8859-5'), + array('ISo-8859-6'), + array('ISo-8859-7'), + array('ISo-8859-8'), + array('ISo-8859-9'), + array('ISo-8859-10'), + array('ISo-8859-11'), + array('ISo-8859-13'), + array('ISo-8859-14'), + array('ISo-8859-15'), + array('ISo-8859-16'), ); } /** - * @dataProvider singleByCharsets + * @dataProvider getSingleByCharsets * @param string $charset */ public function testIsSingleByteCharsetReturnsTrue($charset) @@ -72,8 +80,18 @@ public function testIsSingleByteCharsetReturnsTrue($charset) $this->assertTrue(StringUtils::isSingleByteCharset($charset)); } + public function getNonSingleByteCharsets() + { + return array( + array('UTf-8'), + array('UTf-16'), + array('usC-2'), + array('CESU-8'), + ); + } + /** - * @dataProvider nonSingleByteCharsets + * @dataProvider getNonSingleByteCharsets * @param string $charset */ public function testIsSingleByteCharsetReturnsFalse($charset) @@ -118,10 +136,41 @@ public function testGetWrapper() $this->assertInstanceOf('Zend\Stdlib\StringWrapper\Iconv', $wrapper); } } catch (Exception $e) { - if (extension_loaded('mbstring') || extension_loaded('iconv') - ) { + if (extension_loaded('mbstring') || extension_loaded('iconv')) { $this->fail("Failed to get mbstring or iconv wrapper for UTF-8 and ISO-8859-1"); } } } + + public function getUtf8StringValidity() + { + return array( + // valid + array('', true), + array("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F" + . "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F" + . ' !"#$%&\'()*+,-./0123456789:;<=>?' + . '@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_' + . '`abcdefghijklmnopqrstuvwxyz{|}~', + true + ), + + // invalid + array(true, false), + array(123, false), + array(123.45, false), + array("\xFF", false), + array("\x90a", false), + ); + } + + /** + * @dataProvider getUtf8StringValidity + * @param string $str + * @param boolean $valid + */ + public function testIsValidUtf8($str, $valid) + { + $this->assertSame($valid, StringUtils::isValidUtf8($str)); + } } From 9b3302c4c42d178f293636f50357f9342504422d Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Tue, 27 Nov 2012 12:33:51 +0100 Subject: [PATCH 10/51] StringUtilsTest: updated phpdoc --- tests/ZendTest/Stdlib/StringUtilsTest.php | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/tests/ZendTest/Stdlib/StringUtilsTest.php b/tests/ZendTest/Stdlib/StringUtilsTest.php index ea75946790b..94fcdd89578 100644 --- a/tests/ZendTest/Stdlib/StringUtilsTest.php +++ b/tests/ZendTest/Stdlib/StringUtilsTest.php @@ -1,23 +1,11 @@ Date: Tue, 27 Nov 2012 12:48:25 +0100 Subject: [PATCH 11/51] StringUtils: cs --- library/Zend/Stdlib/StringUtils.php | 1 - library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php | 1 - library/Zend/Stdlib/StringWrapper/Iconv.php | 1 - library/Zend/Stdlib/StringWrapper/Intl.php | 1 - library/Zend/Stdlib/StringWrapper/MbString.php | 1 - library/Zend/Stdlib/StringWrapper/Native.php | 1 - library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php | 1 - 7 files changed, 7 deletions(-) diff --git a/library/Zend/Stdlib/StringUtils.php b/library/Zend/Stdlib/StringUtils.php index 2af69f43e68..a854d4ab17a 100644 --- a/library/Zend/Stdlib/StringUtils.php +++ b/library/Zend/Stdlib/StringUtils.php @@ -164,4 +164,3 @@ public static function isValidUtf8($str) return is_string($str) && ($str === '' || preg_match('/^./su', $str) == 1); } } - diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 9eb122ea6d7..62e38a270dc 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -178,4 +178,3 @@ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD return $return; } } - diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index 73ba6dcc3aa..0e930c57df3 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -99,4 +99,3 @@ public function convert($str, $toCharset, $fromCharset = 'UTF-8') return iconv($fromCharset, $toCharset, $str); } } - diff --git a/library/Zend/Stdlib/StringWrapper/Intl.php b/library/Zend/Stdlib/StringWrapper/Intl.php index e7e789bc56a..b6954015b15 100644 --- a/library/Zend/Stdlib/StringWrapper/Intl.php +++ b/library/Zend/Stdlib/StringWrapper/Intl.php @@ -112,4 +112,3 @@ public function convert($str, $toCharset, $fromCharset = 'UTF-8') return true; } } - diff --git a/library/Zend/Stdlib/StringWrapper/MbString.php b/library/Zend/Stdlib/StringWrapper/MbString.php index 08119bf4327..dfe2fbce464 100644 --- a/library/Zend/Stdlib/StringWrapper/MbString.php +++ b/library/Zend/Stdlib/StringWrapper/MbString.php @@ -101,4 +101,3 @@ public function convert($str, $toCharset, $fromCharset = 'UTF-8') return mb_convert_encoding($str, $toCharset, $fromCharset); } } - diff --git a/library/Zend/Stdlib/StringWrapper/Native.php b/library/Zend/Stdlib/StringWrapper/Native.php index eb6a03ebcd0..7854fa93aa1 100644 --- a/library/Zend/Stdlib/StringWrapper/Native.php +++ b/library/Zend/Stdlib/StringWrapper/Native.php @@ -81,4 +81,3 @@ public function convert($str, $toCharset, $fromCharset = 'UTF-8') return false; } } - diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index eb7b80bbbdd..f78da2e4da1 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -98,4 +98,3 @@ public function wordWrap($str, $width = 75, $break = "\n", $cut = false, $charse */ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $charset = 'UTF-8'); } - From 53652e27ed22a85a0f3add6e56155315ffabe71a Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 15:13:42 +0100 Subject: [PATCH 12/51] StringUtils: tests + fixes + supported encodings for iconv and mbstring --- .../StringWrapper/AbstractStringWrapper.php | 15 +- library/Zend/Stdlib/StringWrapper/Iconv.php | 97 +++++- library/Zend/Stdlib/StringWrapper/Intl.php | 3 +- .../Zend/Stdlib/StringWrapper/MbString.php | 107 ++++++- library/Zend/Stdlib/StringWrapper/Native.php | 7 +- .../StringWrapper/CommonStringWrapperTest.php | 302 ++++++++++++++++++ .../Stdlib/StringWrapper/IconvTest.php | 34 ++ .../Stdlib/StringWrapper/IntlTest.php | 34 ++ .../Stdlib/StringWrapper/MbStringTest.php | 34 ++ .../Stdlib/StringWrapper/NativeTest.php | 24 ++ 10 files changed, 637 insertions(+), 20 deletions(-) create mode 100644 tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php create mode 100644 tests/ZendTest/Stdlib/StringWrapper/IconvTest.php create mode 100644 tests/ZendTest/Stdlib/StringWrapper/IntlTest.php create mode 100644 tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php create mode 100644 tests/ZendTest/Stdlib/StringWrapper/NativeTest.php diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 62e38a270dc..07298335abc 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -10,6 +10,9 @@ namespace Zend\Stdlib\StringWrapper; +use Zend\Stdlib\Exception; +use Zend\Stdlib\StringUtils; + /** * @category Zend * @package Zend_Stdlib @@ -75,7 +78,11 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $cha throw new Exception\InvalidArgumentException('Cannot force cut when width is zero'); } - $charset = strtoupper($charset); + $charset = strtoupper($charset); + if (StringUtils::isSingleByteCharset($charset)) { + return wordwrap($string, $width, $break, $cut); + } + $stringWidth = $this->strlen($string, $charset); $breakWidth = $this->strlen($break, $charset); @@ -139,7 +146,11 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $cha */ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $charset = 'UTF-8') { - $charset = strtoupper($charset); + $charset = strtoupper($charset); + if (StringUtils::isSingleByteCharset($charset)) { + return str_pad($input, $padLength, $padString, $padType); + } + $return = ''; $lengthOfPadding = $padLength - $this->strlen($input, $charset); $padStringLength = $this->strlen($padString, $charset); diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index 0e930c57df3..887097333c5 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -17,19 +17,102 @@ */ class Iconv extends AbstractStringWrapper { - /** * List of supported character sets (upper case) * * @var string[] - * @link http://php.net/manual/mbstring.supported-encodings.php + * @link http://www.gnu.org/software/libiconv/ */ protected $charsets = array( - 'ASCII', '7BIT', '8BIT', - 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', - 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', 'ISO-8859-10', - 'ISO-8859-11', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16', - 'UTF-8', // TODO + // European languages + 'ASCII', + 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', 'ISO-8859-7', + 'ISO-8859-9', 'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16', + 'KOI8-R', 'KOI8-U', 'KOI8-RU', + 'CP1250', 'CP1251', 'CP1252', 'CP1253', 'CP1254' ,'CP1257', + 'CP850', 'CP866', 'CP1131', + 'MACROMAN', 'MACCENTRALEUROPE', 'MACICELAND', 'MACCROATIAN', 'MACROMANIA', + 'MACCYRILLIC', 'MACUKRAINE', 'MACGREEK', 'MACTURKISH', + 'MACINTOSH', + + // Semitic languages + 'ISO-8859-6', 'ISO-8859-8', + 'CP1255' , 'CP1256', 'CP862', + 'MACHEBREW', 'MACARABIC', + + // Japanese + 'EUC-JP', 'SHIFT_JIS', 'CP932', 'ISO-2022-JP', 'ISO-2022-JP-2', 'ISO-2022-JP-1', + + // Chinese + 'EUC-CN', 'HZ', 'GBK', 'CP936', 'GB18030', 'EUC-TW', 'BIG5', 'CP950', + 'BIG5-HKSCS', 'BIG5-HKSCS:2004', 'BIG5-HKSCS:2001', 'BIG5-HKSCS:1999', + 'ISO-2022-CN', 'ISO-2022-CN-EXT', + + // Korean + 'EUC-KR', 'CP949', 'ISO-2022-KR', 'JOHAB', + + // Armenian + 'ARMSCII-8', + + // Georgian + 'GEORGIAN-ACADEMY', 'GEORGIAN-PS', + + // Tajik + 'KOI8-T', + + // Kazakh + 'PT154', 'RK1048', + + // Thai + 'ISO-8859-11', 'TIS-620', 'CP874', 'MACTHAI', + + // Laotian + 'MULELAO-1', 'CP1133', + + // Vietnamese + 'VISCII', 'TCVN', 'CP1258', + + // Platform specifics + 'HP-ROMAN8', 'NEXTSTEP', + + // Full Unicode + 'UTF-8', + 'UCS-2', 'UCS-2BE', 'UCS-2LE', + 'UCS-4', 'UCS-4BE', 'UCS-4LE', + 'UTF-16', 'UTF-16BE', 'UTF-16LE', + 'UTF-32', 'UTF-32BE', 'UTF-32LE', + 'UTF-7', + 'C99', 'JAVA', + + // Full Unicode, in terms of uint16_t or uint32_t (with machine dependent endianness and alignment) + // 'UCS-2-INTERNAL', 'UCS-4-INTERNAL', + + // Locale dependent, in terms of `char' or `wchar_t' (with machine dependent endianness and alignment, + // and with OS and locale dependent semantics) + // 'char', 'wchar_t', + // '', // The empty encoding name is equivalent to "char": it denotes the locale dependent character encoding. + + // When configured with the option --enable-extra-encodings, + // it also provides support for a few extra encodings: + + // European languages + 'CP437', 'CP737', 'CP775', 'CP852', 'CP853', 'CP855', 'CP857', 'CP858', + 'CP860', 'CP861', 'CP863', 'CP865', 'CP869', 'CP1125', + + // Semitic languages + 'CP864', + + // Japanese + 'EUC-JISX0213', 'Shift_JISX0213', 'ISO-2022-JP-3', + + // Chinese + 'BIG5-2003', // (experimental) + + // Turkmen + 'TDS565', + + // Platform specifics + 'ATARIST', 'RISCOS-LATIN1', ); /** diff --git a/library/Zend/Stdlib/StringWrapper/Intl.php b/library/Zend/Stdlib/StringWrapper/Intl.php index b6954015b15..71a9919d9fa 100644 --- a/library/Zend/Stdlib/StringWrapper/Intl.php +++ b/library/Zend/Stdlib/StringWrapper/Intl.php @@ -17,7 +17,6 @@ */ class Intl extends AbstractStringWrapper { - /** * List of supported character sets (upper case) * @@ -109,6 +108,6 @@ public function convert($str, $toCharset, $fromCharset = 'UTF-8') return false; } - return true; + return $str; } } diff --git a/library/Zend/Stdlib/StringWrapper/MbString.php b/library/Zend/Stdlib/StringWrapper/MbString.php index dfe2fbce464..60bb257b6b0 100644 --- a/library/Zend/Stdlib/StringWrapper/MbString.php +++ b/library/Zend/Stdlib/StringWrapper/MbString.php @@ -17,7 +17,6 @@ */ class MbString extends AbstractStringWrapper { - /** * List of supported character sets (upper case) * @@ -25,13 +24,81 @@ class MbString extends AbstractStringWrapper * @link http://php.net/manual/mbstring.supported-encodings.php */ protected $charsets = array( - 'ASCII', '7BIT', '8BIT', - 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', - 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', 'ISO-8859-10', - 'ISO-8859-11', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16', - 'UCS-4', 'UCS-4BE', 'UCS-4LE', - 'UCS-2', 'UCS-2BE', 'UCS-2LE', - 'UTF-8', // TODO + 'UCS-4', + 'UCS-4BE', + 'UCS-4LE', + 'UCS-2', + 'UCS-2BE', + 'UCS-2LE', + 'UTF-32', + 'UTF-32BE', + 'UTF-32LE', + 'UTF-16', + 'UTF-16BE', + 'UTF-16LE', + 'UTF-7', + 'UTF7-IMAP', + 'UTF-8', + 'ASCII', + 'EUC-JP', + 'SJIS', + 'EUCJP-WIN', + 'SJIS-WIN', + 'ISO-2022-JP', + 'ISO-2022-JP-MS', + 'CP932', + 'CP51932', + 'SJIS-MAC', 'MACJAPANESE', // ** + 'SJIS-Mobile#DOCOMO', 'SJIS-DOCOMO', // ** + 'SJIS-Mobile#KDDI', 'SJIS-KDDI', // ** + 'SJIS-Mobile#SOFTBANK', 'SJIS-SOFTBANK', // ** + 'UTF-8-Mobile#DOCOMO', 'UTF-8-DOCOMO', // ** + 'UTF-8-Mobile#KDDI-A', // ** + 'UTF-8-Mobile#KDDI-B', 'UTF-8-KDDI', // ** + 'UTF-8-Mobile#SOFTBANK', 'UTF-8-SOFTBANK', // ** + 'ISO-2022-JP-MOBILE#KDDI', 'ISO-2022-JP-KDDI', // ** + 'JIS', + 'JIS-MS', + 'CP50220', + 'CP50220RAW', + 'CP50221', + 'CP50222', + 'ISO-8859-1', + 'ISO-8859-2', + 'ISO-8859-3', + 'ISO-8859-4', + 'ISO-8859-5', + 'ISO-8859-6', + 'ISO-8859-7', + 'ISO-8859-8', + 'ISO-8859-9', + 'ISO-8859-10', + 'ISO-8859-13', + 'ISO-8859-14', + 'ISO-8859-15', + // 'ISO-8859-16', + 'bYTE2BE', + 'bYTE2LE', + 'BYTE4BE', + 'BYTE4LE', + 'BASE64', + 'HTML-ENTITIES', + '7BIT', + '8BIT', + 'EUC-CN', + 'CP936', + 'GB18030', // ** + 'HZ', + 'EUC-TW', + 'CP950', + 'BIG-5', + 'EUC-KR', + 'UHC', 'CP949', + 'ISO-2022-KR', + 'WINDOWS-1251', 'CP1251', + 'WINDOWS-1252', 'CP1252', + 'CP866', 'IBM866', + 'KOI8-R' ); /** @@ -46,6 +113,30 @@ public function __construct() 'PHP extension "mbstring" is required for this wrapper' ); } + + // remove charsets not available before PHP-5.4 + if (version_compare(PHP_VERSION, '5.4', '<')) { + unset( + $this->charsets['SJIS-MAC'], + $this->charsets['MACJAPANESE'], + $this->charsets['SJIS-Mobile#DOCOMO'], + $this->charsets['SJIS-DOCOMO'], + $this->charsets['SJIS-Mobile#KDDI'], + $this->charsets['SJIS-KDDI'], + $this->charsets['SJIS-Mobile#SOFTBANK'], + $this->charsets['SJIS-SOFTBANK'], + $this->charsets['UTF-8-Mobile#DOCOMO'], + $this->charsets['UTF-8-DOCOMO'], + $this->charsets['UTF-8-Mobile#KDDI-A'], + $this->charsets['UTF-8-Mobile#KDDI-B'], + $this->charsets['UTF-8-KDDI'], + $this->charsets['UTF-8-Mobile#SOFTBANK'], + $this->charsets['UTF-8-SOFTBANK'], + $this->charsets['ISO-2022-JP-MOBILE#KDDI'], + $this->charsets['ISO-2022-JP-KDDI'], + $this->charsets['GB18030'] + ); + } } /** diff --git a/library/Zend/Stdlib/StringWrapper/Native.php b/library/Zend/Stdlib/StringWrapper/Native.php index 7854fa93aa1..aee15b7368c 100644 --- a/library/Zend/Stdlib/StringWrapper/Native.php +++ b/library/Zend/Stdlib/StringWrapper/Native.php @@ -78,6 +78,11 @@ public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') */ public function convert($str, $toCharset, $fromCharset = 'UTF-8') { - return false; + if (strcasecmp($toCharset, $fromCharset) != 0) { + trigger_error("Can't convert '{$fromCharset}' to '{$toCharset}' using intl", E_WARNING); + return false; + } + + return $str; } } diff --git a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php new file mode 100644 index 00000000000..ef44b2aef1c --- /dev/null +++ b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php @@ -0,0 +1,302 @@ +stringWrapper instanceof StringWrapperInterface) ) { + $this->fail(sprintf( + "%s isn't an instance of %s", + get_class($this) . '::stringWrapper', + 'Zend\Stdlib\StringWrapper\StringWrapperInterface' + )); + } + } + + public function strlenProvider() + { + return array( + array('abcdefghijklmnopqrstuvwxyz', 'ascii', 26), + array('abcdefghijklmnopqrstuvwxyz', 'utf-8', 26), + array('äöüß', 'utf-8', 4), + ); + } + + /** + * @dataProvider strlenProvider + * @param string $string + * @param string $charset + * @param mixed $expected + */ + public function testStrlen($str, $charset, $expected) + { + if (!$this->stringWrapper->isCharsetSupported($charset)) { + $this->markTestSkipped( + "Charset {$charset} not supported by " . get_class($this->stringWrapper) + ); + } + + $result = $this->stringWrapper->strlen($str, $charset); + $this->assertSame($expected, $result); + } + + public function substrProvider() + { + return array( + array('abcdefghijkl', 1, 5, 'ascii', 'bcdef'), + array('abcdefghijkl', 1, 5, 'utf-8', 'bcdef'), + array('äöüß', 1, 2, 'utf-8', 'öü'), + ); + } + + /** + * @dataProvider substrProvider + * @param string $str + * @param int $offset + * @param int|null $length + * @param string $charset + * @param mixed $expected + */ + public function testSubstr($str, $offset, $length, $charset, $expected) + { + if (!$this->stringWrapper->isCharsetSupported($charset)) { + $this->markTestSkipped( + "Charset {$charset} not supported by " . get_class($this->stringWrapper) + ); + } + + $result = $this->stringWrapper->substr($str, $offset, $length, $charset); + $this->assertSame($expected, $result); + } + + public function strposProvider() + { + return array( + array('abcdefghijkl', 'g', 3, 'ascii', 6), + array('abcdefghijkl', 'g', 3, 'utf-8', 6), + array('äöüß', 'ü', 1, 'utf-8', 2), + ); + } + + /** + * @dataProvider strposProvider + * @param string $haystack + * @param string $needle + * @param int $offset + * @param string $charset + * @param mixed $expected + */ + public function testStrpos($haystack, $needle, $offset, $charset, $expected) + { + if (!$this->stringWrapper->isCharsetSupported($charset)) { + $this->markTestSkipped( + "Charset {$charset} not supported by " . get_class($this->stringWrapper) + ); + } + + $result = $this->stringWrapper->strpos($haystack, $needle, $offset, $charset); + $this->assertSame($expected, $result); + } + + public function convertProvider() + { + return array( + array('abc', 'ascii', 'ascii', 'abc'), + array('abc', 'utf-8', 'ascii', 'abc'), + array('abc', 'ascii', 'utf-8', 'abc'), + array('€', 'iso-8859-15', 'utf-8', "\xA4"), + array('€', 'iso-8859-16', 'utf-8', "\xA4"), // ISO-8859-16 is wrong @ mbstring + ); + } + + /** + * @dataProvider convertProvider + * @param string $str + * @param string $toCharset + * @param string $fromCharset + * @param mixed $expected + */ + public function testConvert($str, $toCharset, $fromCharset, $expected) + { + if (!$this->stringWrapper->isCharsetSupported($toCharset)) { + $this->markTestSkipped( + "Charset {$toCharset} not supported by " . get_class($this->stringWrapper) + ); + } elseif (!$this->stringWrapper->isCharsetSupported($fromCharset)) { + $this->markTestSkipped( + "Charset {$fromCharset} not supported by " . get_class($this->stringWrapper) + ); + } + + $result = $this->stringWrapper->convert($str, $toCharset, $fromCharset); + $this->assertSame($expected, $result); + } + + public function wordWrapProvider() + { + return array( + // Standard cut tests + array('äbüöcß', 2, ' ', true, 'utf-8', + 'äb üö cß'), + array('äbüöc ß äbüöcß', 2, ' ', true, 'utf-8', + 'äb üö c ß äb üö cß'), + array('Ä very long wöööööööööööörd.', 8, "\n", true, 'utf-8', + "Ä very\nlong\nwööööööö\nööööörd."), + array("Ä very\nlong wöööööööööööörd.", 8, "\n", false, 'utf-8', + "Ä very\nlong\nwöööööööööööörd."), + array("Ä very
long wöö
öööööööö
öörd.", 8, '
', false, 'utf-8', + "Ä very
long wöö
öööööööö
öörd."), + + // Alternative cut tests + array(' äüöäöü', 3, ' ', true, 'utf-8', + ' äüö äöü'), + array('äüöäöü ', 3, ' ', true, 'utf-8', + 'äüö äöü '), + array('äöüäöü ', 3, '-', true, 'utf-8', + 'äöü-äöü-'), + array('äüöäöü ', 3, ' ', true, 'utf-8', + 'äüö äöü '), + array('12345 ', 5, '-', false, 'utf-8', + '12345-'), + array('12345 ', 5, '-', false, 'utf-8', + '12345- '), + array('äüöäöü ', 3, ' ', true, 'utf-8', + 'äüö äöü '), + array('äüöäöü--', 3, '-', true, 'utf-8', + 'äüö-äöü--'), + array("äbü\töcß", 3, ' ', true, 'utf-8', + "äbü \töc ß"), + array("äbü\nößt", 3, ' ', true, 'utf-8', + "äbü \nöß t"), + array("äbü\nößte", 3, "\n", true, 'utf-8', + "äbü\nößt\ne"), + + // Break cut tests + array('foobar-foofoofoo', 8, '-', true, 'ascii', + 'foobar-foofoofo-o'), + array('foobar-foobar', 6, '-', true, 'ascii', + 'foobar-foobar'), + array('foobar-foobar', 7, '-', true, 'ascii', + 'foobar-foobar'), + array('foobar-', 7, '-', true, 'ascii', + 'foobar-'), + array('foobar-foobar', 5, '-', true, 'ascii', + 'fooba-r-fooba-r'), + + // Standard no-cut tests + array('äbüöcß', 2, ' ', false, 'utf-8', + 'äbüöcß'), + array('äbüöc ß äbüöcß', 2, "\n", false, 'utf-8', + "äbüöc\nß\näbüöcß"), + array('äöü äöü äöü', 5, "\n", false, 'utf-8', + "äöü\näöü\näöü"), + + // Break no-cut tests + array('foobar-foofoofoo', 8, '-', false, 'ascii', + 'foobar-foofoofoo'), + array('foobar-foobar', 6, '-', false, 'ascii', + 'foobar-foobar'), + array('foobar-foobar', 7, '-', false, 'ascii', + 'foobar-foobar'), + array('foobar-', 7, '-', false, 'ascii', + 'foobar-'), + array('foobar-foobar', 5, '-', false, 'ascii', + 'foobar-foobar'), + ); + } + + /** + * @dataProvider wordWrapProvider + * @param string $str + * @param integer $width + * @param string $break + * @param boolean $cut + * @param string $charset + * @param mixed $expected + */ + public function testWordWrap($string, $width, $break, $cut, $charset, $expected) + { + if (!$this->stringWrapper->isCharsetSupported($charset)) { + $this->markTestSkipped( + "Charset {$charset} not supported by " . get_class($this->stringWrapper) + ); + } + + $result = $this->stringWrapper->wordWrap($string, $width, $break, $cut, $charset); + $this->assertSame($expected, $result); + } + + public function testWordWrapInvalidArgument() + { + $this->setExpectedException( + 'Zend\Stdlib\Exception\InvalidArgumentException', + "Cannot force cut when width is zero" + ); + $this->stringWrapper->wordWrap('a', 0, "\n", true); + } + + public function strPadProvider() + { + return array( + // single-byte + array('aaa', 5, 'o', STR_PAD_LEFT, 'ascii', 'ooaaa'), + array('aaa', 6, 'o', STR_PAD_BOTH, 'ascii', 'oaaaoo'), + array('aaa', 5, 'o', STR_PAD_RIGHT, 'ascii', 'aaaoo'), + + // multi-byte + array('äää', 5, 'ö', STR_PAD_LEFT, 'utf-8', 'ööäää'), + array('äää', 6, 'ö', STR_PAD_BOTH, 'utf-8', 'öäääöö'), + array('äää', 5, 'ö', STR_PAD_RIGHT, 'utf-8', 'äääöö'), + + // ZF-12186 + array('äääöö', 2, 'ö', STR_PAD_RIGHT, 'utf-8', 'äääöö'), // PadInputLongerThanPadLength + array('äääöö', 5, 'ö', STR_PAD_RIGHT, 'utf-8', 'äääöö'), // PadInputSameAsPadLength + array('äääöö', -2, 'ö', STR_PAD_RIGHT, 'utf-8', 'äääöö'), // PadNegativePadLength + ); + } + + /** + * @dataProvider strPadProvider + * @param string $input + * @param integer $padLength + * @param string $padString + * @param integer $padType + * @param string $charset + * @param mixed $expected + * + * @group ZF-12186 + */ + public function testStrPad($input, $padLength, $padString, $padType, $charset, $expected) + { + if (!$this->stringWrapper->isCharsetSupported($charset)) { + $this->markTestSkipped( + "Charset {$charset} not supported by " . get_class($this->stringWrapper) + ); + } + + $result = $this->stringWrapper->strPad($input, $padLength, $padString, $padType, $charset); + $this->assertSame($expected, $result); + } +} diff --git a/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php b/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php new file mode 100644 index 00000000000..92a83e1c683 --- /dev/null +++ b/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php @@ -0,0 +1,34 @@ +fail('Missing expected Zend\Stdlib\Exception\ExtensionNotLoadedException'); + } catch (Exception\ExtensionNotLoadedException $e) { + $this->markTestSkipped('Missing ext/iconv'); + } + } + + $this->stringWrapper = new Iconv(); + parent::setUp(); + } +} diff --git a/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php b/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php new file mode 100644 index 00000000000..f764b3d98a9 --- /dev/null +++ b/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php @@ -0,0 +1,34 @@ +fail('Missing expected Zend\Stdlib\Exception\ExtensionNotLoadedException'); + } catch (Exception\ExtensionNotLoadedException $e) { + $this->markTestSkipped('Missing ext/intl'); + } + } + + $this->stringWrapper = new Intl(); + parent::setUp(); + } +} diff --git a/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php b/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php new file mode 100644 index 00000000000..bd6ee7aecde --- /dev/null +++ b/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php @@ -0,0 +1,34 @@ +fail('Missing expected Zend\Stdlib\Exception\ExtensionNotLoadedException'); + } catch (Exception\ExtensionNotLoadedException $e) { + $this->markTestSkipped('Missing ext/mbstring'); + } + } + + $this->stringWrapper = new MbString(); + parent::setUp(); + } +} diff --git a/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php b/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php new file mode 100644 index 00000000000..329a19ba390 --- /dev/null +++ b/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php @@ -0,0 +1,24 @@ +stringWrapper = new Native(); + parent::setUp(); + } +} From c0007fa0f7f80aee0e24d89b51f3ab94ffcc3d60 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 15:35:05 +0100 Subject: [PATCH 13/51] StringUtils: wording: charset -> encoding --- library/Zend/Stdlib/StringUtils.php | 36 +++++----- .../StringWrapper/AbstractStringWrapper.php | 62 ++++++++--------- library/Zend/Stdlib/StringWrapper/Iconv.php | 28 ++++---- library/Zend/Stdlib/StringWrapper/Intl.php | 36 +++++----- .../Zend/Stdlib/StringWrapper/MbString.php | 66 +++++++++---------- library/Zend/Stdlib/StringWrapper/Native.php | 24 +++---- .../StringWrapper/StringWrapperInterface.php | 36 +++++----- tests/ZendTest/Stdlib/StringUtilsTest.php | 20 +++--- .../StringWrapper/CommonStringWrapperTest.php | 66 +++++++++---------- 9 files changed, 187 insertions(+), 187 deletions(-) diff --git a/library/Zend/Stdlib/StringUtils.php b/library/Zend/Stdlib/StringUtils.php index a854d4ab17a..6aa3f793700 100644 --- a/library/Zend/Stdlib/StringUtils.php +++ b/library/Zend/Stdlib/StringUtils.php @@ -36,11 +36,11 @@ abstract class StringUtils protected static $wrapperRegistry; /** - * A list of known single-byte charsets (upper-case) + * A list of known single-byte encodings (upper-case) * * @var string[] */ - protected static $singleByteCharsets = array( + protected static $singleByteEncodings = array( 'ASCII', '7BIT', '8BIT', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', 'ISO-8859-10', @@ -106,20 +106,20 @@ public static function unregisterWrapper(StringWrapperInterface $wrapper) } /** - * Get the first string wrapper supporting one or more charsets + * Get the first string wrapper supporting one or more encodings * - * @param string $charset Charset supported by he string wrapper - * @param string $charsetN, ... Unlimited OPTIONAL number of additional charsets + * @param string $encoding Encoding supported by he string wrapper + * @param string $encodingN, ... Unlimited OPTIONAL number of additional encodings * @return StringWrapperInterface - * @throws Exception\RuntimeException If no wrapper supports all given charsets + * @throws Exception\RuntimeException If no wrapper supports all given encodings */ - public static function getWrapper($charset = 'UTF-8') + public static function getWrapper($encoding = 'UTF-8') { - $charsets = func_get_args(); + $$encodings = func_get_args(); foreach (static::getRegisteredWrappers() as $wrapper) { - foreach ($charsets as $charset) { - if (!$wrapper->isCharsetSupported($charset)) { + foreach ($encodings as $encoding) { + if (!$wrapper->isEncodingSupported($encoding)) { continue 2; } } @@ -128,29 +128,29 @@ public static function getWrapper($charset = 'UTF-8') } throw new Exception\RuntimeException( - 'No wrapper found supporting charset(s) ' . implode(', ', $charsets) + 'No wrapper found supporting encoding(s) ' . implode(', ', $encodings) ); } /** - * Get a list of all known single-byte charsets + * Get a list of all known single-byte encodings * * @return string[] */ - public static function getSingleByteCharsets() + public static function getSingleByteEncodings() { - return static::$singleByteCharsets; + return static::$singleByteEncodings; } /** - * Check if a given charset is a known single-byte charset + * Check if a given encoding is a known single-byte encoding * - * @param string $charset + * @param string $encoding * @return boolean */ - public static function isSingleByteCharset($charset) + public static function isSingleByteEncoding($encoding) { - return in_array(strtoupper($charset), static::$singleByteCharsets); + return in_array(strtoupper($encoding), static::$singleByteEncodings); } /** diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 07298335abc..1f0bd2bbba7 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -26,28 +26,28 @@ abstract class AbstractStringWrapper implements StringWrapperInterface * * @var string[] */ - protected $charsets = array(); + protected $encodings = array(); /** - * Check if the given charset is supported + * Check if the given encoding is supported * - * @param string $charset + * @param string $encoding * @return boolean */ - public function isCharsetSupported($charset) + public function isEncodingSupported($encoding) { - $charset = strtoupper($charset); - return in_array($charset, $this->charsets); + $encoding = strtoupper($encoding); + return in_array($encoding, $this->encodings); } /** - * Get a list of supported charsets + * Get a list of supported encodings * * @return string[] */ - public function getSupportedCharsets() + public function getSupportedEncodings() { - return $this->$charsets; + return $this->$encodings; } /** @@ -57,10 +57,10 @@ public function getSupportedCharsets() * @param integer $width * @param string $break * @param boolean $cut - * @param string $charset + * @param string $encoding * @return string */ - public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'UTF-8') + public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $encoding = 'UTF-8') { $string = (string) $string; if ($string === '') { @@ -78,27 +78,27 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $cha throw new Exception\InvalidArgumentException('Cannot force cut when width is zero'); } - $charset = strtoupper($charset); - if (StringUtils::isSingleByteCharset($charset)) { + $encoding = strtoupper($encoding); + if (StringUtils::isSingleByteEncoding($encoding)) { return wordwrap($string, $width, $break, $cut); } - $stringWidth = $this->strlen($string, $charset); - $breakWidth = $this->strlen($break, $charset); + $stringWidth = $this->strlen($string, $encoding); + $breakWidth = $this->strlen($break, $encoding); $result = ''; $lastStart = $lastSpace = 0; for ($current = 0; $current < $stringWidth; $current++) { - $char = $this->substr($string, $current, 1, $charset); + $char = $this->substr($string, $current, 1, $encoding); $possibleBreak = $char; if ($breakWidth !== 1) { - $possibleBreak = $this->substr($string, $current, $breakWidth, $charset); + $possibleBreak = $this->substr($string, $current, $breakWidth, $encoding); } if ($possibleBreak === $break) { - $result .= $this->substr($string, $lastStart, $current - $lastStart + $breakWidth, $charset); + $result .= $this->substr($string, $lastStart, $current - $lastStart + $breakWidth, $encoding); $current += $breakWidth - 1; $lastStart = $lastSpace = $current + 1; continue; @@ -106,7 +106,7 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $cha if ($char === ' ') { if ($current - $lastStart >= $width) { - $result .= $this->substr($string, $lastStart, $current - $lastStart, $charset) . $break; + $result .= $this->substr($string, $lastStart, $current - $lastStart, $encoding) . $break; $lastStart = $current + 1; } @@ -115,20 +115,20 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $cha } if ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) { - $result .= $this->substr($string, $lastStart, $current - $lastStart, $charset) . $break; + $result .= $this->substr($string, $lastStart, $current - $lastStart, $encoding) . $break; $lastStart = $lastSpace = $current; continue; } if ($current - $lastStart >= $width && $lastStart < $lastSpace) { - $result .= $this->substr($string, $lastStart, $lastSpace - $lastStart, $charset) . $break; + $result .= $this->substr($string, $lastStart, $lastSpace - $lastStart, $encoding) . $break; $lastStart = $lastSpace = $lastSpace + 1; continue; } } if ($lastStart !== $current) { - $result .= $this->substr($string, $lastStart, $current - $lastStart, $charset); + $result .= $this->substr($string, $lastStart, $current - $lastStart, $encoding); } return $result; @@ -141,19 +141,19 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $cha * @param integer $padLength * @param string $padString * @param integer $padType - * @param string $charset + * @param string $encoding * @return string */ - public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $charset = 'UTF-8') + public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $encoding = 'UTF-8') { - $charset = strtoupper($charset); - if (StringUtils::isSingleByteCharset($charset)) { + $encoding = strtoupper($encoding); + if (StringUtils::isSingleByteEncoding($encoding)) { return str_pad($input, $padLength, $padString, $padType); } $return = ''; - $lengthOfPadding = $padLength - $this->strlen($input, $charset); - $padStringLength = $this->strlen($padString, $charset); + $lengthOfPadding = $padLength - $this->strlen($input, $encoding); + $padStringLength = $this->strlen($padString, $encoding); if ($padStringLength === 0 || $lengthOfPadding <= 0) { $return = $input; @@ -169,14 +169,14 @@ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2); $lastStringRightLength += $lastStringLength % 2; - $lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength, $charset); - $lastStringRight = $this->substr($padString, 0, $lastStringRightLength, $charset); + $lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength, $encoding); + $lastStringRight = $this->substr($padString, 0, $lastStringRightLength, $encoding); $return = str_repeat($padString, $repeatCountLeft) . $lastStringLeft . $input . str_repeat($padString, $repeatCountRight) . $lastStringRight; } else { - $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength, $charset); + $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength, $encoding); if ($padType === \STR_PAD_LEFT) { $return = str_repeat($padString, $repeatCount) . $lastString . $input; diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index 887097333c5..01be679dae5 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -23,7 +23,7 @@ class Iconv extends AbstractStringWrapper * @var string[] * @link http://www.gnu.org/software/libiconv/ */ - protected $charsets = array( + protected $encodings = array( // European languages 'ASCII', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', 'ISO-8859-7', @@ -133,12 +133,12 @@ public function __construct() * Returns the length of the given string * * @param string $str - * @param string $charset + * @param string $encoding * @return int|false */ - public function strlen($str, $charset = 'UTF-8') + public function strlen($str, $encoding = 'UTF-8') { - return iconv_strlen($str, $charset); + return iconv_strlen($str, $encoding); } /** @@ -147,12 +147,12 @@ public function strlen($str, $charset = 'UTF-8') * @param string $str * @param int $offset * @param int|null $length - * @param string $charset + * @param string $encoding * @return string|false */ - public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') + public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') { - return iconv_substr($str, $offset, $length, $charset); + return iconv_substr($str, $offset, $length, $encoding); } /** @@ -161,24 +161,24 @@ public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') * @param string $haystack * @param string $needle * @param int $offset - * @param string $charset + * @param string $encoding * @return int|false */ - public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') + public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') { - return iconv_strpos($haystack, $needle, $offset, $charset); + return iconv_strpos($haystack, $needle, $offset, $encoding); } /** * Convert a string from one character encoding to another * * @param string $str - * @param string $toCharset - * @param string $fromCharset + * @param string $toEncoding + * @param string $fromEncoding * @return string|false */ - public function convert($str, $toCharset, $fromCharset = 'UTF-8') + public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') { - return iconv($fromCharset, $toCharset, $str); + return iconv($fromEncoding, $toEncoding, $str); } } diff --git a/library/Zend/Stdlib/StringWrapper/Intl.php b/library/Zend/Stdlib/StringWrapper/Intl.php index 71a9919d9fa..490ce768826 100644 --- a/library/Zend/Stdlib/StringWrapper/Intl.php +++ b/library/Zend/Stdlib/StringWrapper/Intl.php @@ -22,7 +22,7 @@ class Intl extends AbstractStringWrapper * * @var string[] */ - protected $charsets = array('UTF-8'); + protected $encodings = array('UTF-8'); /** * Constructor @@ -42,13 +42,13 @@ public function __construct() * Returns the length of the given string * * @param string $str - * @param string $charset + * @param string $encoding * @return int|false */ - public function strlen($str, $charset = 'UTF-8') + public function strlen($str, $encoding = 'UTF-8') { - if (strcasecmp($charset, 'UTF-8') != 0) { - trigger_error("Character set '{$charset}' not supported by intl"); + if (strcasecmp($encoding, 'UTF-8') != 0) { + trigger_error("Character set '{$encoding}' not supported by intl"); return false; } @@ -61,13 +61,13 @@ public function strlen($str, $charset = 'UTF-8') * @param string $str * @param int $offset * @param int|null $length - * @param string $charset + * @param string $encoding * @return string|false */ - public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') + public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') { - if (strcasecmp($charset, 'UTF-8') != 0) { - trigger_error("Character set '{$charset}' not supported by intl"); + if (strcasecmp($encoding, 'UTF-8') != 0) { + trigger_error("Character set '{$encoding}' not supported by intl"); return false; } @@ -80,13 +80,13 @@ public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') * @param string $haystack * @param string $needle * @param int $offset - * @param string $charset + * @param string $encoding * @return int|false */ - public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') + public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') { - if (strcasecmp($charset, 'UTF-8') != 0) { - trigger_error("Character set '{$charset}' not supported by intl"); + if (strcasecmp($encoding, 'UTF-8') != 0) { + trigger_error("Character set '{$encoding}' not supported by intl"); return false; } @@ -97,14 +97,14 @@ public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') * Convert a string from one character encoding to another * * @param string $str - * @param string $toCharset - * @param string $fromCharset + * @param string $toEncoding + * @param string $fromEncoding * @return string|false */ - public function convert($str, $toCharset, $fromCharset = 'UTF-8') + public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') { - if (strcasecmp($toCharset, $fromCharset) != 0) { - trigger_error("Can't convert '{$fromCharset}' to '{$toCharset}' using intl", E_WARNING); + if (strcasecmp($toEncoding, $fromEncoding) != 0) { + trigger_error("Can't convert '{$fromEncoding}' to '{$toEncoding}' using intl", E_WARNING); return false; } diff --git a/library/Zend/Stdlib/StringWrapper/MbString.php b/library/Zend/Stdlib/StringWrapper/MbString.php index 60bb257b6b0..46b5a88f5bd 100644 --- a/library/Zend/Stdlib/StringWrapper/MbString.php +++ b/library/Zend/Stdlib/StringWrapper/MbString.php @@ -23,7 +23,7 @@ class MbString extends AbstractStringWrapper * @var string[] * @link http://php.net/manual/mbstring.supported-encodings.php */ - protected $charsets = array( + protected $encodings = array( 'UCS-4', 'UCS-4BE', 'UCS-4LE', @@ -114,27 +114,27 @@ public function __construct() ); } - // remove charsets not available before PHP-5.4 + // remove encodings not available before PHP-5.4 if (version_compare(PHP_VERSION, '5.4', '<')) { unset( - $this->charsets['SJIS-MAC'], - $this->charsets['MACJAPANESE'], - $this->charsets['SJIS-Mobile#DOCOMO'], - $this->charsets['SJIS-DOCOMO'], - $this->charsets['SJIS-Mobile#KDDI'], - $this->charsets['SJIS-KDDI'], - $this->charsets['SJIS-Mobile#SOFTBANK'], - $this->charsets['SJIS-SOFTBANK'], - $this->charsets['UTF-8-Mobile#DOCOMO'], - $this->charsets['UTF-8-DOCOMO'], - $this->charsets['UTF-8-Mobile#KDDI-A'], - $this->charsets['UTF-8-Mobile#KDDI-B'], - $this->charsets['UTF-8-KDDI'], - $this->charsets['UTF-8-Mobile#SOFTBANK'], - $this->charsets['UTF-8-SOFTBANK'], - $this->charsets['ISO-2022-JP-MOBILE#KDDI'], - $this->charsets['ISO-2022-JP-KDDI'], - $this->charsets['GB18030'] + $this->encodings['SJIS-MAC'], + $this->encodings['MACJAPANESE'], + $this->encodings['SJIS-Mobile#DOCOMO'], + $this->encodings['SJIS-DOCOMO'], + $this->encodings['SJIS-Mobile#KDDI'], + $this->encodings['SJIS-KDDI'], + $this->encodings['SJIS-Mobile#SOFTBANK'], + $this->encodings['SJIS-SOFTBANK'], + $this->encodings['UTF-8-Mobile#DOCOMO'], + $this->encodings['UTF-8-DOCOMO'], + $this->encodings['UTF-8-Mobile#KDDI-A'], + $this->encodings['UTF-8-Mobile#KDDI-B'], + $this->encodings['UTF-8-KDDI'], + $this->encodings['UTF-8-Mobile#SOFTBANK'], + $this->encodings['UTF-8-SOFTBANK'], + $this->encodings['ISO-2022-JP-MOBILE#KDDI'], + $this->encodings['ISO-2022-JP-KDDI'], + $this->encodings['GB18030'] ); } } @@ -143,12 +143,12 @@ public function __construct() * Returns the length of the given string * * @param string $str - * @param string $charset + * @param string $encoding * @return int|false */ - public function strlen($str, $charset = 'UTF-8') + public function strlen($str, $encoding = 'UTF-8') { - return mb_strlen($str, $charset); + return mb_strlen($str, $encoding); } /** @@ -157,12 +157,12 @@ public function strlen($str, $charset = 'UTF-8') * @param string $str * @param int $offset * @param int|null $length - * @param string $charset + * @param string $encoding * @return string|false */ - public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') + public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') { - return mb_substr($str, $offset, $length, $charset); + return mb_substr($str, $offset, $length, $encoding); } /** @@ -171,24 +171,24 @@ public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') * @param string $haystack * @param string $needle * @param int $offset - * @param string $charset + * @param string $encoding * @return int|false */ - public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') + public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') { - return mb_strpos($haystack, $needle, $offset, $charset); + return mb_strpos($haystack, $needle, $offset, $encoding); } /** * Convert a string from one character encoding to another * * @param string $str - * @param string $toCharset - * @param string $fromCharset + * @param string $toEncoding + * @param string $fromEncoding * @return string|false */ - public function convert($str, $toCharset, $fromCharset = 'UTF-8') + public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') { - return mb_convert_encoding($str, $toCharset, $fromCharset); + return mb_convert_encoding($str, $toEncoding, $fromEncoding); } } diff --git a/library/Zend/Stdlib/StringWrapper/Native.php b/library/Zend/Stdlib/StringWrapper/Native.php index aee15b7368c..ed0bf48f6a1 100644 --- a/library/Zend/Stdlib/StringWrapper/Native.php +++ b/library/Zend/Stdlib/StringWrapper/Native.php @@ -25,17 +25,17 @@ class Native extends AbstractStringWrapper */ public function __construct() { - $this->charsets = StringUtils::getSingleByteCharsets(); + $this->encodings = StringUtils::getSingleByteEncodings(); } /** * Returns the length of the given string * * @param string $str - * @param string $charset + * @param string $encoding * @return int|false */ - public function strlen($str, $charset = 'UTF-8') + public function strlen($str, $encoding = 'UTF-8') { return strlen($str); } @@ -46,10 +46,10 @@ public function strlen($str, $charset = 'UTF-8') * @param string $str * @param int $offset * @param int|null $length - * @param string $charset + * @param string $encoding * @return string|false */ - public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') + public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') { return substr($str, $offset, $length); } @@ -60,10 +60,10 @@ public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8') * @param string $haystack * @param string $needle * @param int $offset - * @param string $charset + * @param string $encoding * @return int|false */ - public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') + public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') { return strpos($haystack, $needle, $offset); } @@ -72,14 +72,14 @@ public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8') * Convert a string from one character encoding to another * * @param string $str - * @param string $toCharset - * @param string $fromCharset + * @param string $toEncoding + * @param string $fromEncoding * @return string|false */ - public function convert($str, $toCharset, $fromCharset = 'UTF-8') + public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') { - if (strcasecmp($toCharset, $fromCharset) != 0) { - trigger_error("Can't convert '{$fromCharset}' to '{$toCharset}' using intl", E_WARNING); + if (strcasecmp($toEncoding, $fromEncoding) != 0) { + trigger_error("Can't convert '{$fromEncoding}' to '{$toEncoding}' using intl", E_WARNING); return false; } diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index f78da2e4da1..3929fb696eb 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -19,28 +19,28 @@ interface StringWrapperInterface { /** - * Check if the given charset is supported + * Check if the given encoding is supported * - * @param string $charset + * @param string $encoding * @return boolean */ - public function isCharsetSupported($charset); + public function isEncodingSupported($encoding); /** - * Get a list of supported charsets + * Get a list of supported encodings * * @return string[] */ - public function getSupportedCharsets(); + public function getSupportedEncodings(); /** * Returns the length of the given string * * @param string $str - * @param string $charset + * @param string $encoding * @return int|false */ - public function strlen($str, $charset = 'UTF-8'); + public function strlen($str, $encoding = 'UTF-8'); /** * Returns the portion of string specified by the start and length parameters @@ -48,10 +48,10 @@ public function strlen($str, $charset = 'UTF-8'); * @param string $str * @param int $offset * @param int|null $length - * @param string $charset + * @param string $encoding * @return string|false */ - public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8'); + public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8'); /** * Find the position of the first occurrence of a substring in a string @@ -59,20 +59,20 @@ public function substr($str, $offset = 0, $length = null, $charset = 'UTF-8'); * @param string $haystack * @param string $needle * @param int $offset - * @param string $charset + * @param string $encoding * @return int|false */ - public function strpos($haystack, $needle, $offset = 0, $charset = 'UTF-8'); + public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8'); /** * Convert a string from one character encoding to another * * @param string $str - * @param string $toCharset - * @param string $fromCharset + * @param string $toEncoding + * @param string $fromEncoding * @return string|false */ - public function convert($str, $toCharset, $fromCharset = 'UTF-8'); + public function convert($str, $toEncoding, $fromEncoding = 'UTF-8'); /** * Wraps a string to a given number of characters @@ -81,10 +81,10 @@ public function convert($str, $toCharset, $fromCharset = 'UTF-8'); * @param integer $width * @param string $break * @param boolean $cut - * @param string $charset + * @param string $encoding * @return string */ - public function wordWrap($str, $width = 75, $break = "\n", $cut = false, $charset = 'UTF-8'); + public function wordWrap($str, $width = 75, $break = "\n", $cut = false, $encoding = 'UTF-8'); /** * Pad a string to a certain length with another string @@ -93,8 +93,8 @@ public function wordWrap($str, $width = 75, $break = "\n", $cut = false, $charse * @param integer $padLength * @param string $padString * @param integer $padType - * @param string $charset + * @param string $encoding * @return string */ - public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $charset = 'UTF-8'); + public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $encoding = 'UTF-8'); } diff --git a/tests/ZendTest/Stdlib/StringUtilsTest.php b/tests/ZendTest/Stdlib/StringUtilsTest.php index 94fcdd89578..156431dde06 100644 --- a/tests/ZendTest/Stdlib/StringUtilsTest.php +++ b/tests/ZendTest/Stdlib/StringUtilsTest.php @@ -35,7 +35,7 @@ public function tearDown() } - public function getSingleByCharsets() + public function getSingleByEncodings() { return array( array('AscII'), @@ -60,15 +60,15 @@ public function getSingleByCharsets() } /** - * @dataProvider getSingleByCharsets - * @param string $charset + * @dataProvider getSingleByEncodings + * @param string $encoding */ - public function testIsSingleByteCharsetReturnsTrue($charset) + public function testIsSingleByteEncodingReturnsTrue($encoding) { - $this->assertTrue(StringUtils::isSingleByteCharset($charset)); + $this->assertTrue(StringUtils::isSingleByteEncoding($encoding)); } - public function getNonSingleByteCharsets() + public function getNonSingleByteEncodings() { return array( array('UTf-8'), @@ -79,12 +79,12 @@ public function getNonSingleByteCharsets() } /** - * @dataProvider getNonSingleByteCharsets - * @param string $charset + * @dataProvider getNonSingleByteEncodings + * @param string $encoding */ - public function testIsSingleByteCharsetReturnsFalse($charset) + public function testIsSingleByteEncodingReturnsFalse($encoding) { - $this->assertFalse(StringUtils::isSingleByteCharset($charset)); + $this->assertFalse(StringUtils::isSingleByteEncoding($encoding)); } public function testGetWrapper() diff --git a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php index ef44b2aef1c..4f606a1ae3e 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php @@ -47,18 +47,18 @@ public function strlenProvider() /** * @dataProvider strlenProvider * @param string $string - * @param string $charset + * @param string $encoding * @param mixed $expected */ - public function testStrlen($str, $charset, $expected) + public function testStrlen($str, $encoding, $expected) { - if (!$this->stringWrapper->isCharsetSupported($charset)) { + if (!$this->stringWrapper->isEncodingSupported($encoding)) { $this->markTestSkipped( - "Charset {$charset} not supported by " . get_class($this->stringWrapper) + "Encoding {$encoding} not supported by " . get_class($this->stringWrapper) ); } - $result = $this->stringWrapper->strlen($str, $charset); + $result = $this->stringWrapper->strlen($str, $encoding); $this->assertSame($expected, $result); } @@ -76,18 +76,18 @@ public function substrProvider() * @param string $str * @param int $offset * @param int|null $length - * @param string $charset + * @param string $encoding * @param mixed $expected */ - public function testSubstr($str, $offset, $length, $charset, $expected) + public function testSubstr($str, $offset, $length, $encoding, $expected) { - if (!$this->stringWrapper->isCharsetSupported($charset)) { + if (!$this->stringWrapper->isEncodingSupported($encoding)) { $this->markTestSkipped( - "Charset {$charset} not supported by " . get_class($this->stringWrapper) + "Encoding {$encoding} not supported by " . get_class($this->stringWrapper) ); } - $result = $this->stringWrapper->substr($str, $offset, $length, $charset); + $result = $this->stringWrapper->substr($str, $offset, $length, $encoding); $this->assertSame($expected, $result); } @@ -105,18 +105,18 @@ public function strposProvider() * @param string $haystack * @param string $needle * @param int $offset - * @param string $charset + * @param string $encoding * @param mixed $expected */ - public function testStrpos($haystack, $needle, $offset, $charset, $expected) + public function testStrpos($haystack, $needle, $offset, $encoding, $expected) { - if (!$this->stringWrapper->isCharsetSupported($charset)) { + if (!$this->stringWrapper->isEncodingSupported($encoding)) { $this->markTestSkipped( - "Charset {$charset} not supported by " . get_class($this->stringWrapper) + "Encoding {$encoding} not supported by " . get_class($this->stringWrapper) ); } - $result = $this->stringWrapper->strpos($haystack, $needle, $offset, $charset); + $result = $this->stringWrapper->strpos($haystack, $needle, $offset, $encoding); $this->assertSame($expected, $result); } @@ -134,23 +134,23 @@ public function convertProvider() /** * @dataProvider convertProvider * @param string $str - * @param string $toCharset - * @param string $fromCharset + * @param string $toEncoding + * @param string $fromEncoding * @param mixed $expected */ - public function testConvert($str, $toCharset, $fromCharset, $expected) + public function testConvert($str, $toEncoding, $fromEncoding, $expected) { - if (!$this->stringWrapper->isCharsetSupported($toCharset)) { + if (!$this->stringWrapper->isEncodingSupported($toEncoding)) { $this->markTestSkipped( - "Charset {$toCharset} not supported by " . get_class($this->stringWrapper) + "Encoding {$toEncoding} not supported by " . get_class($this->stringWrapper) ); - } elseif (!$this->stringWrapper->isCharsetSupported($fromCharset)) { + } elseif (!$this->stringWrapper->isEncodingSupported($fromEncoding)) { $this->markTestSkipped( - "Charset {$fromCharset} not supported by " . get_class($this->stringWrapper) + "Encoding {$fromEncoding} not supported by " . get_class($this->stringWrapper) ); } - $result = $this->stringWrapper->convert($str, $toCharset, $fromCharset); + $result = $this->stringWrapper->convert($str, $toEncoding, $fromEncoding); $this->assertSame($expected, $result); } @@ -233,18 +233,18 @@ public function wordWrapProvider() * @param integer $width * @param string $break * @param boolean $cut - * @param string $charset + * @param string $encoding * @param mixed $expected */ - public function testWordWrap($string, $width, $break, $cut, $charset, $expected) + public function testWordWrap($string, $width, $break, $cut, $encoding, $expected) { - if (!$this->stringWrapper->isCharsetSupported($charset)) { + if (!$this->stringWrapper->isEncodingSupported($encoding)) { $this->markTestSkipped( - "Charset {$charset} not supported by " . get_class($this->stringWrapper) + "Encoding {$encoding} not supported by " . get_class($this->stringWrapper) ); } - $result = $this->stringWrapper->wordWrap($string, $width, $break, $cut, $charset); + $result = $this->stringWrapper->wordWrap($string, $width, $break, $cut, $encoding); $this->assertSame($expected, $result); } @@ -283,20 +283,20 @@ public function strPadProvider() * @param integer $padLength * @param string $padString * @param integer $padType - * @param string $charset + * @param string $encoding * @param mixed $expected * * @group ZF-12186 */ - public function testStrPad($input, $padLength, $padString, $padType, $charset, $expected) + public function testStrPad($input, $padLength, $padString, $padType, $encoding, $expected) { - if (!$this->stringWrapper->isCharsetSupported($charset)) { + if (!$this->stringWrapper->isEncodingSupported($encoding)) { $this->markTestSkipped( - "Charset {$charset} not supported by " . get_class($this->stringWrapper) + "Encoding {$encoding} not supported by " . get_class($this->stringWrapper) ); } - $result = $this->stringWrapper->strPad($input, $padLength, $padString, $padType, $charset); + $result = $this->stringWrapper->strPad($input, $padLength, $padString, $padType, $encoding); $this->assertSame($expected, $result); } } From 54c413024ea31177172e8a3091a270c8787d09af Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 15:44:02 +0100 Subject: [PATCH 14/51] fixed wrong typed variable in StringUtils::getWrapper --- library/Zend/Stdlib/StringUtils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Zend/Stdlib/StringUtils.php b/library/Zend/Stdlib/StringUtils.php index 6aa3f793700..4714a47fc48 100644 --- a/library/Zend/Stdlib/StringUtils.php +++ b/library/Zend/Stdlib/StringUtils.php @@ -115,7 +115,7 @@ public static function unregisterWrapper(StringWrapperInterface $wrapper) */ public static function getWrapper($encoding = 'UTF-8') { - $$encodings = func_get_args(); + $encodings = func_get_args(); foreach (static::getRegisteredWrappers() as $wrapper) { foreach ($encodings as $encoding) { From aaab06b12e918102c74c5eef425194e726d81a4c Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 16:20:14 +0100 Subject: [PATCH 15/51] StringUtils: cs --- library/Zend/Stdlib/StringUtils.php | 7 +++--- library/Zend/Stdlib/StringWrapper/Iconv.php | 4 ++-- library/Zend/Stdlib/StringWrapper/Intl.php | 2 +- .../Zend/Stdlib/StringWrapper/MbString.php | 24 +++++++++---------- library/Zend/Stdlib/StringWrapper/Native.php | 2 +- .../StringWrapper/StringWrapperInterface.php | 2 +- 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/library/Zend/Stdlib/StringUtils.php b/library/Zend/Stdlib/StringUtils.php index 4714a47fc48..040ce301f8a 100644 --- a/library/Zend/Stdlib/StringUtils.php +++ b/library/Zend/Stdlib/StringUtils.php @@ -30,14 +30,14 @@ abstract class StringUtils /** * Ordered list of registered string wrapper instances - * + * * @var StringWrapperInterface[] */ protected static $wrapperRegistry; /** * A list of known single-byte encodings (upper-case) - * + * * @var string[] */ protected static $singleByteEncodings = array( @@ -45,9 +45,8 @@ abstract class StringUtils 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', 'ISO-8859-10', 'ISO-8859-11', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16', - 'CP-1251', 'CP-1252' + 'CP-1251', 'CP-1252', // TODO - ); /** diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index 01be679dae5..3de069ed0f3 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -112,7 +112,7 @@ class Iconv extends AbstractStringWrapper 'TDS565', // Platform specifics - 'ATARIST', 'RISCOS-LATIN1', + 'ATARIST', 'RISCOS-LATIN1', ); /** @@ -143,7 +143,7 @@ public function strlen($str, $encoding = 'UTF-8') /** * Returns the portion of string specified by the start and length parameters - * + * * @param string $str * @param int $offset * @param int|null $length diff --git a/library/Zend/Stdlib/StringWrapper/Intl.php b/library/Zend/Stdlib/StringWrapper/Intl.php index 490ce768826..0a0d2cac8f0 100644 --- a/library/Zend/Stdlib/StringWrapper/Intl.php +++ b/library/Zend/Stdlib/StringWrapper/Intl.php @@ -57,7 +57,7 @@ public function strlen($str, $encoding = 'UTF-8') /** * Returns the portion of string specified by the start and length parameters - * + * * @param string $str * @param int $offset * @param int|null $length diff --git a/library/Zend/Stdlib/StringWrapper/MbString.php b/library/Zend/Stdlib/StringWrapper/MbString.php index 46b5a88f5bd..8331ba7894c 100644 --- a/library/Zend/Stdlib/StringWrapper/MbString.php +++ b/library/Zend/Stdlib/StringWrapper/MbString.php @@ -48,15 +48,15 @@ class MbString extends AbstractStringWrapper 'ISO-2022-JP-MS', 'CP932', 'CP51932', - 'SJIS-MAC', 'MACJAPANESE', // ** - 'SJIS-Mobile#DOCOMO', 'SJIS-DOCOMO', // ** - 'SJIS-Mobile#KDDI', 'SJIS-KDDI', // ** - 'SJIS-Mobile#SOFTBANK', 'SJIS-SOFTBANK', // ** - 'UTF-8-Mobile#DOCOMO', 'UTF-8-DOCOMO', // ** - 'UTF-8-Mobile#KDDI-A', // ** - 'UTF-8-Mobile#KDDI-B', 'UTF-8-KDDI', // ** - 'UTF-8-Mobile#SOFTBANK', 'UTF-8-SOFTBANK', // ** - 'ISO-2022-JP-MOBILE#KDDI', 'ISO-2022-JP-KDDI', // ** + 'SJIS-MAC', 'MACJAPANESE', + 'SJIS-Mobile#DOCOMO', 'SJIS-DOCOMO', + 'SJIS-Mobile#KDDI', 'SJIS-KDDI', + 'SJIS-Mobile#SOFTBANK', 'SJIS-SOFTBANK', + 'UTF-8-Mobile#DOCOMO', 'UTF-8-DOCOMO', + 'UTF-8-Mobile#KDDI-A', + 'UTF-8-Mobile#KDDI-B', 'UTF-8-KDDI', + 'UTF-8-Mobile#SOFTBANK', 'UTF-8-SOFTBANK', + 'ISO-2022-JP-MOBILE#KDDI', 'ISO-2022-JP-KDDI', 'JIS', 'JIS-MS', 'CP50220', @@ -87,7 +87,7 @@ class MbString extends AbstractStringWrapper '8BIT', 'EUC-CN', 'CP936', - 'GB18030', // ** + 'GB18030', 'HZ', 'EUC-TW', 'CP950', @@ -98,7 +98,7 @@ class MbString extends AbstractStringWrapper 'WINDOWS-1251', 'CP1251', 'WINDOWS-1252', 'CP1252', 'CP866', 'IBM866', - 'KOI8-R' + 'KOI8-R', ); /** @@ -153,7 +153,7 @@ public function strlen($str, $encoding = 'UTF-8') /** * Returns the portion of string specified by the start and length parameters - * + * * @param string $str * @param int $offset * @param int|null $length diff --git a/library/Zend/Stdlib/StringWrapper/Native.php b/library/Zend/Stdlib/StringWrapper/Native.php index ed0bf48f6a1..07601ba529e 100644 --- a/library/Zend/Stdlib/StringWrapper/Native.php +++ b/library/Zend/Stdlib/StringWrapper/Native.php @@ -42,7 +42,7 @@ public function strlen($str, $encoding = 'UTF-8') /** * Returns the portion of string specified by the start and length parameters - * + * * @param string $str * @param int $offset * @param int|null $length diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index 3929fb696eb..c46570383bc 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -44,7 +44,7 @@ public function strlen($str, $encoding = 'UTF-8'); /** * Returns the portion of string specified by the start and length parameters - * + * * @param string $str * @param int $offset * @param int|null $length From 625aa283ea213cafcc13bf42dd275ef2b69647f9 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 16:51:40 +0100 Subject: [PATCH 16/51] StringUtils: implemented basic functionality into AbstractStringWrapper::convert --- .../StringWrapper/AbstractStringWrapper.php | 18 +++++++++++++++++ library/Zend/Stdlib/StringWrapper/Intl.php | 18 ----------------- library/Zend/Stdlib/StringWrapper/Native.php | 20 +------------------ 3 files changed, 19 insertions(+), 37 deletions(-) diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 1f0bd2bbba7..b8fc2b61b19 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -50,6 +50,24 @@ public function getSupportedEncodings() return $this->$encodings; } + /** + * Convert a string from one character encoding to another + * + * @param string $str + * @param string $toEncoding + * @param string $fromEncoding + * @return string|false + */ + public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') + { + if (strcasecmp($toEncoding, $fromEncoding) != 0) { + trigger_error("Can't convert '{$fromEncoding}' to '{$toEncoding}'", E_WARNING); + return false; + } + + return $str; + } + /** * Wraps a string to a given number of characters * diff --git a/library/Zend/Stdlib/StringWrapper/Intl.php b/library/Zend/Stdlib/StringWrapper/Intl.php index 0a0d2cac8f0..e9c9ae9f502 100644 --- a/library/Zend/Stdlib/StringWrapper/Intl.php +++ b/library/Zend/Stdlib/StringWrapper/Intl.php @@ -92,22 +92,4 @@ public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') return grapheme_strpos($haystack, $needle, $offset); } - - /** - * Convert a string from one character encoding to another - * - * @param string $str - * @param string $toEncoding - * @param string $fromEncoding - * @return string|false - */ - public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') - { - if (strcasecmp($toEncoding, $fromEncoding) != 0) { - trigger_error("Can't convert '{$fromEncoding}' to '{$toEncoding}' using intl", E_WARNING); - return false; - } - - return $str; - } } diff --git a/library/Zend/Stdlib/StringWrapper/Native.php b/library/Zend/Stdlib/StringWrapper/Native.php index 07601ba529e..42b8f4ae571 100644 --- a/library/Zend/Stdlib/StringWrapper/Native.php +++ b/library/Zend/Stdlib/StringWrapper/Native.php @@ -67,22 +67,4 @@ public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') { return strpos($haystack, $needle, $offset); } - - /** - * Convert a string from one character encoding to another - * - * @param string $str - * @param string $toEncoding - * @param string $fromEncoding - * @return string|false - */ - public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') - { - if (strcasecmp($toEncoding, $fromEncoding) != 0) { - trigger_error("Can't convert '{$fromEncoding}' to '{$toEncoding}' using intl", E_WARNING); - return false; - } - - return $str; - } -} +} From 57e129e63fee2e0aef9925f00c3e75df6596bdaa Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 17:37:30 +0100 Subject: [PATCH 17/51] StringUtils: hopefully a little better encoding list --- .../StringWrapper/AbstractStringWrapper.php | 33 ++++++++++++++----- library/Zend/Stdlib/StringWrapper/Iconv.php | 11 +++---- library/Zend/Stdlib/StringWrapper/Native.php | 18 ++++++++-- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index b8fc2b61b19..2ba6629663e 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -22,32 +22,31 @@ abstract class AbstractStringWrapper implements StringWrapperInterface { /** - * List of supported character sets (upper case) + * List of supported character encodings (upper case) * * @var string[] */ protected $encodings = array(); /** - * Check if the given encoding is supported + * Check if the given character encoding is supported * * @param string $encoding * @return boolean */ public function isEncodingSupported($encoding) { - $encoding = strtoupper($encoding); - return in_array($encoding, $this->encodings); + return in_array(strtoupper($encoding), $this->encodings); } /** - * Get a list of supported encodings + * Get a list of supported character encodings * * @return string[] */ public function getSupportedEncodings() { - return $this->$encodings; + return $this->encodings; } /** @@ -61,7 +60,7 @@ public function getSupportedEncodings() public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') { if (strcasecmp($toEncoding, $fromEncoding) != 0) { - trigger_error("Can't convert '{$fromEncoding}' to '{$toEncoding}'", E_WARNING); + trigger_error("Can't convert '{$fromEncoding}' to '{$toEncoding}'", \E_WARNING); return false; } @@ -78,7 +77,7 @@ public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') * @param string $encoding * @return string */ - public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $encoding = 'UTF-8') + public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $encoding = 'UTF-8') { $string = (string) $string; if ($string === '') { @@ -206,4 +205,22 @@ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD return $return; } + + /** + * Get the internal used name of an encoding + * + * @param string $encoding + * @return string + * @throws Exception\InvalidArgumentException On an unsupported encoding + */ + protected function getInternalEncoding($encoding) + { + $encodingUpper = strtoupper($encoding); + if (!isset($this->encodingMap[$encodingUpper])) { + throw new Exception\InvalidArgumentException( + "Character encoding '{$encoding}' isn't supported by this string wrapper" + ); + } + return $this->encodingMap[$encodingUpper]; + } } diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index 3de069ed0f3..3c97ad08332 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -29,19 +29,18 @@ class Iconv extends AbstractStringWrapper 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', 'ISO-8859-7', 'ISO-8859-9', 'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16', 'KOI8-R', 'KOI8-U', 'KOI8-RU', - 'CP1250', 'CP1251', 'CP1252', 'CP1253', 'CP1254' ,'CP1257', - 'CP850', 'CP866', 'CP1131', + 'CP1250', 'CP1251', 'CP1252', 'CP1253', 'CP1254', 'CP1257', 'CP850', 'CP866', 'CP1131', 'MACROMAN', 'MACCENTRALEUROPE', 'MACICELAND', 'MACCROATIAN', 'MACROMANIA', - 'MACCYRILLIC', 'MACUKRAINE', 'MACGREEK', 'MACTURKISH', - 'MACINTOSH', + 'MACCYRILLIC', 'MACUKRAINE', 'MACGREEK', 'MACTURKISH', 'MACINTOSH', // Semitic languages 'ISO-8859-6', 'ISO-8859-8', - 'CP1255' , 'CP1256', 'CP862', + 'CP1255', 'CP1256', 'CP862', 'MACHEBREW', 'MACARABIC', // Japanese - 'EUC-JP', 'SHIFT_JIS', 'CP932', 'ISO-2022-JP', 'ISO-2022-JP-2', 'ISO-2022-JP-1', + 'EUC-JP', 'SHIFT_JIS', 'CP932', + 'ISO-2022-JP', 'ISO-2022-JP-2', 'ISO-2022-JP-1', // Chinese 'EUC-CN', 'HZ', 'GBK', 'CP936', 'GB18030', 'EUC-TW', 'BIG5', 'CP950', diff --git a/library/Zend/Stdlib/StringWrapper/Native.php b/library/Zend/Stdlib/StringWrapper/Native.php index 42b8f4ae571..d7e13e9b7b1 100644 --- a/library/Zend/Stdlib/StringWrapper/Native.php +++ b/library/Zend/Stdlib/StringWrapper/Native.php @@ -19,13 +19,25 @@ */ class Native extends AbstractStringWrapper { + /** + * Check if the given encoding is supported + * + * @param string $encoding + * @return boolean + */ + public function isEncodingSupported($encoding) + { + return StringUtils::isSingleByteEncoding($encoding); + } /** - * Constructor + * Get a list of supported character encodings + * + * @return string[] */ - public function __construct() + public function getSupportedEncodings() { - $this->encodings = StringUtils::getSingleByteEncodings(); + return StringUtils::getSingleByteEncodings(); } /** From 23c52b4ad3cde63e54d1f10d29b30288f5b63d6e Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 17:41:10 +0100 Subject: [PATCH 18/51] StringUtils: hopefully a little better encoding list --- library/Zend/Stdlib/StringUtils.php | 14 +++++++------- .../StringWrapper/AbstractStringWrapper.php | 18 ------------------ .../StringWrapper/StringWrapperInterface.php | 4 ++-- 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/library/Zend/Stdlib/StringUtils.php b/library/Zend/Stdlib/StringUtils.php index 040ce301f8a..2b1b227e831 100644 --- a/library/Zend/Stdlib/StringUtils.php +++ b/library/Zend/Stdlib/StringUtils.php @@ -36,7 +36,7 @@ abstract class StringUtils protected static $wrapperRegistry; /** - * A list of known single-byte encodings (upper-case) + * A list of known single-byte character encodings (upper-case) * * @var string[] */ @@ -105,12 +105,12 @@ public static function unregisterWrapper(StringWrapperInterface $wrapper) } /** - * Get the first string wrapper supporting one or more encodings + * Get the first string wrapper supporting one or more character encodings * - * @param string $encoding Encoding supported by he string wrapper - * @param string $encodingN, ... Unlimited OPTIONAL number of additional encodings + * @param string $encoding Character encoding supported by he string wrapper + * @param string $encodingN, ... Unlimited OPTIONAL number of additional character encodings * @return StringWrapperInterface - * @throws Exception\RuntimeException If no wrapper supports all given encodings + * @throws Exception\RuntimeException If no wrapper supports all given character encodings */ public static function getWrapper($encoding = 'UTF-8') { @@ -132,7 +132,7 @@ public static function getWrapper($encoding = 'UTF-8') } /** - * Get a list of all known single-byte encodings + * Get a list of all known single-byte character encodings * * @return string[] */ @@ -142,7 +142,7 @@ public static function getSingleByteEncodings() } /** - * Check if a given encoding is a known single-byte encoding + * Check if a given encoding is a known single-byte character encoding * * @param string $encoding * @return boolean diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 2ba6629663e..ac329357c5e 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -205,22 +205,4 @@ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD return $return; } - - /** - * Get the internal used name of an encoding - * - * @param string $encoding - * @return string - * @throws Exception\InvalidArgumentException On an unsupported encoding - */ - protected function getInternalEncoding($encoding) - { - $encodingUpper = strtoupper($encoding); - if (!isset($this->encodingMap[$encodingUpper])) { - throw new Exception\InvalidArgumentException( - "Character encoding '{$encoding}' isn't supported by this string wrapper" - ); - } - return $this->encodingMap[$encodingUpper]; - } } diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index c46570383bc..f3f8ebe15e7 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -19,7 +19,7 @@ interface StringWrapperInterface { /** - * Check if the given encoding is supported + * Check if the given character encoding is supported * * @param string $encoding * @return boolean @@ -27,7 +27,7 @@ interface StringWrapperInterface public function isEncodingSupported($encoding); /** - * Get a list of supported encodings + * Get a list of supported character encodings * * @return string[] */ From 5f8c74158e17f513baec78caeaf64c053294b3ff Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 17:46:58 +0100 Subject: [PATCH 19/51] StringUtils: optimations --- .../StringWrapper/AbstractStringWrapper.php | 49 +++++++++---------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index ac329357c5e..bd9e7713fcf 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -75,7 +75,7 @@ public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') * @param string $break * @param boolean $cut * @param string $encoding - * @return string + * @return string|false */ public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $encoding = 'UTF-8') { @@ -168,41 +168,38 @@ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD return str_pad($input, $padLength, $padString, $padType); } - $return = ''; $lengthOfPadding = $padLength - $this->strlen($input, $encoding); $padStringLength = $this->strlen($padString, $encoding); if ($padStringLength === 0 || $lengthOfPadding <= 0) { - $return = $input; - } else { - $repeatCount = floor($lengthOfPadding / $padStringLength); + return $input; + } - if ($padType === \STR_PAD_BOTH) { - $lastStringLeft = ''; - $lastStringRight = ''; - $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2; + $repeatCount = floor($lengthOfPadding / $padStringLength); - $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength; - $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2); - $lastStringRightLength += $lastStringLength % 2; + if ($padType === \STR_PAD_BOTH) { + $lastStringLeft = ''; + $lastStringRight = ''; + $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2; - $lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength, $encoding); - $lastStringRight = $this->substr($padString, 0, $lastStringRightLength, $encoding); + $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength; + $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2); + $lastStringRightLength += $lastStringLength % 2; - $return = str_repeat($padString, $repeatCountLeft) . $lastStringLeft - . $input - . str_repeat($padString, $repeatCountRight) . $lastStringRight; - } else { - $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength, $encoding); + $lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength, $encoding); + $lastStringRight = $this->substr($padString, 0, $lastStringRightLength, $encoding); - if ($padType === \STR_PAD_LEFT) { - $return = str_repeat($padString, $repeatCount) . $lastString . $input; - } else { - $return = $input . str_repeat($padString, $repeatCount) . $lastString; - } + return str_repeat($padString, $repeatCountLeft) . $lastStringLeft + . $input + . str_repeat($padString, $repeatCountRight) . $lastStringRight; + } else { + $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength, $encoding); + + if ($padType === \STR_PAD_LEFT) { + return str_repeat($padString, $repeatCount) . $lastString . $input; + } else { + return $input . str_repeat($padString, $repeatCount) . $lastString; } } - - return $return; } } From a26f2df82b4b475e243f4923cc1887c572a7d048 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 19:49:28 +0100 Subject: [PATCH 20/51] StringUtils: cs --- library/Zend/Stdlib/StringWrapper/Native.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Zend/Stdlib/StringWrapper/Native.php b/library/Zend/Stdlib/StringWrapper/Native.php index d7e13e9b7b1..3aee7f13bf7 100644 --- a/library/Zend/Stdlib/StringWrapper/Native.php +++ b/library/Zend/Stdlib/StringWrapper/Native.php @@ -79,4 +79,4 @@ public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') { return strpos($haystack, $needle, $offset); } -} +} From 7d6e8de35ff8723285ee87707a7467b5a56a19d0 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 20:53:40 +0100 Subject: [PATCH 21/51] Updated Zend\Validator to used StringUtils --- library/Zend/Validator/Barcode/Code128.php | 65 +++++++++++++++++----- library/Zend/Validator/Hostname.php | 9 ++- library/Zend/Validator/StringLength.php | 24 +++----- 3 files changed, 64 insertions(+), 34 deletions(-) diff --git a/library/Zend/Validator/Barcode/Code128.php b/library/Zend/Validator/Barcode/Code128.php index e2951ff25f0..cbd4c2fb195 100644 --- a/library/Zend/Validator/Barcode/Code128.php +++ b/library/Zend/Validator/Barcode/Code128.php @@ -10,12 +10,22 @@ namespace Zend\Validator\Barcode; +use Zend\Stdlib\StringUtils; +use Zend\Stdlib\StringWrapper\StringWrapperInterface; + /** * @category Zend * @package Zend_Validator */ class Code128 extends AbstractAdapter { + /** + * The used string wrapper used for basic UTF-8 string functions + * + * @var SrtringWrapperInterface + */ + protected $utf8StringWrapper; + /** * Constructor for this barcode adapter */ @@ -72,6 +82,29 @@ public function __construct() } + public function setUtf8StringWrapper(StringWrapperInterface $utf8StringWrapper) + { + if (!$utf8StringWrapper->isEncodingSupported('UTF-8')) { + throw new Exception\InvalidArgumentException( + "The string wrapper needs to support UTF-8 character encoding" + ); + } + $this->utf8StringWrapper = $utf8StringWrapper; + } + + /** + * Get the string wrapper supporting UTF-8 character encoding + * + * @return StringWrapperInterface + */ + public function getUtf8StringWrapper() + { + if (!$this->utf8StringWrapper) { + $this->utf8StringWrapper = StringUtils::getWrapper('UTF-8'); + } + return $this->utf8StringWrapper; + } + /** * Checks for allowed characters within the barcode * @@ -84,16 +117,19 @@ public function hasValidCharacters($value) return false; } + // get used string wrapper for UTF-8 character encoding + $strWrapper = $this->getUtf8StringWrapper(); + // detect starting charset $set = $this->getCodingSet($value); $read = $set; if ($set != '') { - $value = iconv_substr($value, 1, iconv_strlen($value, 'UTF-8'), 'UTF-8'); + $value = $strWrapper->substr($value, 1, null, 'UTF-8'); } // process barcode while ($value != '') { - $char = iconv_substr($value, 0, 1, 'UTF-8'); + $char = $strWrapper->substr($value, 0, 1, 'UTF-8'); switch ($char) { // Function definition @@ -149,11 +185,11 @@ public function hasValidCharacters($value) break; } - $value = iconv_substr($value, 1); + $value = $strWrapper->substr($value, 1, null, 'UTF-8'); $read = $set; } - if (($value != '') && (iconv_strlen($value, 'UTF-8') != 1)) { + if (($value != '') && ($strWrapper->strlen($value, 'UTF-8') != 1)) { return false; } @@ -173,7 +209,8 @@ protected function code128($value) $set = $this->getCodingSet($value); $read = $set; $usecheck = $this->useChecksum(null); - $char = iconv_substr($value, 0, 1, 'UTF-8'); + $strWrapper = $this->getUtf8StringWrapper(); + $char = $strWrapper->substr($value, 0, 1, 'UTF-8'); if ($char == '‡') { $sum = 103; } elseif ($char == 'ˆ') { @@ -185,11 +222,11 @@ protected function code128($value) return false; } - $value = iconv_substr($value, 1, iconv_strlen($value, 'UTF-8'), 'UTF-8'); - while (iconv_strpos($value, 'Š') || ($value != '')) { - $char = iconv_substr($value, 0, 1, 'UTF-8'); + $value = $strWrapper->substr($value, 1, null, 'UTF-8'); + while ($strWrapper->strpos($value, 'Š', 0, 'UTF-8') || ($value != '')) { + $char = $strWrapper->substr($value, 0, 1, 'UTF-8'); if ($read == 'C') { - $char = iconv_substr($value, 0, 2, 'UTF-8'); + $char = $strWrapper->substr($value, 0, 2, 'UTF-8'); } switch ($char) { @@ -246,22 +283,22 @@ protected function code128($value) break; } - $value = iconv_substr($value, 1, iconv_strlen($value, 'UTF-8'), 'UTF-8'); + $value = $strWrapper->substr($value, 1, null, 'UTF-8'); ++$pos; - if ((iconv_strpos($value, 'Š', 0, 'UTF-8') == 1) && (iconv_strlen($value, 'UTF-8') == 2)) { + if (($strWrapper->strpos($value, 'Š', 0, 'UTF-8') == 1) && ($strWrapper->strlen($value, 'UTF-8') == 2)) { // break by stop and checksum char break; } $read = $set; } - if ((iconv_strpos($value, 'Š', 0, 'UTF-8') != 1) || (iconv_strlen($value, 'UTF-8') != 2)) { + if (($strWrapper->strpos($value, 'Š', 0, 'UTF-8') != 1) || ($strWrapper->strlen($value, 'UTF-8') != 2)) { // return false if checksum is not readable and true if no startvalue is detected return (!$usecheck); } $mod = $sum % 103; - if (iconv_substr($value, 0, 1, 'UTF-8') == $this->chr128($mod, $set)) { + if ($strWrapper->substr($value, 0, 1, 'UTF-8') == $this->chr128($mod, $set)) { return true; } @@ -276,7 +313,7 @@ protected function code128($value) */ protected function getCodingSet($value) { - $value = iconv_substr($value, 0, 1, 'UTF-8'); + $value = $this->getUtf8StringWrapper()->substr($value, 0, 1, 'UTF-8'); switch ($value) { case '‡' : return 'A'; diff --git a/library/Zend/Validator/Hostname.php b/library/Zend/Validator/Hostname.php index d780cd51b4b..6f2825d14a1 100644 --- a/library/Zend/Validator/Hostname.php +++ b/library/Zend/Validator/Hostname.php @@ -11,6 +11,7 @@ namespace Zend\Validator; use Zend\Stdlib\ErrorHandler; +use Zend\Stdlib\StringUtils; /** * Please note there are two standalone test scripts for testing IDN characters due to problems @@ -484,10 +485,9 @@ public function isValid($value) // Check input against DNS hostname schema if ((count($domainParts) > 1) && (strlen($value) >= 4) && (strlen($value) <= 254)) { - $status = false; + $utf8StrWrapper = StringUtils::getWrapper('UTF-8'); + $status = false; - $origenc = iconv_get_encoding('internal_encoding'); - iconv_set_encoding('internal_encoding', 'UTF-8'); do { // First check TLD $matches = array(); @@ -557,7 +557,7 @@ public function isValid($value) $length = $this->idnLength[strtoupper($this->tld)]; } - if (iconv_strlen($domainPart, 'UTF-8') > $length) { + if ($utf8StrWrapper->strlen($domainPart, 'UTF-8') > $length) { $this->error(self::INVALID_HOSTNAME); } else { $checked = true; @@ -583,7 +583,6 @@ public function isValid($value) } } while (false); - iconv_set_encoding('internal_encoding', $origenc); // If the input passes as an Internet domain name, and domain names are allowed, then the hostname // passes validation if ($status && ($this->getAllow() & self::ALLOW_DNS)) { diff --git a/library/Zend/Validator/StringLength.php b/library/Zend/Validator/StringLength.php index 1587c2458ea..99186704ea3 100644 --- a/library/Zend/Validator/StringLength.php +++ b/library/Zend/Validator/StringLength.php @@ -10,6 +10,8 @@ namespace Zend\Validator; +use Zend\Stdlib\StringUtils; + /** * @category Zend * @package Zend_Validator @@ -38,11 +40,13 @@ class StringLength extends AbstractValidator ); protected $options = array( - 'min' => 0, // Minimum length - 'max' => null, // Maximum length, null if there is no length limitation - 'encoding' => null, // Encoding to use + 'min' => 0, // Minimum length + 'max' => null, // Maximum length, null if there is no length limitation + 'encoding' => 'UTF-8', // Encoding to use ); + protected $stringWrapper; + /** * Sets validator options * @@ -146,13 +150,7 @@ public function getEncoding() public function setEncoding($encoding = null) { if ($encoding !== null) { - $orig = iconv_get_encoding('internal_encoding'); - $result = iconv_set_encoding('internal_encoding', $encoding); - if (!$result) { - throw new Exception\InvalidArgumentException('Given encoding not supported on this OS!'); - } - - iconv_set_encoding('internal_encoding', $orig); + StringUtils::getWrapper($encoding); } $this->options['encoding'] = $encoding; @@ -174,12 +172,8 @@ public function isValid($value) } $this->setValue($value); - if ($this->getEncoding() !== null) { - $length = iconv_strlen($value, $this->getEncoding()); - } else { - $length = iconv_strlen($value); - } + $length = StringUtils::getWrapper($this->getEncoding())->strlen($value, $this->getEncoding()); if ($length < $this->getMin()) { $this->error(self::TOO_SHORT); } From a8a4812b825974bc6c0ae75bad91cbb10b1c0b1d Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 21:00:53 +0100 Subject: [PATCH 22/51] Updated Zend\Mvc to used StringUtils --- library/Zend/Mvc/View/Console/RouteNotFoundStrategy.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/library/Zend/Mvc/View/Console/RouteNotFoundStrategy.php b/library/Zend/Mvc/View/Console/RouteNotFoundStrategy.php index e1b4e205608..afaa65b376f 100644 --- a/library/Zend/Mvc/View/Console/RouteNotFoundStrategy.php +++ b/library/Zend/Mvc/View/Console/RouteNotFoundStrategy.php @@ -26,6 +26,7 @@ use Zend\View\Model\ConsoleModel; use Zend\Version\Version; use Zend\Stdlib\ResponseInterface as Response; +use Zend\Stdlib\StringUtils; use Zend\Text\Table; /** @@ -389,6 +390,7 @@ protected function renderTable($data, $cols, $consoleWidth) $result = ''; $padding = 2; + // If there is only 1 column, just concatenate it if ($cols == 1) { foreach ($data as $row) { @@ -397,12 +399,15 @@ protected function renderTable($data, $cols, $consoleWidth) return $result; } + // Get the string wrapper supporting UTF-8 character encoding + $strWrapper = StringUtils::getWrapper('UTF-8'); + // Determine max width for each column $maxW = array(); for ($x = 1; $x <= $cols; $x += 1) { $maxW[$x] = 0; foreach ($data as $row) { - $maxW[$x] = max($maxW[$x], mb_strlen($row[$x-1],'utf-8') + $padding * 2); + $maxW[$x] = max($maxW[$x], $strWrapper->strlen($row[$x-1],'UTF-8') + $padding * 2); } } From efdea010cfe0b100a58e79ecf3e6c9b5e685f25b Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 21:09:12 +0100 Subject: [PATCH 23/51] StringUtils: MbString wrapper use of 'mb_list_encodings' --- .../Zend/Stdlib/StringWrapper/MbString.php | 102 +----------------- 1 file changed, 2 insertions(+), 100 deletions(-) diff --git a/library/Zend/Stdlib/StringWrapper/MbString.php b/library/Zend/Stdlib/StringWrapper/MbString.php index 8331ba7894c..a17241228f9 100644 --- a/library/Zend/Stdlib/StringWrapper/MbString.php +++ b/library/Zend/Stdlib/StringWrapper/MbString.php @@ -23,83 +23,7 @@ class MbString extends AbstractStringWrapper * @var string[] * @link http://php.net/manual/mbstring.supported-encodings.php */ - protected $encodings = array( - 'UCS-4', - 'UCS-4BE', - 'UCS-4LE', - 'UCS-2', - 'UCS-2BE', - 'UCS-2LE', - 'UTF-32', - 'UTF-32BE', - 'UTF-32LE', - 'UTF-16', - 'UTF-16BE', - 'UTF-16LE', - 'UTF-7', - 'UTF7-IMAP', - 'UTF-8', - 'ASCII', - 'EUC-JP', - 'SJIS', - 'EUCJP-WIN', - 'SJIS-WIN', - 'ISO-2022-JP', - 'ISO-2022-JP-MS', - 'CP932', - 'CP51932', - 'SJIS-MAC', 'MACJAPANESE', - 'SJIS-Mobile#DOCOMO', 'SJIS-DOCOMO', - 'SJIS-Mobile#KDDI', 'SJIS-KDDI', - 'SJIS-Mobile#SOFTBANK', 'SJIS-SOFTBANK', - 'UTF-8-Mobile#DOCOMO', 'UTF-8-DOCOMO', - 'UTF-8-Mobile#KDDI-A', - 'UTF-8-Mobile#KDDI-B', 'UTF-8-KDDI', - 'UTF-8-Mobile#SOFTBANK', 'UTF-8-SOFTBANK', - 'ISO-2022-JP-MOBILE#KDDI', 'ISO-2022-JP-KDDI', - 'JIS', - 'JIS-MS', - 'CP50220', - 'CP50220RAW', - 'CP50221', - 'CP50222', - 'ISO-8859-1', - 'ISO-8859-2', - 'ISO-8859-3', - 'ISO-8859-4', - 'ISO-8859-5', - 'ISO-8859-6', - 'ISO-8859-7', - 'ISO-8859-8', - 'ISO-8859-9', - 'ISO-8859-10', - 'ISO-8859-13', - 'ISO-8859-14', - 'ISO-8859-15', - // 'ISO-8859-16', - 'bYTE2BE', - 'bYTE2LE', - 'BYTE4BE', - 'BYTE4LE', - 'BASE64', - 'HTML-ENTITIES', - '7BIT', - '8BIT', - 'EUC-CN', - 'CP936', - 'GB18030', - 'HZ', - 'EUC-TW', - 'CP950', - 'BIG-5', - 'EUC-KR', - 'UHC', 'CP949', - 'ISO-2022-KR', - 'WINDOWS-1251', 'CP1251', - 'WINDOWS-1252', 'CP1252', - 'CP866', 'IBM866', - 'KOI8-R', - ); + protected $encodings = array(); /** * Constructor @@ -114,29 +38,7 @@ public function __construct() ); } - // remove encodings not available before PHP-5.4 - if (version_compare(PHP_VERSION, '5.4', '<')) { - unset( - $this->encodings['SJIS-MAC'], - $this->encodings['MACJAPANESE'], - $this->encodings['SJIS-Mobile#DOCOMO'], - $this->encodings['SJIS-DOCOMO'], - $this->encodings['SJIS-Mobile#KDDI'], - $this->encodings['SJIS-KDDI'], - $this->encodings['SJIS-Mobile#SOFTBANK'], - $this->encodings['SJIS-SOFTBANK'], - $this->encodings['UTF-8-Mobile#DOCOMO'], - $this->encodings['UTF-8-DOCOMO'], - $this->encodings['UTF-8-Mobile#KDDI-A'], - $this->encodings['UTF-8-Mobile#KDDI-B'], - $this->encodings['UTF-8-KDDI'], - $this->encodings['UTF-8-Mobile#SOFTBANK'], - $this->encodings['UTF-8-SOFTBANK'], - $this->encodings['ISO-2022-JP-MOBILE#KDDI'], - $this->encodings['ISO-2022-JP-KDDI'], - $this->encodings['GB18030'] - ); - } + $this->encodings = array_map('strtoupper', mb_list_encodings()); } /** From 6940c9ac4f24f85a4e765016d18fbb7cea26bc66 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 22:09:58 +0100 Subject: [PATCH 24/51] Updated Zend\Text to use StringUtils and deprecated Zend\Text\MultiByte --- library/Zend/Text/Figlet/Figlet.php | 25 ++- library/Zend/Text/MultiByte.php | 114 ++---------- library/Zend/Text/Table/Column.php | 9 +- tests/ZendTest/Text/MultiByteTest.php | 242 +------------------------- 4 files changed, 40 insertions(+), 350 deletions(-) diff --git a/library/Zend/Text/Figlet/Figlet.php b/library/Zend/Text/Figlet/Figlet.php index abb4996ed13..a325f00a5b0 100644 --- a/library/Zend/Text/Figlet/Figlet.php +++ b/library/Zend/Text/Figlet/Figlet.php @@ -13,6 +13,7 @@ use Traversable; use Zend\Stdlib\ArrayUtils; use Zend\Stdlib\ErrorHandler; +use Zend\Stdlib\StringUtils; /** * Zend\Text\Figlet is a PHP implementation of FIGlet @@ -413,8 +414,13 @@ public function render($text, $encoding = 'UTF-8') throw new Exception\InvalidArgumentException('$text must be a string'); } - if ($encoding !== 'UTF-8') { - $text = iconv($encoding, 'UTF-8', $text); + // Get the string wrapper supporting UTF-8 character encoding and the input encoding + $strWrapper = StringUtils::getWrapper('UTF-8', $encoding); + + // Convert $text to UTF-8 and check encoding + $text = $strWrapper->convert($text, 'UTF-8', $encoding); + if (!StringUtils::isValidUtf8($text)) { + throw new Exception\UnexpectedValueException('$text is not encoded with ' . $encoding); } $this->output = ''; @@ -427,21 +433,14 @@ public function render($text, $encoding = 'UTF-8') $wordBreakMode = 0; $lastCharWasEol = false; - - ErrorHandler::start(E_NOTICE); - $textLength = iconv_strlen($text, 'UTF-8'); - $error = ErrorHandler::stop(); - - if ($textLength === false) { - throw new Exception\UnexpectedValueException('$text is not encoded with ' . $encoding, 0, $error); - } + $textLength = $strWrapper->strlen($text, 'UTF-8'); for ($charNum = 0; $charNum < $textLength; $charNum++) { // Handle paragraphs - $char = iconv_substr($text, $charNum, 1, 'UTF-8'); + $char = $strWrapper->substr($text, $charNum, 1, 'UTF-8'); if ($char === "\n" && $this->handleParagraphs && !$lastCharWasEol) { - $nextChar = iconv_substr($text, ($charNum + 1), 1, 'UTF-8'); + $nextChar = $strWrapper->substr($text, ($charNum + 1), 1, 'UTF-8'); if (!$nextChar) { $nextChar = null; } @@ -686,7 +685,7 @@ protected function _addChar($char) } } - $this->outlineLength = strlen($this->outputLine[0]); + $this->outlineLength = strlen($this->outputLine[0]); $this->inCharLine[$this->inCharLineLength++] = $char; return true; diff --git a/library/Zend/Text/MultiByte.php b/library/Zend/Text/MultiByte.php index b3b4128ecc2..d7408533444 100644 --- a/library/Zend/Text/MultiByte.php +++ b/library/Zend/Text/MultiByte.php @@ -10,6 +10,8 @@ namespace Zend\Text; +use Zend\Stdlib\StringUtils; + /** * Contains multibyte safe string methods * @@ -28,70 +30,20 @@ class MultiByte * @param string $charset * @throws Exception\InvalidArgumentException * @return string + * @deprecated Please use Zend\Stdlib\StringUtils instead */ public static function wordWrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'utf-8') { - $stringWidth = iconv_strlen($string, $charset); - $breakWidth = iconv_strlen($break, $charset); - - if (strlen($string) === 0) { - return ''; - } - - if ($breakWidth === null) { - throw new Exception\InvalidArgumentException('Break string cannot be empty'); + trigger_error(sprintf( + "This method is deprecated, please use '%s' instead", + 'Zend\Stdlib\StringUtils::getWrapper()->wordWrap' + ), E_USER_DEPRECATED); + + try { + return StringUtils::getWrapper($charset)->wordWrap($string, $width, $break, $cut, $charset); + } catch (\Zend\Stdlib\Exception\InvalidArgumentException $e) { + throw new Exception\InvalidArgumentException($e->getMessage(), $e->getCode(), $e); } - - if ($width === 0 && $cut) { - throw new Exception\InvalidArgumentException('Cannot force cut when width is zero'); - } - - $result = ''; - $lastStart = $lastSpace = 0; - - for ($current = 0; $current < $stringWidth; $current++) { - $char = iconv_substr($string, $current, 1, $charset); - - $possibleBreak = $char; - if ($breakWidth !== 1) { - $possibleBreak = iconv_substr($string, $current, $breakWidth, $charset); - } - - if ($possibleBreak === $break) { - $result .= iconv_substr($string, $lastStart, $current - $lastStart + $breakWidth, $charset); - $current += $breakWidth - 1; - $lastStart = $lastSpace = $current + 1; - continue; - } - - if ($char === ' ') { - if ($current - $lastStart >= $width) { - $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break; - $lastStart = $current + 1; - } - - $lastSpace = $current; - continue; - } - - if ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) { - $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break; - $lastStart = $lastSpace = $current; - continue; - } - - if ($current - $lastStart >= $width && $lastStart < $lastSpace) { - $result .= iconv_substr($string, $lastStart, $lastSpace - $lastStart, $charset) . $break; - $lastStart = $lastSpace = $lastSpace + 1; - continue; - } - } - - if ($lastStart !== $current) { - $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset); - } - - return $result; } /** @@ -104,43 +56,13 @@ public static function wordWrap($string, $width = 75, $break = "\n", $cut = fals * @param string $charset * @return string */ - public static function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT, $charset = 'utf-8') + public static function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $charset = 'utf-8') { - $return = ''; - $lengthOfPadding = $padLength - iconv_strlen($input, $charset); - $padStringLength = iconv_strlen($padString, $charset); - - if ($padStringLength === 0 || $lengthOfPadding <= 0) { - $return = $input; - } else { - $repeatCount = floor($lengthOfPadding / $padStringLength); - - if ($padType === STR_PAD_BOTH) { - $lastStringLeft = ''; - $lastStringRight = ''; - $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2; - - $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength; - $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2); - $lastStringRightLength += $lastStringLength % 2; - - $lastStringLeft = iconv_substr($padString, 0, $lastStringLeftLength, $charset); - $lastStringRight = iconv_substr($padString, 0, $lastStringRightLength, $charset); - - $return = str_repeat($padString, $repeatCountLeft) . $lastStringLeft - . $input - . str_repeat($padString, $repeatCountRight) . $lastStringRight; - } else { - $lastString = iconv_substr($padString, 0, $lengthOfPadding % $padStringLength, $charset); - - if ($padType === STR_PAD_LEFT) { - $return = str_repeat($padString, $repeatCount) . $lastString . $input; - } else { - $return = $input . str_repeat($padString, $repeatCount) . $lastString; - } - } - } + trigger_error(sprintf( + "This method is deprecated, please use '%s' instead", + 'Zend\Stdlib\StringUtils::getWrapper()->strPad' + ), E_USER_DEPRECATED); - return $return; + return StringUtils::getWrapper($charset)->strPad($input, $padLength, $padString, $padType, $charset); } } diff --git a/library/Zend/Text/Table/Column.php b/library/Zend/Text/Table/Column.php index 2eb55e6b6ac..987d41e27f0 100644 --- a/library/Zend/Text/Table/Column.php +++ b/library/Zend/Text/Table/Column.php @@ -10,6 +10,7 @@ namespace Zend\Text\Table; +use Zend\Stdlib\StringUtils; use Zend\Text; /** @@ -107,7 +108,8 @@ public function setContent($content, $charset = null) if ($inputCharset !== $outputCharset) { if (PHP_OS !== 'AIX') { // AIX does not understand these character sets - $content = iconv($inputCharset, $outputCharset, $content); + $strWrapper = StringUtils::getWrapper($inputCharset, $outputCharset); + $content = $strWrapper->convert($content, $outputCharset, $inputCharset); } } @@ -203,12 +205,13 @@ public function render($columnWidth, $padding = 0) } $outputCharset = Table::getOutputCharset(); - $lines = explode("\n", Text\MultiByte::wordWrap($this->content, $columnWidth, "\n", true, $outputCharset)); + $strWrapper = StringUtils::getWrapper($outputCharset); + $lines = explode("\n", $strWrapper->wordWrap($this->content, $columnWidth, "\n", true, $outputCharset)); $paddedLines = array(); foreach ($lines AS $line) { $paddedLines[] = str_repeat(' ', $padding) - . Text\MultiByte::strPad($line, $columnWidth, ' ', $padMode, $outputCharset) + . $strWrapper->strPad($line, $columnWidth, ' ', $padMode, $outputCharset) . str_repeat(' ', $padding); } diff --git a/tests/ZendTest/Text/MultiByteTest.php b/tests/ZendTest/Text/MultiByteTest.php index 47bb4f82a0b..0abecac38a2 100644 --- a/tests/ZendTest/Text/MultiByteTest.php +++ b/tests/ZendTest/Text/MultiByteTest.php @@ -20,249 +20,15 @@ */ class MultiByteTest extends \PHPUnit_Framework_TestCase { - /** - * Standard cut tests - */ - public function testWordWrapCutSingleLine() + public function testWordWrapTriggersDeprecatedError() { + $this->setExpectedException('PHPUnit_Framework_Error_Deprecated'); $line = Text\MultiByte::wordWrap('äbüöcß', 2, ' ', true); - $this->assertEquals('äb üö cß', $line); } - public function testWordWrapCutMultiLine() - { - $line = Text\MultiByte::wordWrap('äbüöc ß äbüöcß', 2, ' ', true); - $this->assertEquals('äb üö c ß äb üö cß', $line); - } - - public function testWordWrapCutMultiLineShortWords() - { - $line = Text\MultiByte::wordWrap('Ä very long wöööööööööööörd.', 8, "\n", true); - $this->assertEquals("Ä very\nlong\nwööööööö\nööööörd.", $line); - } - - public function testWordWrapCutMultiLineWithPreviousNewlines() - { - $line = Text\MultiByte::wordWrap("Ä very\nlong wöööööööööööörd.", 8, "\n", false); - $this->assertEquals("Ä very\nlong\nwöööööööööööörd.", $line); - } - - /** - * Long-Break tests - */ - public function testWordWrapLongBreak() - { - $line = Text\MultiByte::wordWrap("Ä very
long wöö
öööööööö
öörd.", 8, '
', false); - $this->assertEquals("Ä very
long wöö
öööööööö
öörd.", $line); - } - - /** - * Alternative cut tests - */ - public function testWordWrapCutBeginningSingleSpace() - { - $line = Text\MultiByte::wordWrap(' äüöäöü', 3, ' ', true); - $this->assertEquals(' äüö äöü', $line); - } - - public function testWordWrapCutEndingSingleSpace() - { - $line = Text\MultiByte::wordWrap('äüöäöü ', 3, ' ', true); - $this->assertEquals('äüö äöü ', $line); - } - - public function testWordWrapCutEndingSingleSpaceWithNonSpaceDivider() - { - $line = Text\MultiByte::wordWrap('äöüäöü ', 3, '-', true); - $this->assertEquals('äöü-äöü-', $line); - } - - public function testWordWrapCutEndingTwoSpaces() - { - $line = Text\MultiByte::wordWrap('äüöäöü ', 3, ' ', true); - $this->assertEquals('äüö äöü ', $line); - } - - public function testWordWrapNoCutEndingSingleSpace() - { - $line = Text\Multibyte::wordWrap('12345 ', 5, '-', false); - $this->assertEquals('12345-', $line); - } - - public function testWordWrapNoCutEndingTwoSpaces() - { - $line = Text\MultiByte::wordWrap('12345 ', 5, '-', false); - $this->assertEquals('12345- ', $line); - } - - public function testWordWrapCutEndingThreeSpaces() - { - $line = Text\MultiByte::wordWrap('äüöäöü ', 3, ' ', true); - $this->assertEquals('äüö äöü ', $line); - } - - public function testWordWrapCutEndingTwoBreaks() - { - $line = Text\MultiByte::wordWrap('äüöäöü--', 3, '-', true); - $this->assertEquals('äüö-äöü--', $line); - } - - public function testWordWrapCutTab() - { - $line = Text\MultiByte::wordWrap("äbü\töcß", 3, ' ', true); - $this->assertEquals("äbü \töc ß", $line); - } - - public function testWordWrapCutNewlineWithSpace() - { - $line = Text\MultiByte::wordWrap("äbü\nößt", 3, ' ', true); - $this->assertEquals("äbü \nöß t", $line); - } - - public function testWordWrapCutNewlineWithNewline() - { - $line = Text\MultiByte::wordWrap("äbü\nößte", 3, "\n", true); - $this->assertEquals("äbü\nößt\ne", $line); - } - - /** - * Break cut tests - */ - public function testWordWrapCutBreakBefore() - { - $line = Text\MultiByte::wordWrap('foobar-foofoofoo', 8, '-', true); - $this->assertEquals('foobar-foofoofo-o', $line); - } - - public function testWordWrapCutBreakWith() - { - $line = Text\MultiByte::wordWrap('foobar-foobar', 6, '-', true); - $this->assertEquals('foobar-foobar', $line); - } - - public function testWordWrapCutBreakWithin() - { - $line = Text\MultiByte::wordWrap('foobar-foobar', 7, '-', true); - $this->assertEquals('foobar-foobar', $line); - } - - public function testWordWrapCutBreakWithinEnd() - { - $line = Text\MultiByte::wordWrap('foobar-', 7, '-', true); - $this->assertEquals('foobar-', $line); - } - - public function testWordWrapCutBreakAfter() - { - $line = Text\MultiByte::wordWrap('foobar-foobar', 5, '-', true); - $this->assertEquals('fooba-r-fooba-r', $line); - } - - /** - * Standard no-cut tests - */ - public function testWordWrapNoCutSingleLine() - { - $line = Text\MultiByte::wordWrap('äbüöcß', 2, ' ', false); - $this->assertEquals('äbüöcß', $line); - } - - public function testWordWrapNoCutMultiLine() - { - $line = Text\MultiByte::wordWrap('äbüöc ß äbüöcß', 2, "\n", false); - $this->assertEquals("äbüöc\nß\näbüöcß", $line); - } - - public function testWordWrapNoCutMultiWord() - { - $line = Text\MultiByte::wordWrap('äöü äöü äöü', 5, "\n", false); - $this->assertEquals("äöü\näöü\näöü", $line); - } - - /** - * Break no-cut tests - */ - public function testWordWrapNoCutBreakBefore() - { - $line = Text\MultiByte::wordWrap('foobar-foofoofoo', 8, '-', false); - $this->assertEquals('foobar-foofoofoo', $line); - } - - public function testWordWrapNoCutBreakWith() - { - $line = Text\MultiByte::wordWrap('foobar-foobar', 6, '-', false); - $this->assertEquals('foobar-foobar', $line); - } - - public function testWordWrapNoCutBreakWithin() - { - $line = Text\MultiByte::wordWrap('foobar-foobar', 7, '-', false); - $this->assertEquals('foobar-foobar', $line); - } - - public function testWordWrapNoCutBreakWithinEnd() - { - $line = Text\MultiByte::wordWrap('foobar-', 7, '-', false); - $this->assertEquals('foobar-', $line); - } - - public function testWordWrapNoCutBreakAfter() - { - $line = Text\MultiByte::wordWrap('foobar-foobar', 5, '-', false); - $this->assertEquals('foobar-foobar', $line); - } - - /** - * Pad tests - */ - public function testLeftPad() - { - $text = Text\MultiByte::strPad('äää', 5, 'ö', STR_PAD_LEFT); - $this->assertEquals('ööäää', $text); - } - - public function testCenterPad() - { - $text = Text\MultiByte::strPad('äää', 6, 'ö', STR_PAD_BOTH); - $this->assertEquals('öäääöö', $text); - } - - public function testRightPad() - { - $text = Text\MultiByte::strPad('äääöö', 5, 'ö', STR_PAD_RIGHT); - $this->assertEquals('äääöö', $text); - } - - public function testWordWrapInvalidArgument() - { - $this->setExpectedException('Zend\Text\Exception\InvalidArgumentException', "Cannot force cut when width is zero"); - Text\MultiByte::wordWrap('a', 0, "\n", true); - } - - /** - * @group ZF-12186 - */ - public function testPadInputLongerThanPadLength() + public function testStrPadTriggersDeprecatedError() { + $this->setExpectedException('PHPUnit_Framework_Error_Deprecated'); $text = Text\MultiByte::strPad('äääöö', 2, 'ö'); - $this->assertEquals('äääöö', $text); - } - - /** - * @group ZF-12186 - */ - public function testPadInputSameAsPadLength() - { - $text = Text\MultiByte::strPad('äääöö', 5, 'ö'); - $this->assertEquals('äääöö', $text); - } - - /** - * @group ZF-12186 - */ - public function testPadNegativePadLength() - { - $text = Text\MultiByte::strPad('äääöö', -2, 'ö'); - $this->assertEquals('äääöö', $text); } } From 1ec961d1498c9a62bac6ab89c87f76b69432a520 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 29 Nov 2012 22:14:56 +0100 Subject: [PATCH 25/51] Updated Zend\Feed to use StringUtils --- library/Zend/ProgressBar/Adapter/Console.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/library/Zend/ProgressBar/Adapter/Console.php b/library/Zend/ProgressBar/Adapter/Console.php index e3798092891..1e577f2fd33 100644 --- a/library/Zend/ProgressBar/Adapter/Console.php +++ b/library/Zend/ProgressBar/Adapter/Console.php @@ -12,6 +12,7 @@ use Zend\ProgressBar\Adapter\Exception; use Zend\Stdlib\ErrorHandler; +use Zend\Stdlib\StringUtils; /** * Zend_ProgressBar_Adapter_Console offers a text-based progressbar for console @@ -438,7 +439,13 @@ public function notify($current, $max, $percent, $timeTaken, $timeRemaining, $te break; case self::ELEMENT_TEXT: - $renderedElements[] = \Zend\Text\MultiByte::strPad(substr($text, 0, $this->textWidth), $this->textWidth, ' ', STR_PAD_RIGHT, $this->charset); + $renderedElements[] = StringUtils::getWrapper($this->charset)->strPad( + substr($text, 0, $this->textWidth), + $this->textWidth, + ' ', + \STR_PAD_RIGHT, + $this->charset + ); break; } } From 46e6e582204afeda354a48c342839ae0730457d7 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Fri, 30 Nov 2012 08:53:44 +0100 Subject: [PATCH 26/51] Zend\Feed: replaced one iconv_strlen with a string wrapper --- .../Feed/Writer/Extension/ITunes/Entry.php | 29 +++++++++++--- .../Feed/Writer/Extension/ITunes/Feed.php | 40 ++++++++++++++----- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/library/Zend/Feed/Writer/Extension/ITunes/Entry.php b/library/Zend/Feed/Writer/Extension/ITunes/Entry.php index d6b9ce9cb7e..a9169e93ba3 100644 --- a/library/Zend/Feed/Writer/Extension/ITunes/Entry.php +++ b/library/Zend/Feed/Writer/Extension/ITunes/Entry.php @@ -12,6 +12,8 @@ use Zend\Feed\Writer; use Zend\Feed\Writer\Extension; +use Zend\Stdlib\StringUtils; +use Zend\Stdlib\StringWrapper\StringWrapperInterface; /** * @category Zend @@ -33,6 +35,18 @@ class Entry */ protected $encoding = 'UTF-8'; + /** + * The used string wrapper supporting encoding + * + * @var StringWrapperInterface + */ + protected $stringWrapper; + + public function __construct() + { + $this->stringWrapper = StringUtils::getWrapper($this->getEncoding()); + } + /** * Set feed encoding * @@ -41,7 +55,8 @@ class Entry */ public function setEncoding($enc) { - $this->encoding = $enc; + $this->stringWrapper = StringUtils::getWrapper($enc); + $this->encoding = $enc; return $this; } @@ -68,7 +83,8 @@ public function setItunesBlock($value) throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only' . ' contain alphabetic characters'); } - if (iconv_strlen($value, $this->getEncoding()) > 255) { + + if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only' . ' contain a maximum of 255 characters'); } @@ -98,7 +114,7 @@ public function addItunesAuthors(array $values) */ public function addItunesAuthor($value) { - if (iconv_strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "author" may only' . ' contain a maximum of 255 characters each'); } @@ -160,8 +176,9 @@ public function setItunesKeywords(array $value) throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only' . ' contain a maximum of 12 terms'); } + $concat = implode(',', $value); - if (iconv_strlen($concat, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($concat, $this->getEncoding()) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only' . ' have a concatenated length of 255 chars where terms are delimited' . ' by a comma'); @@ -179,7 +196,7 @@ public function setItunesKeywords(array $value) */ public function setItunesSubtitle($value) { - if (iconv_strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "subtitle" may only' . ' contain a maximum of 255 characters'); } @@ -196,7 +213,7 @@ public function setItunesSubtitle($value) */ public function setItunesSummary($value) { - if (iconv_strlen($value, $this->getEncoding()) > 4000) { + if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 4000) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "summary" may only' . ' contain a maximum of 4000 characters'); } diff --git a/library/Zend/Feed/Writer/Extension/ITunes/Feed.php b/library/Zend/Feed/Writer/Extension/ITunes/Feed.php index 25d1d7bfece..ac6e27bfcfa 100644 --- a/library/Zend/Feed/Writer/Extension/ITunes/Feed.php +++ b/library/Zend/Feed/Writer/Extension/ITunes/Feed.php @@ -12,6 +12,8 @@ use Zend\Feed\Writer; use Zend\Uri; +use Zend\Stdlib\StringUtils; +use Zend\Stdlib\StringWrapper\StringWrapperInterface; /** * @category Zend @@ -33,6 +35,21 @@ class Feed */ protected $encoding = 'UTF-8'; + /** + * The used string wrapper supporting encoding + * + * @var StringWrapperInterface + */ + protected $stringWrapper; + + /** + * Constructor + */ + public function __construct() + { + $this->stringWrapper = StringUtils::getWrapper($this->getEncoding()); + } + /** * Set feed encoding * @@ -41,7 +58,8 @@ class Feed */ public function setEncoding($enc) { - $this->encoding = $enc; + $this->stringWrapper = StringUtils::getWrapper($enc); + $this->encoding = $enc; return $this; } @@ -68,7 +86,7 @@ public function setItunesBlock($value) throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only' . ' contain alphabetic characters'); } - if (iconv_strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only' . ' contain a maximum of 255 characters'); } @@ -99,7 +117,7 @@ public function addItunesAuthors(array $values) */ public function addItunesAuthor($value) { - if (iconv_strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "author" may only' . ' contain a maximum of 255 characters each'); } @@ -124,19 +142,19 @@ public function setItunesCategories(array $values) } foreach ($values as $key=>$value) { if (!is_array($value)) { - if (iconv_strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "category" may only' . ' contain a maximum of 255 characters each'); } $this->data['categories'][] = $value; } else { - if (iconv_strlen($key, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($key, $this->getEncoding()) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "category" may only' . ' contain a maximum of 255 characters each'); } $this->data['categories'][$key] = array(); foreach ($value as $val) { - if (iconv_strlen($val, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($val, $this->getEncoding()) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "category" may only' . ' contain a maximum of 255 characters each'); } @@ -221,7 +239,7 @@ public function setItunesKeywords(array $value) . ' contain a maximum of 12 terms'); } $concat = implode(',', $value); - if (iconv_strlen($concat, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($concat, $this->getEncoding()) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only' . ' have a concatenated length of 255 chars where terms are delimited' . ' by a comma'); @@ -274,8 +292,8 @@ public function addItunesOwner(array $value) throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "owner" must' . ' be an array containing keys "name" and "email"'); } - if (iconv_strlen($value['name'], $this->getEncoding()) > 255 - || iconv_strlen($value['email'], $this->getEncoding()) > 255 + if ($this->stringWrapper->strlen($value['name'], $this->getEncoding()) > 255 + || $this->stringWrapper->strlen($value['email'], $this->getEncoding()) > 255 ) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "owner" may only' . ' contain a maximum of 255 characters each for "name" and "email"'); @@ -296,7 +314,7 @@ public function addItunesOwner(array $value) */ public function setItunesSubtitle($value) { - if (iconv_strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "subtitle" may only' . ' contain a maximum of 255 characters'); } @@ -313,7 +331,7 @@ public function setItunesSubtitle($value) */ public function setItunesSummary($value) { - if (iconv_strlen($value, $this->getEncoding()) > 4000) { + if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 4000) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "summary" may only' . ' contain a maximum of 4000 characters'); } From a82e5e721abe03837674e8d0b0fd113c1cb835d0 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Fri, 30 Nov 2012 09:16:08 +0100 Subject: [PATCH 27/51] FIXME: Converting the euro sign from UTF-8 to ISO-8859-16 using the mbstring extension gives a wrong result --- library/Zend/Stdlib/StringWrapper/Intl.php | 15 --------------- library/Zend/Stdlib/StringWrapper/MbString.php | 6 ++++++ 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/library/Zend/Stdlib/StringWrapper/Intl.php b/library/Zend/Stdlib/StringWrapper/Intl.php index e9c9ae9f502..81f1549436f 100644 --- a/library/Zend/Stdlib/StringWrapper/Intl.php +++ b/library/Zend/Stdlib/StringWrapper/Intl.php @@ -47,11 +47,6 @@ public function __construct() */ public function strlen($str, $encoding = 'UTF-8') { - if (strcasecmp($encoding, 'UTF-8') != 0) { - trigger_error("Character set '{$encoding}' not supported by intl"); - return false; - } - return grapheme_strlen($str); } @@ -66,11 +61,6 @@ public function strlen($str, $encoding = 'UTF-8') */ public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') { - if (strcasecmp($encoding, 'UTF-8') != 0) { - trigger_error("Character set '{$encoding}' not supported by intl"); - return false; - } - return grapheme_substr($str, $offset, $length); } @@ -85,11 +75,6 @@ public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') */ public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') { - if (strcasecmp($encoding, 'UTF-8') != 0) { - trigger_error("Character set '{$encoding}' not supported by intl"); - return false; - } - return grapheme_strpos($haystack, $needle, $offset); } } diff --git a/library/Zend/Stdlib/StringWrapper/MbString.php b/library/Zend/Stdlib/StringWrapper/MbString.php index a17241228f9..849f3abc58e 100644 --- a/library/Zend/Stdlib/StringWrapper/MbString.php +++ b/library/Zend/Stdlib/StringWrapper/MbString.php @@ -39,6 +39,12 @@ public function __construct() } $this->encodings = array_map('strtoupper', mb_list_encodings()); + + // FIXME: Converting € (UTF-8) to ISO-8859-16 gives a wrong result + $indexIso885916 = array_search('ISO-8859-16', $this->encodings, true); + if ($indexIso885916 !== false) { + unset($this->encodings[$indexIso885916]); + } } /** From cb97199fd72f0cd96f1a0a7d6069505220ef509c Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Fri, 30 Nov 2012 09:57:52 +0100 Subject: [PATCH 28/51] removed trailing spaces --- library/Zend/Feed/Writer/Extension/ITunes/Entry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Zend/Feed/Writer/Extension/ITunes/Entry.php b/library/Zend/Feed/Writer/Extension/ITunes/Entry.php index a9169e93ba3..6384c0a452b 100644 --- a/library/Zend/Feed/Writer/Extension/ITunes/Entry.php +++ b/library/Zend/Feed/Writer/Extension/ITunes/Entry.php @@ -83,7 +83,7 @@ public function setItunesBlock($value) throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only' . ' contain alphabetic characters'); } - + if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only' . ' contain a maximum of 255 characters'); From 1fa9f899d22a840459c2c6e9625c42d9fa977126 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Mon, 31 Dec 2012 16:33:37 +0100 Subject: [PATCH 29/51] Changed API to only check given encoding once and to better support the new Intl-UConverter of PHP 5.5 --- library/Zend/Stdlib/StringUtils.php | 57 ++--- .../StringWrapper/AbstractStringWrapper.php | 139 +++++++---- library/Zend/Stdlib/StringWrapper/Iconv.php | 34 ++- library/Zend/Stdlib/StringWrapper/Intl.php | 24 +- .../Zend/Stdlib/StringWrapper/MbString.php | 52 +++-- library/Zend/Stdlib/StringWrapper/Native.php | 22 +- .../StringWrapper/StringWrapperInterface.php | 40 ++-- .../StringWrapper/CommonStringWrapperTest.php | 217 ++++++++---------- .../Stdlib/StringWrapper/IconvTest.php | 16 +- .../Stdlib/StringWrapper/IntlTest.php | 16 +- .../Stdlib/StringWrapper/MbStringTest.php | 16 +- .../Stdlib/StringWrapper/NativeTest.php | 13 +- 12 files changed, 378 insertions(+), 268 deletions(-) diff --git a/library/Zend/Stdlib/StringUtils.php b/library/Zend/Stdlib/StringUtils.php index 2b1b227e831..68ce89bedc2 100644 --- a/library/Zend/Stdlib/StringUtils.php +++ b/library/Zend/Stdlib/StringUtils.php @@ -11,10 +11,6 @@ namespace Zend\Stdlib; use Zend\Stdlib\StringWrapper\StringWrapperInterface; -use Zend\Stdlib\StringWrapper\MbString as MbStringWrapper; -use Zend\Stdlib\StringWrapper\Iconv as IconvWrapper; -use Zend\Stdlib\StringWrapper\Intl as IntlWrapper; -use Zend\Stdlib\StringWrapper\Native as NativeWrapper; /** * Utility class for handling strings of different character encodings @@ -50,9 +46,9 @@ abstract class StringUtils ); /** - * Get registered wrappers + * Get registered wrapper classes * - * @return StringWrapperInterface[] + * @return string[] */ public static function getRegisteredWrappers() { @@ -60,74 +56,71 @@ public static function getRegisteredWrappers() static::$wrapperRegistry = array(); if (extension_loaded('intl')) { - static::$wrapperRegistry[] = new IntlWrapper(); + static::$wrapperRegistry[] = 'Zend\Stdlib\StringWrapper\Intl'; } if (extension_loaded('mbstring')) { - static::$wrapperRegistry[] = new MbStringWrapper(); + static::$wrapperRegistry[] = 'Zend\Stdlib\StringWrapper\MbString'; } if (extension_loaded('iconv')) { - static::$wrapperRegistry[] = new IconvWrapper(); + static::$wrapperRegistry[] = 'Zend\Stdlib\StringWrapper\Iconv'; } - static::$wrapperRegistry[] = new NativeWrapper(); + static::$wrapperRegistry[] = 'Zend\Stdlib\StringWrapper\Native'; } return static::$wrapperRegistry; } /** - * Register a string wrapper + * Register a string wrapper class * - * @param StringWrapperInterface + * @param string $wrapper * @return void */ - public static function registerWrapper(StringWrapperInterface $wrapper) + public static function registerWrapper($wrapper) { + $wrapper = (string) $wrapper; if (!in_array($wrapper, static::$wrapperRegistry, true)) { static::$wrapperRegistry[] = $wrapper; } } /** - * Unregister a string wrapper + * Unregister a string wrapper class * - * @param StringWrapperInterface $wrapper + * @param string $wrapper * @return void */ - public static function unregisterWrapper(StringWrapperInterface $wrapper) + public static function unregisterWrapper($wrapper) { - $index = array_search($wrapper, static::$wrapperRegistry, true); + $index = array_search((string) $wrapper, static::$wrapperRegistry, true); if ($index !== false) { unset(static::$wrapperRegistry[$index]); } } /** - * Get the first string wrapper supporting one or more character encodings + * Get the first string wrapper supporting the given character encoding + * and supports to convert into the given convert encoding. * - * @param string $encoding Character encoding supported by he string wrapper - * @param string $encodingN, ... Unlimited OPTIONAL number of additional character encodings + * @param string $encoding Character encoding to support + * @param string|null $convertEncoding OPTIONAL character encoding to convert in * @return StringWrapperInterface - * @throws Exception\RuntimeException If no wrapper supports all given character encodings + * @throws Exception\RuntimeException If no wrapper supports given character encodings */ - public static function getWrapper($encoding = 'UTF-8') + public static function getWrapper($encoding = 'UTF-8', $convertEncoding = null) { - $encodings = func_get_args(); - - foreach (static::getRegisteredWrappers() as $wrapper) { - foreach ($encodings as $encoding) { - if (!$wrapper->isEncodingSupported($encoding)) { - continue 2; - } + foreach (static::getRegisteredWrappers() as $wrapperClass) { + if ($wrapperClass::isSupported($encoding, $convertEncoding)) { + return new $wrapperClass($encoding, $convertEncoding); } - - return $wrapper; } throw new Exception\RuntimeException( - 'No wrapper found supporting encoding(s) ' . implode(', ', $encodings) + 'No wrapper found supporting "' . $encoding . '"' + . (($convertEncoding !== null) ? ' and "' . $convertEncoding . '"' : '') ); } diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index bd9e7713fcf..69a658f2a48 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -20,51 +20,108 @@ */ abstract class AbstractStringWrapper implements StringWrapperInterface { + /** + * The character encoding working on + * @var string|null + */ + protected $encoding; /** - * List of supported character encodings (upper case) - * - * @var string[] + * An optionally character encoding to convert to + * @var string|null */ - protected $encodings = array(); + protected $convertEncoding; /** - * Check if the given character encoding is supported + * Check if the given character encoding is supported by this wrapper + * and the character encoding to convert to is also supported. * - * @param string $encoding - * @return boolean + * @param string $encoding + * @param string|null $convertEncoding + */ + public static function isSupported($encoding, $convertEncoding = null) + { + $supportedEncodings = static::getSupportedEncodings(); + + if (!in_array(strtoupper($encoding), $supportedEncodings)) { + return false; + } + + if ($convertEncoding !== null && !in_array(strtoupper($convertEncoding), $supportedEncodings)) { + return false; + } + + return true; + } + + /** + * Constructor + * @param string $encoding Character encoding working on + * @param string|null $convertEncoding Character encoding to convert to + * @throws Exception\InvalidArgumentException */ - public function isEncodingSupported($encoding) + public function __construct($encoding, $convertEncoding = null) { - return in_array(strtoupper($encoding), $this->encodings); + $this->setEncoding($encoding, $convertEncoding); } /** - * Get a list of supported character encodings + * Set character encoding working with and convert to * - * @return string[] + * @param string $encoding The character encoding to work with + * @param string|null $convertEncoding The character encoding to convert to + * @return StringWrapperInterface */ - public function getSupportedEncodings() + public function setEncoding($encoding, $convertEncoding) { - return $this->encodings; + $supportedEncodings = static::getSupportedEncodings(); + + $encodingUpper = strtoupper($encoding); + if (!in_array($encodingUpper, $supportedEncodings)) { + throw new Exception\InvalidArgumentException( + 'Wrapper doesn\'t support character encoding "' . $encoding . '"' + ); + } + + + if ($convertEncoding !== null) { + $convertEncodingUpper = strtoupper($convertEncoding); + if (!in_array($convertEncodingUpper, $supportedEncodings)) { + throw new Exception\InvalidArgumentException( + 'Wrapper doesn\'t support character encoding "' . $convertEncoding . '"' + ); + } + + $this->convertEncoding = $convertEncodingUpper; + } else { + $this->convertEncoding = null; + } + $this->encoding = $encodingUpper; + + return $this; } /** * Convert a string from one character encoding to another * - * @param string $str - * @param string $toEncoding - * @param string $fromEncoding + * @param string $str + * @param boolean $backward * @return string|false */ - public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') + public function convert($str, $backward = false) { - if (strcasecmp($toEncoding, $fromEncoding) != 0) { - trigger_error("Can't convert '{$fromEncoding}' to '{$toEncoding}'", \E_WARNING); - return false; + $from = $backward ? $this->convertEncoding : $this->encoding; + $to = $backward ? $this->encoding : $this->convertEncoding; + + if ($to == $from) { + return $str; } - return $str; + throw new Exception\RuntimeException(sprintf( + 'Converting from "%s" to "%s" isn\'t supported by this string wrapper', + $from, + $to + )); } /** @@ -74,10 +131,9 @@ public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') * @param integer $width * @param string $break * @param boolean $cut - * @param string $encoding * @return string|false */ - public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $encoding = 'UTF-8') + public function wordWrap($string, $width = 75, $break = "\n", $cut = false) { $string = (string) $string; if ($string === '') { @@ -95,27 +151,26 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $enc throw new Exception\InvalidArgumentException('Cannot force cut when width is zero'); } - $encoding = strtoupper($encoding); - if (StringUtils::isSingleByteEncoding($encoding)) { + if (StringUtils::isSingleByteEncoding($this->encoding)) { return wordwrap($string, $width, $break, $cut); } - $stringWidth = $this->strlen($string, $encoding); - $breakWidth = $this->strlen($break, $encoding); + $stringWidth = $this->strlen($string); + $breakWidth = $this->strlen($break); $result = ''; $lastStart = $lastSpace = 0; for ($current = 0; $current < $stringWidth; $current++) { - $char = $this->substr($string, $current, 1, $encoding); + $char = $this->substr($string, $current, 1); $possibleBreak = $char; if ($breakWidth !== 1) { - $possibleBreak = $this->substr($string, $current, $breakWidth, $encoding); + $possibleBreak = $this->substr($string, $current, $breakWidth); } if ($possibleBreak === $break) { - $result .= $this->substr($string, $lastStart, $current - $lastStart + $breakWidth, $encoding); + $result .= $this->substr($string, $lastStart, $current - $lastStart + $breakWidth); $current += $breakWidth - 1; $lastStart = $lastSpace = $current + 1; continue; @@ -123,7 +178,7 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $enc if ($char === ' ') { if ($current - $lastStart >= $width) { - $result .= $this->substr($string, $lastStart, $current - $lastStart, $encoding) . $break; + $result .= $this->substr($string, $lastStart, $current - $lastStart) . $break; $lastStart = $current + 1; } @@ -132,20 +187,20 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $enc } if ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) { - $result .= $this->substr($string, $lastStart, $current - $lastStart, $encoding) . $break; + $result .= $this->substr($string, $lastStart, $current - $lastStart) . $break; $lastStart = $lastSpace = $current; continue; } if ($current - $lastStart >= $width && $lastStart < $lastSpace) { - $result .= $this->substr($string, $lastStart, $lastSpace - $lastStart, $encoding) . $break; + $result .= $this->substr($string, $lastStart, $lastSpace - $lastStart) . $break; $lastStart = $lastSpace = $lastSpace + 1; continue; } } if ($lastStart !== $current) { - $result .= $this->substr($string, $lastStart, $current - $lastStart, $encoding); + $result .= $this->substr($string, $lastStart, $current - $lastStart); } return $result; @@ -158,18 +213,16 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $enc * @param integer $padLength * @param string $padString * @param integer $padType - * @param string $encoding * @return string */ - public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $encoding = 'UTF-8') + public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT) { - $encoding = strtoupper($encoding); - if (StringUtils::isSingleByteEncoding($encoding)) { + if (StringUtils::isSingleByteEncoding($this->encoding)) { return str_pad($input, $padLength, $padString, $padType); } - $lengthOfPadding = $padLength - $this->strlen($input, $encoding); - $padStringLength = $this->strlen($padString, $encoding); + $lengthOfPadding = $padLength - $this->strlen($input); + $padStringLength = $this->strlen($padString); if ($padStringLength === 0 || $lengthOfPadding <= 0) { return $input; @@ -186,14 +239,14 @@ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2); $lastStringRightLength += $lastStringLength % 2; - $lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength, $encoding); - $lastStringRight = $this->substr($padString, 0, $lastStringRightLength, $encoding); + $lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength); + $lastStringRight = $this->substr($padString, 0, $lastStringRightLength); return str_repeat($padString, $repeatCountLeft) . $lastStringLeft . $input . str_repeat($padString, $repeatCountRight) . $lastStringRight; } else { - $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength, $encoding); + $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength); if ($padType === \STR_PAD_LEFT) { return str_repeat($padString, $repeatCount) . $lastString . $input; diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index 3c97ad08332..f4982fb1202 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -10,6 +10,8 @@ namespace Zend\Stdlib\StringWrapper; +use Zend\Stdlib\Exception; + /** * @category Zend * @package Zend_Stdlib @@ -23,7 +25,7 @@ class Iconv extends AbstractStringWrapper * @var string[] * @link http://www.gnu.org/software/libiconv/ */ - protected $encodings = array( + protected static $encodings = array( // European languages 'ASCII', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', 'ISO-8859-7', @@ -114,18 +116,30 @@ class Iconv extends AbstractStringWrapper 'ATARIST', 'RISCOS-LATIN1', ); + /** + * Get a list of supported character encodings + * + * @return string[] + */ + public static function getSupportedEncodings() + { + return static::$encodings; + } + /** * Constructor * * @throws Exception\ExtensionNotLoadedException */ - public function __construct() + public function __construct($encoding, $convertEncoding = null) { if (!extension_loaded('iconv')) { throw new Exception\ExtensionNotLoadedException( 'PHP extension "iconv" is required for this wrapper' ); } + + parent::__construct($encoding, $convertEncoding); } /** @@ -135,9 +149,9 @@ public function __construct() * @param string $encoding * @return int|false */ - public function strlen($str, $encoding = 'UTF-8') + public function strlen($str) { - return iconv_strlen($str, $encoding); + return iconv_strlen($str, $this->encoding); } /** @@ -149,9 +163,9 @@ public function strlen($str, $encoding = 'UTF-8') * @param string $encoding * @return string|false */ - public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') + public function substr($str, $offset = 0, $length = null) { - return iconv_substr($str, $offset, $length, $encoding); + return iconv_substr($str, $offset, $length, $this->encoding); } /** @@ -163,9 +177,9 @@ public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') * @param string $encoding * @return int|false */ - public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') + public function strpos($haystack, $needle, $offset = 0) { - return iconv_strpos($haystack, $needle, $offset, $encoding); + return iconv_strpos($haystack, $needle, $offset, $this->encoding); } /** @@ -176,8 +190,10 @@ public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') * @param string $fromEncoding * @return string|false */ - public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') + public function convert($str, $backword = false) { + $fromEncoding = $backword ? $this->convertEncoding : $this->encoding; + $toEncoding = $backword ? $this->encoding : $this->convertEncoding; return iconv($fromEncoding, $toEncoding, $str); } } diff --git a/library/Zend/Stdlib/StringWrapper/Intl.php b/library/Zend/Stdlib/StringWrapper/Intl.php index 81f1549436f..7b48accd69e 100644 --- a/library/Zend/Stdlib/StringWrapper/Intl.php +++ b/library/Zend/Stdlib/StringWrapper/Intl.php @@ -10,6 +10,8 @@ namespace Zend\Stdlib\StringWrapper; +use Zend\Stdlib\Exception; + /** * @category Zend * @package Zend_Stdlib @@ -22,20 +24,32 @@ class Intl extends AbstractStringWrapper * * @var string[] */ - protected $encodings = array('UTF-8'); + protected static $encodings = array('UTF-8'); + + /** + * Get a list of supported character encodings + * + * @return string[] + */ + public static function getSupportedEncodings() + { + return static::$encodings; + } /** * Constructor * * @throws Exception\ExtensionNotLoadedException */ - public function __construct() + public function __construct($encoding, $convertEncoding = null) { if (!extension_loaded('intl')) { throw new Exception\ExtensionNotLoadedException( 'PHP extension "intl" is required for this wrapper' ); } + + parent::__construct($encoding, $convertEncoding); } /** @@ -45,7 +59,7 @@ public function __construct() * @param string $encoding * @return int|false */ - public function strlen($str, $encoding = 'UTF-8') + public function strlen($str) { return grapheme_strlen($str); } @@ -59,7 +73,7 @@ public function strlen($str, $encoding = 'UTF-8') * @param string $encoding * @return string|false */ - public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') + public function substr($str, $offset = 0, $length = null) { return grapheme_substr($str, $offset, $length); } @@ -73,7 +87,7 @@ public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') * @param string $encoding * @return int|false */ - public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') + public function strpos($haystack, $needle, $offset = 0) { return grapheme_strpos($haystack, $needle, $offset); } diff --git a/library/Zend/Stdlib/StringWrapper/MbString.php b/library/Zend/Stdlib/StringWrapper/MbString.php index 849f3abc58e..bf72a5a7c14 100644 --- a/library/Zend/Stdlib/StringWrapper/MbString.php +++ b/library/Zend/Stdlib/StringWrapper/MbString.php @@ -10,6 +10,8 @@ namespace Zend\Stdlib\StringWrapper; +use Zend\Stdlib\Exception; + /** * @category Zend * @package Zend_Stdlib @@ -20,17 +22,37 @@ class MbString extends AbstractStringWrapper /** * List of supported character sets (upper case) * - * @var string[] + * @var null|string[] * @link http://php.net/manual/mbstring.supported-encodings.php */ - protected $encodings = array(); + protected static $encodings = null; + + /** + * Get a list of supported character encodings + * + * @return string[] + */ + public static function getSupportedEncodings() + { + if (static::$encodings === null) { + static::$encodings = array_map('strtoupper', mb_list_encodings()); + + // FIXME: Converting € (UTF-8) to ISO-8859-16 gives a wrong result + $indexIso885916 = array_search('ISO-8859-16', static::$encodings, true); + if ($indexIso885916 !== false) { + unset(static::$encodings[$indexIso885916]); + } + } + + return static::$encodings; + } /** * Constructor * * @throws Exception\ExtensionNotLoadedException */ - public function __construct() + public function __construct($encoding, $convertEncoding = null) { if (!extension_loaded('mbstring')) { throw new Exception\ExtensionNotLoadedException( @@ -38,13 +60,7 @@ public function __construct() ); } - $this->encodings = array_map('strtoupper', mb_list_encodings()); - - // FIXME: Converting € (UTF-8) to ISO-8859-16 gives a wrong result - $indexIso885916 = array_search('ISO-8859-16', $this->encodings, true); - if ($indexIso885916 !== false) { - unset($this->encodings[$indexIso885916]); - } + parent::__construct($encoding, $convertEncoding); } /** @@ -54,9 +70,9 @@ public function __construct() * @param string $encoding * @return int|false */ - public function strlen($str, $encoding = 'UTF-8') + public function strlen($str) { - return mb_strlen($str, $encoding); + return mb_strlen($str, $this->encoding); } /** @@ -68,9 +84,9 @@ public function strlen($str, $encoding = 'UTF-8') * @param string $encoding * @return string|false */ - public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') + public function substr($str, $offset = 0, $length = null) { - return mb_substr($str, $offset, $length, $encoding); + return mb_substr($str, $offset, $length, $this->encoding); } /** @@ -82,9 +98,9 @@ public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') * @param string $encoding * @return int|false */ - public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') + public function strpos($haystack, $needle, $offset = 0) { - return mb_strpos($haystack, $needle, $offset, $encoding); + return mb_strpos($haystack, $needle, $offset, $this->encoding); } /** @@ -95,8 +111,10 @@ public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') * @param string $fromEncoding * @return string|false */ - public function convert($str, $toEncoding, $fromEncoding = 'UTF-8') + public function convert($str, $backword = false) { + $fromEncoding = $backword ? $this->convertEncoding : $this->encoding; + $toEncoding = $backword ? $this->encoding : $this->convertEncoding; return mb_convert_encoding($str, $toEncoding, $fromEncoding); } } diff --git a/library/Zend/Stdlib/StringWrapper/Native.php b/library/Zend/Stdlib/StringWrapper/Native.php index 3aee7f13bf7..69468bd69dc 100644 --- a/library/Zend/Stdlib/StringWrapper/Native.php +++ b/library/Zend/Stdlib/StringWrapper/Native.php @@ -19,23 +19,12 @@ */ class Native extends AbstractStringWrapper { - /** - * Check if the given encoding is supported - * - * @param string $encoding - * @return boolean - */ - public function isEncodingSupported($encoding) - { - return StringUtils::isSingleByteEncoding($encoding); - } - /** * Get a list of supported character encodings * * @return string[] */ - public function getSupportedEncodings() + public static function getSupportedEncodings() { return StringUtils::getSingleByteEncodings(); } @@ -44,10 +33,9 @@ public function getSupportedEncodings() * Returns the length of the given string * * @param string $str - * @param string $encoding * @return int|false */ - public function strlen($str, $encoding = 'UTF-8') + public function strlen($str) { return strlen($str); } @@ -58,10 +46,9 @@ public function strlen($str, $encoding = 'UTF-8') * @param string $str * @param int $offset * @param int|null $length - * @param string $encoding * @return string|false */ - public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') + public function substr($str, $offset = 0, $length = null) { return substr($str, $offset, $length); } @@ -72,10 +59,9 @@ public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8') * @param string $haystack * @param string $needle * @param int $offset - * @param string $encoding * @return int|false */ - public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8') + public function strpos($haystack, $needle, $offset = 0) { return strpos($haystack, $needle, $offset); } diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index f3f8ebe15e7..f99dbf73cdc 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -17,30 +17,37 @@ */ interface StringWrapperInterface { - /** - * Check if the given character encoding is supported + * Check if the given character encoding is supported by this wrapper + * and the character encoding to convert to is also supported. * - * @param string $encoding - * @return boolean + * @param string $encoding + * @param string|null $convertEncoding */ - public function isEncodingSupported($encoding); + public static function isSupported($encoding, $convertEncoding = null); /** * Get a list of supported character encodings * * @return string[] */ - public function getSupportedEncodings(); + public static function getSupportedEncodings(); + + /** + * Constructor + * + * @param string $encoding Character encoding working on + * @param string|null $convertEncoding Character encoding to convert to + */ + public function __construct($encoding, $convertEncoding = null); /** * Returns the length of the given string * * @param string $str - * @param string $encoding * @return int|false */ - public function strlen($str, $encoding = 'UTF-8'); + public function strlen($str); /** * Returns the portion of string specified by the start and length parameters @@ -51,7 +58,7 @@ public function strlen($str, $encoding = 'UTF-8'); * @param string $encoding * @return string|false */ - public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8'); + public function substr($str, $offset = 0, $length = null); /** * Find the position of the first occurrence of a substring in a string @@ -62,17 +69,16 @@ public function substr($str, $offset = 0, $length = null, $encoding = 'UTF-8'); * @param string $encoding * @return int|false */ - public function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8'); + public function strpos($haystack, $needle, $offset = 0); /** * Convert a string from one character encoding to another * - * @param string $str - * @param string $toEncoding - * @param string $fromEncoding + * @param string $str + * @param boolean $backward * @return string|false */ - public function convert($str, $toEncoding, $fromEncoding = 'UTF-8'); + public function convert($str, $backward = false); /** * Wraps a string to a given number of characters @@ -81,10 +87,9 @@ public function convert($str, $toEncoding, $fromEncoding = 'UTF-8'); * @param integer $width * @param string $break * @param boolean $cut - * @param string $encoding * @return string */ - public function wordWrap($str, $width = 75, $break = "\n", $cut = false, $encoding = 'UTF-8'); + public function wordWrap($str, $width = 75, $break = "\n", $cut = false); /** * Pad a string to a certain length with another string @@ -93,8 +98,7 @@ public function wordWrap($str, $width = 75, $break = "\n", $cut = false, $encodi * @param integer $padLength * @param string $padString * @param integer $padType - * @param string $encoding * @return string */ - public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $encoding = 'UTF-8'); + public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT); } diff --git a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php index 4f606a1ae3e..6bea2a0d65a 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php @@ -17,286 +17,269 @@ abstract class CommonStringWrapperTest extends TestCase { - - /** - * An instance of the string wrapper to test - * @StringWrapperInterface - */ - protected $stringWrapper; - - public function setUp() - { - if ( !($this->stringWrapper instanceof StringWrapperInterface) ) { - $this->fail(sprintf( - "%s isn't an instance of %s", - get_class($this) . '::stringWrapper', - 'Zend\Stdlib\StringWrapper\StringWrapperInterface' - )); - } - } + abstract protected function getWrapper($encoding = null, $convertEncoding = null); public function strlenProvider() { return array( - array('abcdefghijklmnopqrstuvwxyz', 'ascii', 26), - array('abcdefghijklmnopqrstuvwxyz', 'utf-8', 26), - array('äöüß', 'utf-8', 4), + array('ascii', 'abcdefghijklmnopqrstuvwxyz', 26), + array('utf-8', 'abcdefghijklmnopqrstuvwxyz', 26), + array('utf-8', 'äöüß', 4), ); } /** * @dataProvider strlenProvider - * @param string $string * @param string $encoding + * @param string $string * @param mixed $expected */ - public function testStrlen($str, $encoding, $expected) + public function testStrlen($encoding, $str, $expected) { - if (!$this->stringWrapper->isEncodingSupported($encoding)) { - $this->markTestSkipped( - "Encoding {$encoding} not supported by " . get_class($this->stringWrapper) - ); + $wrapper = $this->getWrapper($encoding); + if (!$wrapper) { + $this->markTestSkipped("Encoding {$encoding} not supported"); } - $result = $this->stringWrapper->strlen($str, $encoding); + $result = $wrapper->strlen($str); $this->assertSame($expected, $result); } public function substrProvider() { return array( - array('abcdefghijkl', 1, 5, 'ascii', 'bcdef'), - array('abcdefghijkl', 1, 5, 'utf-8', 'bcdef'), - array('äöüß', 1, 2, 'utf-8', 'öü'), + array('ascii', 'abcdefghijkl', 1, 5, 'bcdef'), + array('utf-8', 'abcdefghijkl', 1, 5, 'bcdef'), + array('utf-8', 'äöüß', 1, 2, 'öü'), ); } /** * @dataProvider substrProvider + * @param string $encoding * @param string $str * @param int $offset * @param int|null $length - * @param string $encoding * @param mixed $expected */ - public function testSubstr($str, $offset, $length, $encoding, $expected) + public function testSubstr($encoding, $str, $offset, $length, $expected) { - if (!$this->stringWrapper->isEncodingSupported($encoding)) { - $this->markTestSkipped( - "Encoding {$encoding} not supported by " . get_class($this->stringWrapper) - ); + $wrapper = $this->getWrapper($encoding); + if (!$wrapper) { + $this->markTestSkipped("Encoding {$encoding} not supported"); } - $result = $this->stringWrapper->substr($str, $offset, $length, $encoding); + $result = $wrapper->substr($str, $offset, $length); $this->assertSame($expected, $result); } public function strposProvider() { return array( - array('abcdefghijkl', 'g', 3, 'ascii', 6), - array('abcdefghijkl', 'g', 3, 'utf-8', 6), - array('äöüß', 'ü', 1, 'utf-8', 2), + array('ascii', 'abcdefghijkl', 'g', 3, 6), + array('utf-8', 'abcdefghijkl', 'g', 3, 6), + array('utf-8', 'äöüß', 'ü', 1, 2), ); } /** * @dataProvider strposProvider + * @param string $encoding * @param string $haystack * @param string $needle * @param int $offset - * @param string $encoding * @param mixed $expected */ - public function testStrpos($haystack, $needle, $offset, $encoding, $expected) + public function testStrpos($encoding, $haystack, $needle, $offset, $expected) { - if (!$this->stringWrapper->isEncodingSupported($encoding)) { - $this->markTestSkipped( - "Encoding {$encoding} not supported by " . get_class($this->stringWrapper) - ); + $wrapper = $this->getWrapper($encoding); + if (!$wrapper) { + $this->markTestSkipped("Encoding {$encoding} not supported"); } - $result = $this->stringWrapper->strpos($haystack, $needle, $offset, $encoding); + $result = $wrapper->strpos($haystack, $needle, $offset); $this->assertSame($expected, $result); } public function convertProvider() { return array( - array('abc', 'ascii', 'ascii', 'abc'), - array('abc', 'utf-8', 'ascii', 'abc'), - array('abc', 'ascii', 'utf-8', 'abc'), - array('€', 'iso-8859-15', 'utf-8', "\xA4"), - array('€', 'iso-8859-16', 'utf-8', "\xA4"), // ISO-8859-16 is wrong @ mbstring + array('ascii', 'ascii', 'abc', 'abc'), + array('ascii', 'utf-8', 'abc', 'abc'), + array('utf-8', 'ascii', 'abc', 'abc'), + array('utf-8', 'iso-8859-15', '€', "\xA4"), + array('utf-8', 'iso-8859-16', '€', "\xA4"), // ISO-8859-16 is wrong @ mbstring ); } /** * @dataProvider convertProvider * @param string $str - * @param string $toEncoding - * @param string $fromEncoding + * @param string $encoding + * @param string $convertEncoding * @param mixed $expected */ - public function testConvert($str, $toEncoding, $fromEncoding, $expected) + public function testConvert($encoding, $convertEncoding, $str, $expected) { - if (!$this->stringWrapper->isEncodingSupported($toEncoding)) { - $this->markTestSkipped( - "Encoding {$toEncoding} not supported by " . get_class($this->stringWrapper) - ); - } elseif (!$this->stringWrapper->isEncodingSupported($fromEncoding)) { - $this->markTestSkipped( - "Encoding {$fromEncoding} not supported by " . get_class($this->stringWrapper) - ); + $wrapper = $this->getWrapper($encoding, $convertEncoding); + if (!$wrapper) { + $this->markTestSkipped("Encoding {$encoding} or {$convertEncoding} not supported"); } - $result = $this->stringWrapper->convert($str, $toEncoding, $fromEncoding); + $result = $wrapper->convert($str); $this->assertSame($expected, $result); + + // backword + $result = $wrapper->convert($expected, true); + $this->assertSame($str, $result); } public function wordWrapProvider() { return array( // Standard cut tests - array('äbüöcß', 2, ' ', true, 'utf-8', + array('utf-8', 'äbüöcß', 2, ' ', true, 'äb üö cß'), - array('äbüöc ß äbüöcß', 2, ' ', true, 'utf-8', + array('utf-8', 'äbüöc ß äbüöcß', 2, ' ', true, 'äb üö c ß äb üö cß'), - array('Ä very long wöööööööööööörd.', 8, "\n", true, 'utf-8', + array('utf-8', 'Ä very long wöööööööööööörd.', 8, "\n", true, "Ä very\nlong\nwööööööö\nööööörd."), - array("Ä very\nlong wöööööööööööörd.", 8, "\n", false, 'utf-8', + array('utf-8', "Ä very\nlong wöööööööööööörd.", 8, "\n", false, "Ä very\nlong\nwöööööööööööörd."), - array("Ä very
long wöö
öööööööö
öörd.", 8, '
', false, 'utf-8', + array('utf-8', "Ä very
long wöö
öööööööö
öörd.", 8, '
', false, "Ä very
long wöö
öööööööö
öörd."), // Alternative cut tests - array(' äüöäöü', 3, ' ', true, 'utf-8', + array('utf-8', ' äüöäöü', 3, ' ', true, ' äüö äöü'), - array('äüöäöü ', 3, ' ', true, 'utf-8', + array('utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '), - array('äöüäöü ', 3, '-', true, 'utf-8', + array('utf-8', 'äöüäöü ', 3, '-', true, 'äöü-äöü-'), - array('äüöäöü ', 3, ' ', true, 'utf-8', + array('utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '), - array('12345 ', 5, '-', false, 'utf-8', + array('utf-8', '12345 ', 5, '-', false, '12345-'), - array('12345 ', 5, '-', false, 'utf-8', + array('utf-8', '12345 ', 5, '-', false, '12345- '), - array('äüöäöü ', 3, ' ', true, 'utf-8', + array('utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '), - array('äüöäöü--', 3, '-', true, 'utf-8', + array('utf-8', 'äüöäöü--', 3, '-', true, 'äüö-äöü--'), - array("äbü\töcß", 3, ' ', true, 'utf-8', + array('utf-8', "äbü\töcß", 3, ' ', true, "äbü \töc ß"), - array("äbü\nößt", 3, ' ', true, 'utf-8', + array('utf-8', "äbü\nößt", 3, ' ', true, "äbü \nöß t"), - array("äbü\nößte", 3, "\n", true, 'utf-8', + array('utf-8', "äbü\nößte", 3, "\n", true, "äbü\nößt\ne"), // Break cut tests - array('foobar-foofoofoo', 8, '-', true, 'ascii', + array('ascii', 'foobar-foofoofoo', 8, '-', true, 'foobar-foofoofo-o'), - array('foobar-foobar', 6, '-', true, 'ascii', + array('ascii', 'foobar-foobar', 6, '-', true, 'foobar-foobar'), - array('foobar-foobar', 7, '-', true, 'ascii', + array('ascii', 'foobar-foobar', 7, '-', true, 'foobar-foobar'), - array('foobar-', 7, '-', true, 'ascii', + array('ascii', 'foobar-', 7, '-', true, 'foobar-'), - array('foobar-foobar', 5, '-', true, 'ascii', + array('ascii', 'foobar-foobar', 5, '-', true, 'fooba-r-fooba-r'), // Standard no-cut tests - array('äbüöcß', 2, ' ', false, 'utf-8', + array('utf-8', 'äbüöcß', 2, ' ', false, 'äbüöcß'), - array('äbüöc ß äbüöcß', 2, "\n", false, 'utf-8', + array('utf-8', 'äbüöc ß äbüöcß', 2, "\n", false, "äbüöc\nß\näbüöcß"), - array('äöü äöü äöü', 5, "\n", false, 'utf-8', + array('utf-8', 'äöü äöü äöü', 5, "\n", false, "äöü\näöü\näöü"), // Break no-cut tests - array('foobar-foofoofoo', 8, '-', false, 'ascii', + array('ascii', 'foobar-foofoofoo', 8, '-', false, 'foobar-foofoofoo'), - array('foobar-foobar', 6, '-', false, 'ascii', + array('ascii', 'foobar-foobar', 6, '-', false, 'foobar-foobar'), - array('foobar-foobar', 7, '-', false, 'ascii', + array('ascii', 'foobar-foobar', 7, '-', false, 'foobar-foobar'), - array('foobar-', 7, '-', false, 'ascii', + array('ascii', 'foobar-', 7, '-', false, 'foobar-'), - array('foobar-foobar', 5, '-', false, 'ascii', + array('ascii', 'foobar-foobar', 5, '-', false, 'foobar-foobar'), ); } /** * @dataProvider wordWrapProvider + * @param string $encoding * @param string $str * @param integer $width * @param string $break * @param boolean $cut - * @param string $encoding * @param mixed $expected */ - public function testWordWrap($string, $width, $break, $cut, $encoding, $expected) + public function testWordWrap($encoding, $string, $width, $break, $cut, $expected) { - if (!$this->stringWrapper->isEncodingSupported($encoding)) { - $this->markTestSkipped( - "Encoding {$encoding} not supported by " . get_class($this->stringWrapper) - ); + $wrapper = $this->getWrapper($encoding); + if (!$wrapper) { + $this->markTestSkipped("Encoding {$encoding} not supported"); } - $result = $this->stringWrapper->wordWrap($string, $width, $break, $cut, $encoding); + $result = $wrapper->wordWrap($string, $width, $break, $cut); $this->assertSame($expected, $result); } public function testWordWrapInvalidArgument() { + $wrapper = $this->getWrapper(); + if (!$wrapper) { + $this->fail("Can't instantiate wrapper"); + } + $this->setExpectedException( 'Zend\Stdlib\Exception\InvalidArgumentException', "Cannot force cut when width is zero" ); - $this->stringWrapper->wordWrap('a', 0, "\n", true); + $wrapper->wordWrap('a', 0, "\n", true); } public function strPadProvider() { return array( // single-byte - array('aaa', 5, 'o', STR_PAD_LEFT, 'ascii', 'ooaaa'), - array('aaa', 6, 'o', STR_PAD_BOTH, 'ascii', 'oaaaoo'), - array('aaa', 5, 'o', STR_PAD_RIGHT, 'ascii', 'aaaoo'), + array('ascii', 'aaa', 5, 'o', STR_PAD_LEFT, 'ooaaa'), + array('ascii', 'aaa', 6, 'o', STR_PAD_BOTH, 'oaaaoo'), + array('ascii', 'aaa', 5, 'o', STR_PAD_RIGHT, 'aaaoo'), // multi-byte - array('äää', 5, 'ö', STR_PAD_LEFT, 'utf-8', 'ööäää'), - array('äää', 6, 'ö', STR_PAD_BOTH, 'utf-8', 'öäääöö'), - array('äää', 5, 'ö', STR_PAD_RIGHT, 'utf-8', 'äääöö'), + array('utf-8', 'äää', 5, 'ö', STR_PAD_LEFT, 'ööäää'), + array('utf-8', 'äää', 6, 'ö', STR_PAD_BOTH, 'öäääöö'), + array('utf-8', 'äää', 5, 'ö', STR_PAD_RIGHT, 'äääöö'), // ZF-12186 - array('äääöö', 2, 'ö', STR_PAD_RIGHT, 'utf-8', 'äääöö'), // PadInputLongerThanPadLength - array('äääöö', 5, 'ö', STR_PAD_RIGHT, 'utf-8', 'äääöö'), // PadInputSameAsPadLength - array('äääöö', -2, 'ö', STR_PAD_RIGHT, 'utf-8', 'äääöö'), // PadNegativePadLength + array('utf-8', 'äääöö', 2, 'ö', STR_PAD_RIGHT, 'äääöö'), // PadInputLongerThanPadLength + array('utf-8', 'äääöö', 5, 'ö', STR_PAD_RIGHT, 'äääöö'), // PadInputSameAsPadLength + array('utf-8', 'äääöö', -2, 'ö', STR_PAD_RIGHT, 'äääöö'), // PadNegativePadLength ); } /** * @dataProvider strPadProvider + * @param string $encoding * @param string $input * @param integer $padLength * @param string $padString * @param integer $padType - * @param string $encoding * @param mixed $expected * * @group ZF-12186 */ - public function testStrPad($input, $padLength, $padString, $padType, $encoding, $expected) + public function testStrPad($encoding, $input, $padLength, $padString, $padType, $expected) { - if (!$this->stringWrapper->isEncodingSupported($encoding)) { - $this->markTestSkipped( - "Encoding {$encoding} not supported by " . get_class($this->stringWrapper) - ); + $wrapper = $this->getWrapper($encoding); + if (!$wrapper) { + $this->markTestSkipped("Encoding {$encoding} not supported"); } - $result = $this->stringWrapper->strPad($input, $padLength, $padString, $padType, $encoding); + $result = $wrapper->strPad($input, $padLength, $padString, $padType); $this->assertSame($expected, $result); } } diff --git a/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php b/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php index 92a83e1c683..b1f3a0bbaff 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php @@ -21,14 +21,26 @@ public function setUp() { if (!extension_loaded('iconv')) { try { - new Iconv(); + new Iconv('utf-8'); $this->fail('Missing expected Zend\Stdlib\Exception\ExtensionNotLoadedException'); } catch (Exception\ExtensionNotLoadedException $e) { $this->markTestSkipped('Missing ext/iconv'); } } - $this->stringWrapper = new Iconv(); parent::setUp(); } + + protected function getWrapper($encoding = null, $convertEncoding = null) + { + if ($encoding === null) { + $supportedEncodings = Iconv::getSupportedEncodings(); + $encoding = array_shift($supportedEncodings); + } + + if (!Iconv::isSupported($encoding, $convertEncoding)) { + return false; + } + return new Iconv($encoding, $convertEncoding); + } } diff --git a/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php b/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php index f764b3d98a9..ab2ae0402b0 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php @@ -21,14 +21,26 @@ public function setUp() { if (!extension_loaded('intl')) { try { - new Intl(); + new Intl('utf-8'); $this->fail('Missing expected Zend\Stdlib\Exception\ExtensionNotLoadedException'); } catch (Exception\ExtensionNotLoadedException $e) { $this->markTestSkipped('Missing ext/intl'); } } - $this->stringWrapper = new Intl(); parent::setUp(); } + + protected function getWrapper($encoding = null, $convertEncoding = null) + { + if ($encoding === null) { + $supportedEncodings = Intl::getSupportedEncodings(); + $encoding = array_shift($supportedEncodings); + } + + if (!Intl::isSupported($encoding, $convertEncoding)) { + return false; + } + return new Intl($encoding, $convertEncoding); + } } diff --git a/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php b/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php index bd6ee7aecde..fe4a5500d92 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php @@ -21,14 +21,26 @@ public function setUp() { if (!extension_loaded('mbstring')) { try { - new MbString(); + new MbString('utf-8'); $this->fail('Missing expected Zend\Stdlib\Exception\ExtensionNotLoadedException'); } catch (Exception\ExtensionNotLoadedException $e) { $this->markTestSkipped('Missing ext/mbstring'); } } - $this->stringWrapper = new MbString(); parent::setUp(); } + + protected function getWrapper($encoding = null, $convertEncoding = null) + { + if ($encoding === null) { + $supportedEncodings = MbString::getSupportedEncodings(); + $encoding = array_shift($supportedEncodings); + } + + if (!MbString::isSupported($encoding, $convertEncoding)) { + return false; + } + return new MbString($encoding, $convertEncoding); + } } diff --git a/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php b/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php index 329a19ba390..8f3b64ccdc1 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php @@ -16,9 +16,16 @@ class NativeTest extends CommonStringWrapperTest { - public function setUp() + protected function getWrapper($encoding = null, $convertEncoding = null) { - $this->stringWrapper = new Native(); - parent::setUp(); + if ($encoding === null) { + $supportedEncodings = Native::getSupportedEncodings(); + $encoding = array_shift($supportedEncodings); + } + + if (!Native::isSupported($encoding, $convertEncoding)) { + return false; + } + return new Native($encoding, $convertEncoding); } } From 61418de569d5c9acbeee4ccea82e225dd6dbd705 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Mon, 31 Dec 2012 17:50:03 +0100 Subject: [PATCH 30/51] Updated consumers after API changes --- .../View/Console/RouteNotFoundStrategy.php | 2 +- library/Zend/ProgressBar/Adapter/Console.php | 3 +- .../StringWrapper/AbstractStringWrapper.php | 2 +- .../StringWrapper/StringWrapperInterface.php | 9 +++++ library/Zend/Text/Figlet/Figlet.php | 12 ++++--- library/Zend/Text/MultiByte.php | 4 +-- library/Zend/Text/Table/Column.php | 8 ++--- library/Zend/Validator/Barcode/Code128.php | 31 ++++++++-------- library/Zend/Validator/Hostname.php | 2 +- library/Zend/Validator/StringLength.php | 35 +++++++++++++++---- 10 files changed, 71 insertions(+), 37 deletions(-) diff --git a/library/Zend/Mvc/View/Console/RouteNotFoundStrategy.php b/library/Zend/Mvc/View/Console/RouteNotFoundStrategy.php index afaa65b376f..4daffb87227 100644 --- a/library/Zend/Mvc/View/Console/RouteNotFoundStrategy.php +++ b/library/Zend/Mvc/View/Console/RouteNotFoundStrategy.php @@ -407,7 +407,7 @@ protected function renderTable($data, $cols, $consoleWidth) for ($x = 1; $x <= $cols; $x += 1) { $maxW[$x] = 0; foreach ($data as $row) { - $maxW[$x] = max($maxW[$x], $strWrapper->strlen($row[$x-1],'UTF-8') + $padding * 2); + $maxW[$x] = max($maxW[$x], $strWrapper->strlen($row[$x-1]) + $padding * 2); } } diff --git a/library/Zend/ProgressBar/Adapter/Console.php b/library/Zend/ProgressBar/Adapter/Console.php index 1e577f2fd33..50a52e3da86 100644 --- a/library/Zend/ProgressBar/Adapter/Console.php +++ b/library/Zend/ProgressBar/Adapter/Console.php @@ -443,8 +443,7 @@ public function notify($current, $max, $percent, $timeTaken, $timeRemaining, $te substr($text, 0, $this->textWidth), $this->textWidth, ' ', - \STR_PAD_RIGHT, - $this->charset + \STR_PAD_RIGHT ); break; } diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 69a658f2a48..89ad28ce6a2 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -72,7 +72,7 @@ public function __construct($encoding, $convertEncoding = null) * @param string|null $convertEncoding The character encoding to convert to * @return StringWrapperInterface */ - public function setEncoding($encoding, $convertEncoding) + public function setEncoding($encoding, $convertEncoding = null) { $supportedEncodings = static::getSupportedEncodings(); diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index f99dbf73cdc..bba34fe2348 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -41,6 +41,15 @@ public static function getSupportedEncodings(); */ public function __construct($encoding, $convertEncoding = null); + /** + * Set character encoding working with and convert to + * + * @param string $encoding The character encoding to work with + * @param string|null $convertEncoding The character encoding to convert to + * @return StringWrapperInterface + */ + public function setEncoding($encoding, $convertEncoding = null); + /** * Returns the length of the given string * diff --git a/library/Zend/Text/Figlet/Figlet.php b/library/Zend/Text/Figlet/Figlet.php index a325f00a5b0..dbbe7ab754b 100644 --- a/library/Zend/Text/Figlet/Figlet.php +++ b/library/Zend/Text/Figlet/Figlet.php @@ -415,14 +415,16 @@ public function render($text, $encoding = 'UTF-8') } // Get the string wrapper supporting UTF-8 character encoding and the input encoding - $strWrapper = StringUtils::getWrapper('UTF-8', $encoding); + $strWrapper = StringUtils::getWrapper($encoding, 'UTF-8'); // Convert $text to UTF-8 and check encoding - $text = $strWrapper->convert($text, 'UTF-8', $encoding); + $text = $strWrapper->convert($text); if (!StringUtils::isValidUtf8($text)) { throw new Exception\UnexpectedValueException('$text is not encoded with ' . $encoding); } + $strWrapper = StringUtils::getWrapper('UTF-8'); + $this->output = ''; $this->outputLine = array(); @@ -433,14 +435,14 @@ public function render($text, $encoding = 'UTF-8') $wordBreakMode = 0; $lastCharWasEol = false; - $textLength = $strWrapper->strlen($text, 'UTF-8'); + $textLength = $strWrapper->strlen($text); for ($charNum = 0; $charNum < $textLength; $charNum++) { // Handle paragraphs - $char = $strWrapper->substr($text, $charNum, 1, 'UTF-8'); + $char = $strWrapper->substr($text, $charNum, 1); if ($char === "\n" && $this->handleParagraphs && !$lastCharWasEol) { - $nextChar = $strWrapper->substr($text, ($charNum + 1), 1, 'UTF-8'); + $nextChar = $strWrapper->substr($text, ($charNum + 1), 1); if (!$nextChar) { $nextChar = null; } diff --git a/library/Zend/Text/MultiByte.php b/library/Zend/Text/MultiByte.php index d7408533444..c00def16bd8 100644 --- a/library/Zend/Text/MultiByte.php +++ b/library/Zend/Text/MultiByte.php @@ -40,7 +40,7 @@ public static function wordWrap($string, $width = 75, $break = "\n", $cut = fals ), E_USER_DEPRECATED); try { - return StringUtils::getWrapper($charset)->wordWrap($string, $width, $break, $cut, $charset); + return StringUtils::getWrapper($charset)->wordWrap($string, $width, $break, $cut); } catch (\Zend\Stdlib\Exception\InvalidArgumentException $e) { throw new Exception\InvalidArgumentException($e->getMessage(), $e->getCode(), $e); } @@ -63,6 +63,6 @@ public static function strPad($input, $padLength, $padString = ' ', $padType = \ 'Zend\Stdlib\StringUtils::getWrapper()->strPad' ), E_USER_DEPRECATED); - return StringUtils::getWrapper($charset)->strPad($input, $padLength, $padString, $padType, $charset); + return StringUtils::getWrapper($charset)->strPad($input, $padLength, $padString, $padType); } } diff --git a/library/Zend/Text/Table/Column.php b/library/Zend/Text/Table/Column.php index 987d41e27f0..2729769aca1 100644 --- a/library/Zend/Text/Table/Column.php +++ b/library/Zend/Text/Table/Column.php @@ -109,7 +109,7 @@ public function setContent($content, $charset = null) if (PHP_OS !== 'AIX') { // AIX does not understand these character sets $strWrapper = StringUtils::getWrapper($inputCharset, $outputCharset); - $content = $strWrapper->convert($content, $outputCharset, $inputCharset); + $content = $strWrapper->convert($content); } } @@ -206,12 +206,12 @@ public function render($columnWidth, $padding = 0) $outputCharset = Table::getOutputCharset(); $strWrapper = StringUtils::getWrapper($outputCharset); - $lines = explode("\n", $strWrapper->wordWrap($this->content, $columnWidth, "\n", true, $outputCharset)); + $lines = explode("\n", $strWrapper->wordWrap($this->content, $columnWidth, "\n", true)); $paddedLines = array(); - foreach ($lines AS $line) { + foreach ($lines as $line) { $paddedLines[] = str_repeat(' ', $padding) - . $strWrapper->strPad($line, $columnWidth, ' ', $padMode, $outputCharset) + . $strWrapper->strPad($line, $columnWidth, ' ', $padMode) . str_repeat(' ', $padding); } diff --git a/library/Zend/Validator/Barcode/Code128.php b/library/Zend/Validator/Barcode/Code128.php index cbd4c2fb195..e801af3f3ee 100644 --- a/library/Zend/Validator/Barcode/Code128.php +++ b/library/Zend/Validator/Barcode/Code128.php @@ -10,6 +10,7 @@ namespace Zend\Validator\Barcode; +use Zend\Validator\Exception; use Zend\Stdlib\StringUtils; use Zend\Stdlib\StringWrapper\StringWrapperInterface; @@ -84,7 +85,7 @@ public function __construct() public function setUtf8StringWrapper(StringWrapperInterface $utf8StringWrapper) { - if (!$utf8StringWrapper->isEncodingSupported('UTF-8')) { + if (!$utf8StringWrapper->isSupported('UTF-8')) { throw new Exception\InvalidArgumentException( "The string wrapper needs to support UTF-8 character encoding" ); @@ -124,12 +125,12 @@ public function hasValidCharacters($value) $set = $this->getCodingSet($value); $read = $set; if ($set != '') { - $value = $strWrapper->substr($value, 1, null, 'UTF-8'); + $value = $strWrapper->substr($value, 1, null); } // process barcode while ($value != '') { - $char = $strWrapper->substr($value, 0, 1, 'UTF-8'); + $char = $strWrapper->substr($value, 0, 1); switch ($char) { // Function definition @@ -185,11 +186,11 @@ public function hasValidCharacters($value) break; } - $value = $strWrapper->substr($value, 1, null, 'UTF-8'); + $value = $strWrapper->substr($value, 1, null); $read = $set; } - if (($value != '') && ($strWrapper->strlen($value, 'UTF-8') != 1)) { + if (($value != '') && ($strWrapper->strlen($value) != 1)) { return false; } @@ -210,7 +211,7 @@ protected function code128($value) $read = $set; $usecheck = $this->useChecksum(null); $strWrapper = $this->getUtf8StringWrapper(); - $char = $strWrapper->substr($value, 0, 1, 'UTF-8'); + $char = $strWrapper->substr($value, 0, 1); if ($char == '‡') { $sum = 103; } elseif ($char == 'ˆ') { @@ -222,11 +223,11 @@ protected function code128($value) return false; } - $value = $strWrapper->substr($value, 1, null, 'UTF-8'); - while ($strWrapper->strpos($value, 'Š', 0, 'UTF-8') || ($value != '')) { - $char = $strWrapper->substr($value, 0, 1, 'UTF-8'); + $value = $strWrapper->substr($value, 1, null); + while ($strWrapper->strpos($value, 'Š') || ($value != '')) { + $char = $strWrapper->substr($value, 0, 1); if ($read == 'C') { - $char = $strWrapper->substr($value, 0, 2, 'UTF-8'); + $char = $strWrapper->substr($value, 0, 2); } switch ($char) { @@ -283,22 +284,22 @@ protected function code128($value) break; } - $value = $strWrapper->substr($value, 1, null, 'UTF-8'); + $value = $strWrapper->substr($value, 1); ++$pos; - if (($strWrapper->strpos($value, 'Š', 0, 'UTF-8') == 1) && ($strWrapper->strlen($value, 'UTF-8') == 2)) { + if (($strWrapper->strpos($value, 'Š') == 1) && ($strWrapper->strlen($value) == 2)) { // break by stop and checksum char break; } $read = $set; } - if (($strWrapper->strpos($value, 'Š', 0, 'UTF-8') != 1) || ($strWrapper->strlen($value, 'UTF-8') != 2)) { + if (($strWrapper->strpos($value, 'Š') != 1) || ($strWrapper->strlen($value) != 2)) { // return false if checksum is not readable and true if no startvalue is detected return (!$usecheck); } $mod = $sum % 103; - if ($strWrapper->substr($value, 0, 1, 'UTF-8') == $this->chr128($mod, $set)) { + if ($strWrapper->substr($value, 0, 1) == $this->chr128($mod, $set)) { return true; } @@ -313,7 +314,7 @@ protected function code128($value) */ protected function getCodingSet($value) { - $value = $this->getUtf8StringWrapper()->substr($value, 0, 1, 'UTF-8'); + $value = $this->getUtf8StringWrapper()->substr($value, 0, 1); switch ($value) { case '‡' : return 'A'; diff --git a/library/Zend/Validator/Hostname.php b/library/Zend/Validator/Hostname.php index 6f2825d14a1..afd035c064c 100644 --- a/library/Zend/Validator/Hostname.php +++ b/library/Zend/Validator/Hostname.php @@ -557,7 +557,7 @@ public function isValid($value) $length = $this->idnLength[strtoupper($this->tld)]; } - if ($utf8StrWrapper->strlen($domainPart, 'UTF-8') > $length) { + if ($utf8StrWrapper->strlen($domainPart) > $length) { $this->error(self::INVALID_HOSTNAME); } else { $checked = true; diff --git a/library/Zend/Validator/StringLength.php b/library/Zend/Validator/StringLength.php index 99186704ea3..58ce77212cb 100644 --- a/library/Zend/Validator/StringLength.php +++ b/library/Zend/Validator/StringLength.php @@ -11,6 +11,7 @@ namespace Zend\Validator; use Zend\Stdlib\StringUtils; +use Zend\Stdlib\StringWrapper\StringWrapperInterface as StringWrapper; /** * @category Zend @@ -130,6 +131,31 @@ public function setMax($max) return $this; } + /** + * Get the string wrapper to detect the string length + * + * @return StringWrapper + */ + public function getStringWrapper() + { + if (!$this->stringWrapper) { + $this->stringWrapper = StringUtils::getWrapper($this->getEncoding()); + } + return $this->stringWrapper; + } + + /** + * Set the string wrapper to detect the string length + * + * @param StringWrapper + * @return StringLength + */ + public function setStringWrapper(StringWrapper $stringWrapper) + { + $stringWrapper->setEncoding($this->getEncoding()); + $this->stringWrapper = $stringWrapper; + } + /** * Returns the actual encoding * @@ -147,12 +173,9 @@ public function getEncoding() * @return StringLength * @throws Exception\InvalidArgumentException */ - public function setEncoding($encoding = null) + public function setEncoding($encoding) { - if ($encoding !== null) { - StringUtils::getWrapper($encoding); - } - + $this->stringWrapper = StringUtils::getWrapper($encoding); $this->options['encoding'] = $encoding; return $this; } @@ -173,7 +196,7 @@ public function isValid($value) $this->setValue($value); - $length = StringUtils::getWrapper($this->getEncoding())->strlen($value, $this->getEncoding()); + $length = $this->getStringWrapper()->strlen($value); if ($length < $this->getMin()) { $this->error(self::TOO_SHORT); } From 9a55394c8662bffc1b8b08606b5e660c90333182 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Wed, 2 Jan 2013 18:34:32 +0100 Subject: [PATCH 31/51] It's 2013 --- library/Zend/Stdlib/StringUtils.php | 2 +- library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php | 2 +- library/Zend/Stdlib/StringWrapper/Iconv.php | 2 +- library/Zend/Stdlib/StringWrapper/Intl.php | 2 +- library/Zend/Stdlib/StringWrapper/MbString.php | 2 +- library/Zend/Stdlib/StringWrapper/Native.php | 2 +- library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php | 2 +- tests/ZendTest/Stdlib/StringUtilsTest.php | 2 +- tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php | 2 +- tests/ZendTest/Stdlib/StringWrapper/IconvTest.php | 2 +- tests/ZendTest/Stdlib/StringWrapper/IntlTest.php | 2 +- tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php | 2 +- tests/ZendTest/Stdlib/StringWrapper/NativeTest.php | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/library/Zend/Stdlib/StringUtils.php b/library/Zend/Stdlib/StringUtils.php index 68ce89bedc2..031800b7031 100644 --- a/library/Zend/Stdlib/StringUtils.php +++ b/library/Zend/Stdlib/StringUtils.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib */ diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 89ad28ce6a2..4b4e8544c36 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib */ diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index f4982fb1202..946624474e8 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib */ diff --git a/library/Zend/Stdlib/StringWrapper/Intl.php b/library/Zend/Stdlib/StringWrapper/Intl.php index 7b48accd69e..3669e002b61 100644 --- a/library/Zend/Stdlib/StringWrapper/Intl.php +++ b/library/Zend/Stdlib/StringWrapper/Intl.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib */ diff --git a/library/Zend/Stdlib/StringWrapper/MbString.php b/library/Zend/Stdlib/StringWrapper/MbString.php index bf72a5a7c14..4ae1f1b1cdc 100644 --- a/library/Zend/Stdlib/StringWrapper/MbString.php +++ b/library/Zend/Stdlib/StringWrapper/MbString.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib */ diff --git a/library/Zend/Stdlib/StringWrapper/Native.php b/library/Zend/Stdlib/StringWrapper/Native.php index 69468bd69dc..0f26fcb0844 100644 --- a/library/Zend/Stdlib/StringWrapper/Native.php +++ b/library/Zend/Stdlib/StringWrapper/Native.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib */ diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index bba34fe2348..35467cef073 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib */ diff --git a/tests/ZendTest/Stdlib/StringUtilsTest.php b/tests/ZendTest/Stdlib/StringUtilsTest.php index 156431dde06..fefed580767 100644 --- a/tests/ZendTest/Stdlib/StringUtilsTest.php +++ b/tests/ZendTest/Stdlib/StringUtilsTest.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib */ diff --git a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php index 6bea2a0d65a..5ae04708ab1 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib * @subpackage StringWrapper diff --git a/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php b/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php index b1f3a0bbaff..2819b6b000f 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib * @subpackage StringWrapper diff --git a/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php b/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php index ab2ae0402b0..efa3fdddb68 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib * @subpackage StringWrapper diff --git a/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php b/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php index fe4a5500d92..01f332bee70 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib * @subpackage StringWrapper diff --git a/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php b/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php index 8f3b64ccdc1..9a3716dbdfc 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @package Zend_Stdlib * @subpackage StringWrapper From d59ae386313fffff6b583d74781d17ed6dc9222e Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 3 Jan 2013 15:23:07 +0100 Subject: [PATCH 32/51] else isn't needed, as the previous block returns on completion + removed call to strlen if length of padding is less than or equal 0 --- .../StringWrapper/AbstractStringWrapper.php | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 4b4e8544c36..eebf006fa69 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -222,9 +222,12 @@ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD } $lengthOfPadding = $padLength - $this->strlen($input); - $padStringLength = $this->strlen($padString); + if ($lengthOfPadding <= 0) { + return $input; + } - if ($padStringLength === 0 || $lengthOfPadding <= 0) { + $padStringLength = $this->strlen($padString); + if ($padStringLength === 0) { return $input; } @@ -245,14 +248,14 @@ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD return str_repeat($padString, $repeatCountLeft) . $lastStringLeft . $input . str_repeat($padString, $repeatCountRight) . $lastStringRight; - } else { - $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength); + } - if ($padType === \STR_PAD_LEFT) { - return str_repeat($padString, $repeatCount) . $lastString . $input; - } else { - return $input . str_repeat($padString, $repeatCount) . $lastString; - } + $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength); + + if ($padType === \STR_PAD_LEFT) { + return str_repeat($padString, $repeatCount) . $lastString . $input; } + + return $input . str_repeat($padString, $repeatCount) . $lastString; } } From 4a2896f2b03534862c4078f6f794e5a7867e072d Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 3 Jan 2013 15:41:34 +0100 Subject: [PATCH 33/51] StringWrapper::convert() Renamed second argument to and fixed phpdoc --- .../Stdlib/StringWrapper/AbstractStringWrapper.php | 11 +++++------ library/Zend/Stdlib/StringWrapper/Iconv.php | 11 +++++------ library/Zend/Stdlib/StringWrapper/MbString.php | 11 +++++------ .../Stdlib/StringWrapper/StringWrapperInterface.php | 4 ++-- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index eebf006fa69..2f3bc1febab 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -105,18 +105,17 @@ public function setEncoding($encoding, $convertEncoding = null) * Convert a string from one character encoding to another * * @param string $str - * @param boolean $backward + * @param boolean $reverse * @return string|false */ - public function convert($str, $backward = false) + public function convert($str, $reverse = false) { - $from = $backward ? $this->convertEncoding : $this->encoding; - $to = $backward ? $this->encoding : $this->convertEncoding; - - if ($to == $from) { + if ($this->encoding === $this->convertEncoding) { return $str; } + $from = $reverse ? $this->convertEncoding : $this->encoding; + $to = $reverse ? $this->encoding : $this->convertEncoding; throw new Exception\RuntimeException(sprintf( 'Converting from "%s" to "%s" isn\'t supported by this string wrapper', $from, diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index 946624474e8..abbecbd02de 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -185,15 +185,14 @@ public function strpos($haystack, $needle, $offset = 0) /** * Convert a string from one character encoding to another * - * @param string $str - * @param string $toEncoding - * @param string $fromEncoding + * @param string $str + * @param boolean $reverse * @return string|false */ - public function convert($str, $backword = false) + public function convert($str, $reverse = false) { - $fromEncoding = $backword ? $this->convertEncoding : $this->encoding; - $toEncoding = $backword ? $this->encoding : $this->convertEncoding; + $fromEncoding = $reverse ? $this->convertEncoding : $this->encoding; + $toEncoding = $reverse ? $this->encoding : $this->convertEncoding; return iconv($fromEncoding, $toEncoding, $str); } } diff --git a/library/Zend/Stdlib/StringWrapper/MbString.php b/library/Zend/Stdlib/StringWrapper/MbString.php index 4ae1f1b1cdc..72af7fe8478 100644 --- a/library/Zend/Stdlib/StringWrapper/MbString.php +++ b/library/Zend/Stdlib/StringWrapper/MbString.php @@ -106,15 +106,14 @@ public function strpos($haystack, $needle, $offset = 0) /** * Convert a string from one character encoding to another * - * @param string $str - * @param string $toEncoding - * @param string $fromEncoding + * @param string $str + * @param boolean $reverse * @return string|false */ - public function convert($str, $backword = false) + public function convert($str, $reverse = false) { - $fromEncoding = $backword ? $this->convertEncoding : $this->encoding; - $toEncoding = $backword ? $this->encoding : $this->convertEncoding; + $fromEncoding = $reverse ? $this->convertEncoding : $this->encoding; + $toEncoding = $reverse ? $this->encoding : $this->convertEncoding; return mb_convert_encoding($str, $toEncoding, $fromEncoding); } } diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index 35467cef073..f0251d16f91 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -84,10 +84,10 @@ public function strpos($haystack, $needle, $offset = 0); * Convert a string from one character encoding to another * * @param string $str - * @param boolean $backward + * @param boolean $reverse * @return string|false */ - public function convert($str, $backward = false); + public function convert($str, $reverse = false); /** * Wraps a string to a given number of characters From 3b8854a8cfbfe5845592d85007217a5fe1f4ca88 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 3 Jan 2013 15:45:36 +0100 Subject: [PATCH 34/51] phpdoc --- library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php | 2 +- library/Zend/Stdlib/StringWrapper/Iconv.php | 2 +- library/Zend/Stdlib/StringWrapper/MbString.php | 2 +- library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 2f3bc1febab..9613f372a4b 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -102,7 +102,7 @@ public function setEncoding($encoding, $convertEncoding = null) } /** - * Convert a string from one character encoding to another + * Convert a string from defined encoding to the defined convert encoding * * @param string $str * @param boolean $reverse diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index abbecbd02de..d1aaa03acd3 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -183,7 +183,7 @@ public function strpos($haystack, $needle, $offset = 0) } /** - * Convert a string from one character encoding to another + * Convert a string from defined encoding to the defined convert encoding * * @param string $str * @param boolean $reverse diff --git a/library/Zend/Stdlib/StringWrapper/MbString.php b/library/Zend/Stdlib/StringWrapper/MbString.php index 72af7fe8478..8d8d49a7509 100644 --- a/library/Zend/Stdlib/StringWrapper/MbString.php +++ b/library/Zend/Stdlib/StringWrapper/MbString.php @@ -104,7 +104,7 @@ public function strpos($haystack, $needle, $offset = 0) } /** - * Convert a string from one character encoding to another + * Convert a string from defined encoding to the defined convert encoding * * @param string $str * @param boolean $reverse diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index f0251d16f91..4dc133fb1c3 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -81,7 +81,7 @@ public function substr($str, $offset = 0, $length = null); public function strpos($haystack, $needle, $offset = 0); /** - * Convert a string from one character encoding to another + * Convert a string from defined encoding to the defined convert encoding * * @param string $str * @param boolean $reverse From 68b7b95b49b47c3c521041dff47d4c5700481063 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 3 Jan 2013 16:08:57 +0100 Subject: [PATCH 35/51] Added missing @deprecated --- library/Zend/Text/MultiByte.php | 1 + 1 file changed, 1 insertion(+) diff --git a/library/Zend/Text/MultiByte.php b/library/Zend/Text/MultiByte.php index c00def16bd8..bc10ae41a27 100644 --- a/library/Zend/Text/MultiByte.php +++ b/library/Zend/Text/MultiByte.php @@ -55,6 +55,7 @@ public static function wordWrap($string, $width = 75, $break = "\n", $cut = fals * @param integer $padType * @param string $charset * @return string + * @deprecated Please use Zend\Stdlib\StringUtils instead */ public static function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $charset = 'utf-8') { From 8f5292b3fe1cc362bfb268e4de406e14a39caa27 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 3 Jan 2013 16:19:24 +0100 Subject: [PATCH 36/51] The native string wrapper doesn't support to convert between encodings --- .../StringWrapper/AbstractStringWrapper.php | 3 +- library/Zend/Stdlib/StringWrapper/Native.php | 63 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 9613f372a4b..50621cd0349 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -102,7 +102,7 @@ public function setEncoding($encoding, $convertEncoding = null) } /** - * Convert a string from defined encoding to the defined convert encoding + * Convert a string from defined character encoding to the defined convert encoding * * @param string $str * @param boolean $reverse @@ -114,6 +114,7 @@ public function convert($str, $reverse = false) return $str; } + var_dump($this->encoding, $this->convertEncoding); $from = $reverse ? $this->convertEncoding : $this->encoding; $to = $reverse ? $this->encoding : $this->convertEncoding; throw new Exception\RuntimeException(sprintf( diff --git a/library/Zend/Stdlib/StringWrapper/Native.php b/library/Zend/Stdlib/StringWrapper/Native.php index 0f26fcb0844..8ddcf3d6cce 100644 --- a/library/Zend/Stdlib/StringWrapper/Native.php +++ b/library/Zend/Stdlib/StringWrapper/Native.php @@ -10,6 +10,7 @@ namespace Zend\Stdlib\StringWrapper; +use Zend\Stdlib\Exception; use Zend\Stdlib\StringUtils; /** @@ -19,6 +20,30 @@ */ class Native extends AbstractStringWrapper { + /** + * Check if the given character encoding is supported by this wrapper + * and the character encoding to convert to is also supported. + * + * @param string $encoding + * @param string|null $convertEncoding + */ + public static function isSupported($encoding, $convertEncoding = null) + { + $encodingUpper = strtoupper($encoding); + $supportedEncodings = static::getSupportedEncodings(); + + if (!in_array($encodingUpper, $supportedEncodings)) { + return false; + } + + // This adapter doesn't support to convert between encodings + if ($convertEncoding !== null && $encodingUpper !== strtoupper($convertEncoding)) { + return false; + } + + return true; + } + /** * Get a list of supported character encodings * @@ -29,6 +54,44 @@ public static function getSupportedEncodings() return StringUtils::getSingleByteEncodings(); } + /** + * Set character encoding working with and convert to + * + * @param string $encoding The character encoding to work with + * @param string|null $convertEncoding The character encoding to convert to + * @return StringWrapperInterface + */ + public function setEncoding($encoding, $convertEncoding = null) + { + $supportedEncodings = static::getSupportedEncodings(); + + $encodingUpper = strtoupper($encoding); + if (!in_array($encodingUpper, $supportedEncodings)) { + throw new Exception\InvalidArgumentException( + 'Wrapper doesn\'t support character encoding "' . $encoding . '"' + ); + } + + if ($encodingUpper !== strtoupper($convertEncoding)) { + $this->convertEncoding = $encodingUpper; + } + + if ($convertEncoding !== null) { + if ($encodingUpper !== strtoupper($convertEncoding)) { + throw new Exception\InvalidArgumentException( + 'Wrapper doesn\'t support to convert between character encodings' + ); + } + + $this->convertEncoding = $encodingUpper; + } else { + $this->convertEncoding = null; + } + $this->encoding = $encodingUpper; + + return $this; + } + /** * Returns the length of the given string * From 508db19bb2bd954f86f682af368085428d7ac3c9 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 3 Jan 2013 16:24:23 +0100 Subject: [PATCH 37/51] Added note why encodings are typed in case-mix --- tests/ZendTest/Stdlib/StringUtilsTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/ZendTest/Stdlib/StringUtilsTest.php b/tests/ZendTest/Stdlib/StringUtilsTest.php index fefed580767..7bcde696c6f 100644 --- a/tests/ZendTest/Stdlib/StringUtilsTest.php +++ b/tests/ZendTest/Stdlib/StringUtilsTest.php @@ -38,9 +38,10 @@ public function tearDown() public function getSingleByEncodings() { return array( + // case-mix to check case-insensitivity array('AscII'), - array('7bit'), - array('8bit'), + array('7bIt'), + array('8Bit'), array('ISo-8859-1'), array('ISo-8859-2'), array('ISo-8859-3'), From eede5147ccb5a1660c7fcb6e6d2e80790e28a42b Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 3 Jan 2013 23:28:04 +0100 Subject: [PATCH 38/51] added methods get[Convert]Encoding() and removed constructor from interface --- library/Zend/Stdlib/StringUtils.php | 4 +- .../StringWrapper/AbstractStringWrapper.php | 54 ++++++++++++------- library/Zend/Stdlib/StringWrapper/Iconv.php | 26 ++++++--- library/Zend/Stdlib/StringWrapper/Intl.php | 4 +- .../Zend/Stdlib/StringWrapper/MbString.php | 27 +++++++--- library/Zend/Stdlib/StringWrapper/Native.php | 8 +++ .../StringWrapper/StringWrapperInterface.php | 22 +++++--- .../Stdlib/StringWrapper/IconvTest.php | 5 +- .../Stdlib/StringWrapper/IntlTest.php | 5 +- .../Stdlib/StringWrapper/MbStringTest.php | 5 +- .../Stdlib/StringWrapper/NativeTest.php | 5 +- 11 files changed, 115 insertions(+), 50 deletions(-) diff --git a/library/Zend/Stdlib/StringUtils.php b/library/Zend/Stdlib/StringUtils.php index 031800b7031..65e6004cc6e 100644 --- a/library/Zend/Stdlib/StringUtils.php +++ b/library/Zend/Stdlib/StringUtils.php @@ -114,7 +114,9 @@ public static function getWrapper($encoding = 'UTF-8', $convertEncoding = null) { foreach (static::getRegisteredWrappers() as $wrapperClass) { if ($wrapperClass::isSupported($encoding, $convertEncoding)) { - return new $wrapperClass($encoding, $convertEncoding); + $wrapper = new $wrapperClass($encoding, $convertEncoding); + $wrapper->setEncoding($encoding, $convertEncoding); + return $wrapper; } } diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 50621cd0349..60e2730be7e 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -24,7 +24,7 @@ abstract class AbstractStringWrapper implements StringWrapperInterface * The character encoding working on * @var string|null */ - protected $encoding; + protected $encoding = 'UTF-8'; /** * An optionally character encoding to convert to @@ -54,17 +54,6 @@ public static function isSupported($encoding, $convertEncoding = null) return true; } - /** - * Constructor - * @param string $encoding Character encoding working on - * @param string|null $convertEncoding Character encoding to convert to - * @throws Exception\InvalidArgumentException - */ - public function __construct($encoding, $convertEncoding = null) - { - $this->setEncoding($encoding, $convertEncoding); - } - /** * Set character encoding working with and convert to * @@ -101,6 +90,28 @@ public function setEncoding($encoding, $convertEncoding = null) return $this; } + + /** + * Get the defined character encoding to work with + * + * @return string + * @throws Exception\LogicException If no encoding was defined + */ + public function getEncoding() + { + return $this->encoding; + } + + /** + * Get the defined character encoding to convert to + * + * @return string|null + */ + public function getConvertEncoding() + { + return $this->convertEncoding; + } + /** * Convert a string from defined character encoding to the defined convert encoding * @@ -110,13 +121,20 @@ public function setEncoding($encoding, $convertEncoding = null) */ public function convert($str, $reverse = false) { - if ($this->encoding === $this->convertEncoding) { + $encoding = $this->getEncoding(); + $convertEncoding = $this->getConvertEncoding(); + if ($convertEncoding === null) { + throw new Exception\LogicException( + 'No convert encoding defined' + ); + } + + if ($encoding === $convertEncoding) { return $str; } - var_dump($this->encoding, $this->convertEncoding); - $from = $reverse ? $this->convertEncoding : $this->encoding; - $to = $reverse ? $this->encoding : $this->convertEncoding; + $from = $reverse ? $convertEncoding : $encoding; + $to = $reverse ? $encoding : $convertEncoding; throw new Exception\RuntimeException(sprintf( 'Converting from "%s" to "%s" isn\'t supported by this string wrapper', $from, @@ -151,7 +169,7 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false) throw new Exception\InvalidArgumentException('Cannot force cut when width is zero'); } - if (StringUtils::isSingleByteEncoding($this->encoding)) { + if (StringUtils::isSingleByteEncoding($this->getEncoding())) { return wordwrap($string, $width, $break, $cut); } @@ -217,7 +235,7 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false) */ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT) { - if (StringUtils::isSingleByteEncoding($this->encoding)) { + if (StringUtils::isSingleByteEncoding($this->getEncoding())) { return str_pad($input, $padLength, $padString, $padType); } diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index d1aaa03acd3..2588dd185f5 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -131,15 +131,13 @@ public static function getSupportedEncodings() * * @throws Exception\ExtensionNotLoadedException */ - public function __construct($encoding, $convertEncoding = null) + public function __construct() { if (!extension_loaded('iconv')) { throw new Exception\ExtensionNotLoadedException( 'PHP extension "iconv" is required for this wrapper' ); } - - parent::__construct($encoding, $convertEncoding); } /** @@ -151,7 +149,7 @@ public function __construct($encoding, $convertEncoding = null) */ public function strlen($str) { - return iconv_strlen($str, $this->encoding); + return iconv_strlen($str, $this->getEncoding()); } /** @@ -165,7 +163,7 @@ public function strlen($str) */ public function substr($str, $offset = 0, $length = null) { - return iconv_substr($str, $offset, $length, $this->encoding); + return iconv_substr($str, $offset, $length, $this->getEncoding()); } /** @@ -179,7 +177,7 @@ public function substr($str, $offset = 0, $length = null) */ public function strpos($haystack, $needle, $offset = 0) { - return iconv_strpos($haystack, $needle, $offset, $this->encoding); + return iconv_strpos($haystack, $needle, $offset, $this->getEncoding()); } /** @@ -191,8 +189,20 @@ public function strpos($haystack, $needle, $offset = 0) */ public function convert($str, $reverse = false) { - $fromEncoding = $reverse ? $this->convertEncoding : $this->encoding; - $toEncoding = $reverse ? $this->encoding : $this->convertEncoding; + $encoding = $this->getEncoding(); + $convertEncoding = $this->getConvertEncoding(); + if ($convertEncoding === null) { + throw new Exception\LogicException( + 'No convert encoding defined' + ); + } + + if ($encoding === $convertEncoding) { + return $str; + } + + $fromEncoding = $reverse ? $convertEncoding : $encoding; + $toEncoding = $reverse ? $encoding : $convertEncoding; return iconv($fromEncoding, $toEncoding, $str); } } diff --git a/library/Zend/Stdlib/StringWrapper/Intl.php b/library/Zend/Stdlib/StringWrapper/Intl.php index 3669e002b61..c7eacaa2404 100644 --- a/library/Zend/Stdlib/StringWrapper/Intl.php +++ b/library/Zend/Stdlib/StringWrapper/Intl.php @@ -41,15 +41,13 @@ public static function getSupportedEncodings() * * @throws Exception\ExtensionNotLoadedException */ - public function __construct($encoding, $convertEncoding = null) + public function __construct() { if (!extension_loaded('intl')) { throw new Exception\ExtensionNotLoadedException( 'PHP extension "intl" is required for this wrapper' ); } - - parent::__construct($encoding, $convertEncoding); } /** diff --git a/library/Zend/Stdlib/StringWrapper/MbString.php b/library/Zend/Stdlib/StringWrapper/MbString.php index 8d8d49a7509..62782dab6d6 100644 --- a/library/Zend/Stdlib/StringWrapper/MbString.php +++ b/library/Zend/Stdlib/StringWrapper/MbString.php @@ -52,15 +52,13 @@ public static function getSupportedEncodings() * * @throws Exception\ExtensionNotLoadedException */ - public function __construct($encoding, $convertEncoding = null) + public function __construct() { if (!extension_loaded('mbstring')) { throw new Exception\ExtensionNotLoadedException( 'PHP extension "mbstring" is required for this wrapper' ); } - - parent::__construct($encoding, $convertEncoding); } /** @@ -72,7 +70,7 @@ public function __construct($encoding, $convertEncoding = null) */ public function strlen($str) { - return mb_strlen($str, $this->encoding); + return mb_strlen($str, $this->getEncoding()); } /** @@ -86,7 +84,7 @@ public function strlen($str) */ public function substr($str, $offset = 0, $length = null) { - return mb_substr($str, $offset, $length, $this->encoding); + return mb_substr($str, $offset, $length, $this->getEncoding()); } /** @@ -100,7 +98,7 @@ public function substr($str, $offset = 0, $length = null) */ public function strpos($haystack, $needle, $offset = 0) { - return mb_strpos($haystack, $needle, $offset, $this->encoding); + return mb_strpos($haystack, $needle, $offset, $this->getEncoding()); } /** @@ -112,8 +110,21 @@ public function strpos($haystack, $needle, $offset = 0) */ public function convert($str, $reverse = false) { - $fromEncoding = $reverse ? $this->convertEncoding : $this->encoding; - $toEncoding = $reverse ? $this->encoding : $this->convertEncoding; + $encoding = $this->getEncoding(); + $convertEncoding = $this->getConvertEncoding(); + + if ($convertEncoding === null) { + throw new Exception\LogicException( + 'No convert encoding defined' + ); + } + + if ($encoding === $convertEncoding) { + return $str; + } + + $fromEncoding = $reverse ? $convertEncoding : $encoding; + $toEncoding = $reverse ? $encoding : $convertEncoding; return mb_convert_encoding($str, $toEncoding, $fromEncoding); } } diff --git a/library/Zend/Stdlib/StringWrapper/Native.php b/library/Zend/Stdlib/StringWrapper/Native.php index 8ddcf3d6cce..375b46f2864 100644 --- a/library/Zend/Stdlib/StringWrapper/Native.php +++ b/library/Zend/Stdlib/StringWrapper/Native.php @@ -20,6 +20,14 @@ */ class Native extends AbstractStringWrapper { + /** + * The character encoding working on + * (overwritten to change defaut encoding) + * + * @var string + */ + protected $encoding = 'ASCII'; + /** * Check if the given character encoding is supported by this wrapper * and the character encoding to convert to is also supported. diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index 4dc133fb1c3..401cda3fd32 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -33,14 +33,6 @@ public static function isSupported($encoding, $convertEncoding = null); */ public static function getSupportedEncodings(); - /** - * Constructor - * - * @param string $encoding Character encoding working on - * @param string|null $convertEncoding Character encoding to convert to - */ - public function __construct($encoding, $convertEncoding = null); - /** * Set character encoding working with and convert to * @@ -50,6 +42,20 @@ public function __construct($encoding, $convertEncoding = null); */ public function setEncoding($encoding, $convertEncoding = null); + /** + * Get the defined character encoding to work with (upper case) + * + * @return string|null + */ + public function getEncoding(); + + /** + * Get the defined character encoding to convert to (upper case) + * + * @return string|null + */ + public function getConvertEncoding(); + /** * Returns the length of the given string * diff --git a/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php b/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php index 2819b6b000f..ec3b0db24d8 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/IconvTest.php @@ -41,6 +41,9 @@ protected function getWrapper($encoding = null, $convertEncoding = null) if (!Iconv::isSupported($encoding, $convertEncoding)) { return false; } - return new Iconv($encoding, $convertEncoding); + + $wrapper = new Iconv(); + $wrapper->setEncoding($encoding, $convertEncoding); + return $wrapper; } } diff --git a/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php b/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php index efa3fdddb68..d3092f60b06 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/IntlTest.php @@ -41,6 +41,9 @@ protected function getWrapper($encoding = null, $convertEncoding = null) if (!Intl::isSupported($encoding, $convertEncoding)) { return false; } - return new Intl($encoding, $convertEncoding); + + $wrapper = new Intl(); + $wrapper->setEncoding($encoding, $convertEncoding); + return $wrapper; } } diff --git a/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php b/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php index 01f332bee70..d024afaa6b0 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/MbStringTest.php @@ -41,6 +41,9 @@ protected function getWrapper($encoding = null, $convertEncoding = null) if (!MbString::isSupported($encoding, $convertEncoding)) { return false; } - return new MbString($encoding, $convertEncoding); + + $wrapper = new MbString(); + $wrapper->setEncoding($encoding, $convertEncoding); + return $wrapper; } } diff --git a/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php b/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php index 9a3716dbdfc..b54e41daad3 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/NativeTest.php @@ -26,6 +26,9 @@ protected function getWrapper($encoding = null, $convertEncoding = null) if (!Native::isSupported($encoding, $convertEncoding)) { return false; } - return new Native($encoding, $convertEncoding); + + $wrapper = new Native(); + $wrapper->setEncoding($encoding, $convertEncoding); + return $wrapper; } } From d553c4b8304b0f8d542be015a0ab7a3daccd2609 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 3 Jan 2013 23:29:49 +0100 Subject: [PATCH 39/51] fixed phpdoc --- library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index 401cda3fd32..888f9c3d67e 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -45,7 +45,7 @@ public function setEncoding($encoding, $convertEncoding = null); /** * Get the defined character encoding to work with (upper case) * - * @return string|null + * @return string */ public function getEncoding(); From 93f347c72cc3c19be0358a832a50ab97fc1168bb Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 6 Jan 2013 15:16:48 +0100 Subject: [PATCH 40/51] psr --- .../StringWrapper/AbstractStringWrapper.php | 1 - library/Zend/Stdlib/StringWrapper/Iconv.php | 149 ++++++++++++++---- 2 files changed, 117 insertions(+), 33 deletions(-) diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 60e2730be7e..8b19605ae65 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -90,7 +90,6 @@ public function setEncoding($encoding, $convertEncoding = null) return $this; } - /** * Get the defined character encoding to work with * diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index 2588dd185f5..d8131579011 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -28,83 +28,167 @@ class Iconv extends AbstractStringWrapper protected static $encodings = array( // European languages 'ASCII', - 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', 'ISO-8859-7', - 'ISO-8859-9', 'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16', - 'KOI8-R', 'KOI8-U', 'KOI8-RU', - 'CP1250', 'CP1251', 'CP1252', 'CP1253', 'CP1254', 'CP1257', 'CP850', 'CP866', 'CP1131', - 'MACROMAN', 'MACCENTRALEUROPE', 'MACICELAND', 'MACCROATIAN', 'MACROMANIA', - 'MACCYRILLIC', 'MACUKRAINE', 'MACGREEK', 'MACTURKISH', 'MACINTOSH', + 'ISO-8859-1', + 'ISO-8859-2', + 'ISO-8859-3', + 'ISO-8859-4', + 'ISO-8859-5', + 'ISO-8859-7', + 'ISO-8859-9', + 'ISO-8859-10', + 'ISO-8859-13', + 'ISO-8859-14', + 'ISO-8859-15', + 'ISO-8859-16', + 'KOI8-R', + 'KOI8-U', + 'KOI8-RU', + 'CP1250', + 'CP1251', + 'CP1252', + 'CP1253', + 'CP1254', + 'CP1257', + 'CP850', + 'CP866', + 'CP1131', + 'MACROMAN', + 'MACCENTRALEUROPE', + 'MACICELAND', + 'MACCROATIAN', + 'MACROMANIA', + 'MACCYRILLIC', + 'MACUKRAINE', + 'MACGREEK', + 'MACTURKISH', + 'MACINTOSH', // Semitic languages - 'ISO-8859-6', 'ISO-8859-8', - 'CP1255', 'CP1256', 'CP862', - 'MACHEBREW', 'MACARABIC', + 'ISO-8859-6', + 'ISO-8859-8', + 'CP1255', + 'CP1256', + 'CP862', + 'MACHEBREW', + 'MACARABIC', // Japanese - 'EUC-JP', 'SHIFT_JIS', 'CP932', - 'ISO-2022-JP', 'ISO-2022-JP-2', 'ISO-2022-JP-1', + 'EUC-JP', + 'SHIFT_JIS', + 'CP932', + 'ISO-2022-JP', + 'ISO-2022-JP-2', + 'ISO-2022-JP-1', // Chinese - 'EUC-CN', 'HZ', 'GBK', 'CP936', 'GB18030', 'EUC-TW', 'BIG5', 'CP950', - 'BIG5-HKSCS', 'BIG5-HKSCS:2004', 'BIG5-HKSCS:2001', 'BIG5-HKSCS:1999', - 'ISO-2022-CN', 'ISO-2022-CN-EXT', + 'EUC-CN', + 'HZ', + 'GBK', + 'CP936', + 'GB18030', + 'EUC-TW', + 'BIG5', + 'CP950', + 'BIG5-HKSCS', + 'BIG5-HKSCS:2004', + 'BIG5-HKSCS:2001', + 'BIG5-HKSCS:1999', + 'ISO-2022-CN', + 'ISO-2022-CN-EXT', // Korean - 'EUC-KR', 'CP949', 'ISO-2022-KR', 'JOHAB', + 'EUC-KR', + 'CP949', + 'ISO-2022-KR', + 'JOHAB', // Armenian 'ARMSCII-8', // Georgian - 'GEORGIAN-ACADEMY', 'GEORGIAN-PS', + 'GEORGIAN-ACADEMY', + 'GEORGIAN-PS', // Tajik 'KOI8-T', // Kazakh - 'PT154', 'RK1048', + 'PT154', + 'RK1048', // Thai - 'ISO-8859-11', 'TIS-620', 'CP874', 'MACTHAI', + 'ISO-8859-11', + 'TIS-620', + 'CP874', + 'MACTHAI', // Laotian - 'MULELAO-1', 'CP1133', + 'MULELAO-1', + 'CP1133', // Vietnamese - 'VISCII', 'TCVN', 'CP1258', + 'VISCII', + 'TCVN', + 'CP1258', // Platform specifics - 'HP-ROMAN8', 'NEXTSTEP', + 'HP-ROMAN8', + 'NEXTSTEP', // Full Unicode 'UTF-8', - 'UCS-2', 'UCS-2BE', 'UCS-2LE', - 'UCS-4', 'UCS-4BE', 'UCS-4LE', - 'UTF-16', 'UTF-16BE', 'UTF-16LE', - 'UTF-32', 'UTF-32BE', 'UTF-32LE', + 'UCS-2', + 'UCS-2BE', + 'UCS-2LE', + 'UCS-4', + 'UCS-4BE', + 'UCS-4LE', + 'UTF-16', + 'UTF-16BE', + 'UTF-16LE', + 'UTF-32', + 'UTF-32BE', + 'UTF-32LE', 'UTF-7', - 'C99', 'JAVA', + 'C99', + 'JAVA', // Full Unicode, in terms of uint16_t or uint32_t (with machine dependent endianness and alignment) - // 'UCS-2-INTERNAL', 'UCS-4-INTERNAL', + // 'UCS-2-INTERNAL', + // 'UCS-4-INTERNAL', // Locale dependent, in terms of `char' or `wchar_t' (with machine dependent endianness and alignment, // and with OS and locale dependent semantics) - // 'char', 'wchar_t', + // 'char', + // 'wchar_t', // '', // The empty encoding name is equivalent to "char": it denotes the locale dependent character encoding. // When configured with the option --enable-extra-encodings, // it also provides support for a few extra encodings: // European languages - 'CP437', 'CP737', 'CP775', 'CP852', 'CP853', 'CP855', 'CP857', 'CP858', - 'CP860', 'CP861', 'CP863', 'CP865', 'CP869', 'CP1125', + 'CP437', + 'CP737', + 'CP775', + 'CP852', + 'CP853', + 'CP855', + 'CP857', + 'CP858', + 'CP860', + 'CP861', + 'CP863', + 'CP865', + 'CP869', + 'CP1125', // Semitic languages 'CP864', // Japanese - 'EUC-JISX0213', 'Shift_JISX0213', 'ISO-2022-JP-3', + 'EUC-JISX0213', + 'Shift_JISX0213', + 'ISO-2022-JP-3', // Chinese 'BIG5-2003', // (experimental) @@ -113,7 +197,8 @@ class Iconv extends AbstractStringWrapper 'TDS565', // Platform specifics - 'ATARIST', 'RISCOS-LATIN1', + 'ATARIST', + 'RISCOS-LATIN1', ); /** From 000818fc758a65441cfcb53ec0957c9918254a64 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 6 Jan 2013 15:21:56 +0100 Subject: [PATCH 41/51] added StringUtils::resetRegisteredWrappers() for testing purposes --- library/Zend/Stdlib/StringUtils.php | 12 +++++++++++- tests/ZendTest/Stdlib/StringUtilsTest.php | 17 +---------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/library/Zend/Stdlib/StringUtils.php b/library/Zend/Stdlib/StringUtils.php index 65e6004cc6e..47e3626926d 100644 --- a/library/Zend/Stdlib/StringUtils.php +++ b/library/Zend/Stdlib/StringUtils.php @@ -29,7 +29,7 @@ abstract class StringUtils * * @var StringWrapperInterface[] */ - protected static $wrapperRegistry; + protected static $wrapperRegistry = null; /** * A list of known single-byte character encodings (upper-case) @@ -101,6 +101,16 @@ public static function unregisterWrapper($wrapper) } } + /** + * Reset all registered wrappers so the default wrappers will be used + * + * @return void + */ + public static function resetRegisteredWrappers() + { + static::$wrapperRegistry = null; + } + /** * Get the first string wrapper supporting the given character encoding * and supports to convert into the given convert encoding. diff --git a/tests/ZendTest/Stdlib/StringUtilsTest.php b/tests/ZendTest/Stdlib/StringUtilsTest.php index 7bcde696c6f..bb27a59bf5b 100644 --- a/tests/ZendTest/Stdlib/StringUtilsTest.php +++ b/tests/ZendTest/Stdlib/StringUtilsTest.php @@ -15,24 +15,9 @@ class StringUtilsTest extends TestCase { - - protected $bufferedWrappers; - - public function setUp() - { - $this->bufferedWrappers = StringUtils::getRegisteredWrappers(); - } - public function tearDown() { - // reset registered wrappers - foreach (StringUtils::getRegisteredWrappers() as $wrapper) { - StringUtils::unregisterWrapper($wrapper); - } - foreach ($this->bufferedWrappers as $wrapper) { - StringUtils::registerWrapper($wrapper); - } - + StringUtils::resetRegisteredWrappers(); } public function getSingleByEncodings() From fe08ee1e61507e3338f38ee8824f358111ac7bb0 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 6 Jan 2013 15:24:40 +0100 Subject: [PATCH 42/51] Global namespace not needed for constants --- library/Zend/ProgressBar/Adapter/Console.php | 2 +- library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php | 6 +++--- .../Zend/Stdlib/StringWrapper/StringWrapperInterface.php | 2 +- library/Zend/Text/MultiByte.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/library/Zend/ProgressBar/Adapter/Console.php b/library/Zend/ProgressBar/Adapter/Console.php index 50a52e3da86..42eaacf87f5 100644 --- a/library/Zend/ProgressBar/Adapter/Console.php +++ b/library/Zend/ProgressBar/Adapter/Console.php @@ -443,7 +443,7 @@ public function notify($current, $max, $percent, $timeTaken, $timeRemaining, $te substr($text, 0, $this->textWidth), $this->textWidth, ' ', - \STR_PAD_RIGHT + STR_PAD_RIGHT ); break; } diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 8b19605ae65..5fb8e85b54c 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -232,7 +232,7 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false) * @param integer $padType * @return string */ - public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT) + public function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT) { if (StringUtils::isSingleByteEncoding($this->getEncoding())) { return str_pad($input, $padLength, $padString, $padType); @@ -250,7 +250,7 @@ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD $repeatCount = floor($lengthOfPadding / $padStringLength); - if ($padType === \STR_PAD_BOTH) { + if ($padType === STR_PAD_BOTH) { $lastStringLeft = ''; $lastStringRight = ''; $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2; @@ -269,7 +269,7 @@ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength); - if ($padType === \STR_PAD_LEFT) { + if ($padType === STR_PAD_LEFT) { return str_repeat($padString, $repeatCount) . $lastString . $input; } diff --git a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php index 888f9c3d67e..bcba9ec48e5 100644 --- a/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php +++ b/library/Zend/Stdlib/StringWrapper/StringWrapperInterface.php @@ -115,5 +115,5 @@ public function wordWrap($str, $width = 75, $break = "\n", $cut = false); * @param integer $padType * @return string */ - public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT); + public function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT); } diff --git a/library/Zend/Text/MultiByte.php b/library/Zend/Text/MultiByte.php index bc10ae41a27..1be27f7e47a 100644 --- a/library/Zend/Text/MultiByte.php +++ b/library/Zend/Text/MultiByte.php @@ -57,7 +57,7 @@ public static function wordWrap($string, $width = 75, $break = "\n", $cut = fals * @return string * @deprecated Please use Zend\Stdlib\StringUtils instead */ - public static function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD_RIGHT, $charset = 'utf-8') + public static function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT, $charset = 'utf-8') { trigger_error(sprintf( "This method is deprecated, please use '%s' instead", From 993d1903a8c69a27f8b1e9829e5888fdd3c370c8 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 6 Jan 2013 15:28:10 +0100 Subject: [PATCH 43/51] just use $this->encoding --- library/Zend/Feed/Writer/Extension/ITunes/Entry.php | 2 +- library/Zend/Feed/Writer/Extension/ITunes/Feed.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/Zend/Feed/Writer/Extension/ITunes/Entry.php b/library/Zend/Feed/Writer/Extension/ITunes/Entry.php index 6384c0a452b..72c703c7f14 100644 --- a/library/Zend/Feed/Writer/Extension/ITunes/Entry.php +++ b/library/Zend/Feed/Writer/Extension/ITunes/Entry.php @@ -44,7 +44,7 @@ class Entry public function __construct() { - $this->stringWrapper = StringUtils::getWrapper($this->getEncoding()); + $this->stringWrapper = StringUtils::getWrapper($this->encoding); } /** diff --git a/library/Zend/Feed/Writer/Extension/ITunes/Feed.php b/library/Zend/Feed/Writer/Extension/ITunes/Feed.php index ac6e27bfcfa..30b83fe301d 100644 --- a/library/Zend/Feed/Writer/Extension/ITunes/Feed.php +++ b/library/Zend/Feed/Writer/Extension/ITunes/Feed.php @@ -47,7 +47,7 @@ class Feed */ public function __construct() { - $this->stringWrapper = StringUtils::getWrapper($this->getEncoding()); + $this->stringWrapper = StringUtils::getWrapper($this->encoding); } /** From c1745cb163df19443cf3a4aa8019759e1adbea71 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 6 Jan 2013 15:31:14 +0100 Subject: [PATCH 44/51] File and class level docblocks --- .../Exception/ExtensionNotLoadedException.php | 15 +++++++++++++++ .../Zend/Stdlib/Exception/RuntimeException.php | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/library/Zend/Stdlib/Exception/ExtensionNotLoadedException.php b/library/Zend/Stdlib/Exception/ExtensionNotLoadedException.php index 40ceaf4ee32..2270b5e00b8 100644 --- a/library/Zend/Stdlib/Exception/ExtensionNotLoadedException.php +++ b/library/Zend/Stdlib/Exception/ExtensionNotLoadedException.php @@ -1,7 +1,22 @@ Date: Sun, 6 Jan 2013 15:34:47 +0100 Subject: [PATCH 45/51] Removed not neccessary variable comversion as there is no strict comparison on it --- library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php | 1 - 1 file changed, 1 deletion(-) diff --git a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php index 5fb8e85b54c..933482c73fa 100644 --- a/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php +++ b/library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php @@ -163,7 +163,6 @@ public function wordWrap($string, $width = 75, $break = "\n", $cut = false) } $width = (int) $width; - $cut = (bool) $cut; if ($width === 0 && $cut) { throw new Exception\InvalidArgumentException('Cannot force cut when width is zero'); } From db71302af3f2cdd0830a5d8bbab17d1b42d17caa Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 6 Jan 2013 16:03:05 +0100 Subject: [PATCH 46/51] Added short describtions on tests using data providers --- .../StringWrapper/CommonStringWrapperTest.php | 120 ++++++++++++------ 1 file changed, 80 insertions(+), 40 deletions(-) diff --git a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php index 5ae04708ab1..cc58f9a7062 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php @@ -138,77 +138,107 @@ public function wordWrapProvider() { return array( // Standard cut tests - array('utf-8', 'äbüöcß', 2, ' ', true, + array('Word wrap cut single-line', + 'utf-8', 'äbüöcß', 2, ' ', true, 'äb üö cß'), - array('utf-8', 'äbüöc ß äbüöcß', 2, ' ', true, + array('Word wrap cut multi-line', + 'utf-8', 'äbüöc ß äbüöcß', 2, ' ', true, 'äb üö c ß äb üö cß'), - array('utf-8', 'Ä very long wöööööööööööörd.', 8, "\n", true, + array('Word wrap cut multi-line short words', + 'utf-8', 'Ä very long wöööööööööööörd.', 8, "\n", true, "Ä very\nlong\nwööööööö\nööööörd."), - array('utf-8', "Ä very\nlong wöööööööööööörd.", 8, "\n", false, + array('Word wrap cut multi-line with previous new lines', + 'utf-8', "Ä very\nlong wöööööööööööörd.", 8, "\n", false, "Ä very\nlong\nwöööööööööööörd."), - array('utf-8', "Ä very
long wöö
öööööööö
öörd.", 8, '
', false, + array('Word wrap long break', + 'utf-8', "Ä very
long wöö
öööööööö
öörd.", 8, '
', false, "Ä very
long wöö
öööööööö
öörd."), // Alternative cut tests - array('utf-8', ' äüöäöü', 3, ' ', true, + array('Word wrap cut beginning single space', + 'utf-8', ' äüöäöü', 3, ' ', true, ' äüö äöü'), - array('utf-8', 'äüöäöü ', 3, ' ', true, + array('Word wrap cut ending single space', + 'utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '), - array('utf-8', 'äöüäöü ', 3, '-', true, + array('Word wrap cut ending single space with non space divider', + 'utf-8', 'äöüäöü ', 3, '-', true, 'äöü-äöü-'), - array('utf-8', 'äüöäöü ', 3, ' ', true, + array('Word wrap cut ending two spaces', + 'utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '), - array('utf-8', '12345 ', 5, '-', false, + array('Word wrap no cut ending single space', + 'utf-8', '12345 ', 5, '-', false, '12345-'), - array('utf-8', '12345 ', 5, '-', false, + array('Word wrap no cut ending two spaces', + 'utf-8', '12345 ', 5, '-', false, '12345- '), - array('utf-8', 'äüöäöü ', 3, ' ', true, + array('Word wrap cut ending three spaces', + 'utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '), - array('utf-8', 'äüöäöü--', 3, '-', true, + array('Word wrap cut ending two breaks', + 'utf-8', 'äüöäöü--', 3, '-', true, 'äüö-äöü--'), - array('utf-8', "äbü\töcß", 3, ' ', true, + array('Word wrap cut tab', + 'utf-8', "äbü\töcß", 3, ' ', true, "äbü \töc ß"), - array('utf-8', "äbü\nößt", 3, ' ', true, + array('Word wrap cut new-line with space', + 'utf-8', "äbü\nößt", 3, ' ', true, "äbü \nöß t"), - array('utf-8', "äbü\nößte", 3, "\n", true, + array('Word wrap cut new-line with new-line', + 'utf-8', "äbü\nößte", 3, "\n", true, "äbü\nößt\ne"), // Break cut tests - array('ascii', 'foobar-foofoofoo', 8, '-', true, + array('Word wrap cut break before', + 'ascii', 'foobar-foofoofoo', 8, '-', true, 'foobar-foofoofo-o'), - array('ascii', 'foobar-foobar', 6, '-', true, + array('Word wrap cut break with', + 'ascii', 'foobar-foobar', 6, '-', true, 'foobar-foobar'), - array('ascii', 'foobar-foobar', 7, '-', true, + array('Word wrap cut break within', + 'ascii', 'foobar-foobar', 7, '-', true, 'foobar-foobar'), - array('ascii', 'foobar-', 7, '-', true, + array('Word wrap cut break within end', + 'ascii', 'foobar-', 7, '-', true, 'foobar-'), - array('ascii', 'foobar-foobar', 5, '-', true, + array('Word wrap cut break after', + 'ascii', 'foobar-foobar', 5, '-', true, 'fooba-r-fooba-r'), // Standard no-cut tests - array('utf-8', 'äbüöcß', 2, ' ', false, + array('Word wrap no cut single-line', + 'utf-8', 'äbüöcß', 2, ' ', false, 'äbüöcß'), - array('utf-8', 'äbüöc ß äbüöcß', 2, "\n", false, + array('Word wrap no cut multi-line', + 'utf-8', 'äbüöc ß äbüöcß', 2, "\n", false, "äbüöc\nß\näbüöcß"), - array('utf-8', 'äöü äöü äöü', 5, "\n", false, + array('Word wrap no cut multi-word', + 'utf-8', 'äöü äöü äöü', 5, "\n", false, "äöü\näöü\näöü"), // Break no-cut tests - array('ascii', 'foobar-foofoofoo', 8, '-', false, + array('Word wrap no cut break before', + 'ascii', 'foobar-foofoofoo', 8, '-', false, 'foobar-foofoofoo'), - array('ascii', 'foobar-foobar', 6, '-', false, + array('Word wrap no cut break with', + 'ascii', 'foobar-foobar', 6, '-', false, 'foobar-foobar'), - array('ascii', 'foobar-foobar', 7, '-', false, + array('Word wrap no cut break within', + 'ascii', 'foobar-foobar', 7, '-', false, 'foobar-foobar'), - array('ascii', 'foobar-', 7, '-', false, + array('Word wrap no cut break within end', + 'ascii', 'foobar-', 7, '-', false, 'foobar-'), - array('ascii', 'foobar-foobar', 5, '-', false, + array('Word wrap no cut break after', + 'ascii', 'foobar-foobar', 5, '-', false, 'foobar-foobar'), ); } /** * @dataProvider wordWrapProvider + * @param string $shortDesc * @param string $encoding * @param string $str * @param integer $width @@ -216,7 +246,7 @@ public function wordWrapProvider() * @param boolean $cut * @param mixed $expected */ - public function testWordWrap($encoding, $string, $width, $break, $cut, $expected) + public function testWordWrap($shortDesc, $encoding, $string, $width, $break, $cut, $expected) { $wrapper = $this->getWrapper($encoding); if (!$wrapper) { @@ -245,24 +275,34 @@ public function strPadProvider() { return array( // single-byte - array('ascii', 'aaa', 5, 'o', STR_PAD_LEFT, 'ooaaa'), - array('ascii', 'aaa', 6, 'o', STR_PAD_BOTH, 'oaaaoo'), - array('ascii', 'aaa', 5, 'o', STR_PAD_RIGHT, 'aaaoo'), + array('Left padding - single byte', + 'ascii', 'aaa', 5, 'o', STR_PAD_LEFT, 'ooaaa'), + array('Center padding - single byte', + 'ascii', 'aaa', 6, 'o', STR_PAD_BOTH, 'oaaaoo'), + array('Right padding - single byte', + 'ascii', 'aaa', 5, 'o', STR_PAD_RIGHT, 'aaaoo'), // multi-byte - array('utf-8', 'äää', 5, 'ö', STR_PAD_LEFT, 'ööäää'), - array('utf-8', 'äää', 6, 'ö', STR_PAD_BOTH, 'öäääöö'), - array('utf-8', 'äää', 5, 'ö', STR_PAD_RIGHT, 'äääöö'), + array('Left padding - multi-byte', + 'utf-8', 'äää', 5, 'ö', STR_PAD_LEFT, 'ööäää'), + array('Center padding - multi byte', + 'utf-8', 'äää', 6, 'ö', STR_PAD_BOTH, 'öäääöö'), + array('Right padding - multi-byte', + 'utf-8', 'äää', 5, 'ö', STR_PAD_RIGHT, 'äääöö'), // ZF-12186 - array('utf-8', 'äääöö', 2, 'ö', STR_PAD_RIGHT, 'äääöö'), // PadInputLongerThanPadLength - array('utf-8', 'äääöö', 5, 'ö', STR_PAD_RIGHT, 'äääöö'), // PadInputSameAsPadLength - array('utf-8', 'äääöö', -2, 'ö', STR_PAD_RIGHT, 'äääöö'), // PadNegativePadLength + array('Input longer than pad length', + 'utf-8', 'äääöö', 2, 'ö', STR_PAD_RIGHT, 'äääöö'), + array('Input same as pad length', + 'utf-8', 'äääöö', 5, 'ö', STR_PAD_RIGHT, 'äääöö'), + array('Negative pad length', + 'utf-8', 'äääöö', -2, 'ö', STR_PAD_RIGHT, 'äääöö'), ); } /** * @dataProvider strPadProvider + * @param string $shortDesc * @param string $encoding * @param string $input * @param integer $padLength @@ -272,7 +312,7 @@ public function strPadProvider() * * @group ZF-12186 */ - public function testStrPad($encoding, $input, $padLength, $padString, $padType, $expected) + public function testStrPad($shortDesc, $encoding, $input, $padLength, $padString, $padType, $expected) { $wrapper = $this->getWrapper($encoding); if (!$wrapper) { From 62e6856a2688ac8844c9f82727c3ff031bd9da07 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 6 Jan 2013 18:22:32 +0100 Subject: [PATCH 47/51] added comment for a commented out block --- library/Zend/Stdlib/StringWrapper/Iconv.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index d8131579011..118b5e082d8 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -153,15 +153,17 @@ class Iconv extends AbstractStringWrapper 'C99', 'JAVA', + /* Commented out because that's internal encodings not existing in real world // Full Unicode, in terms of uint16_t or uint32_t (with machine dependent endianness and alignment) - // 'UCS-2-INTERNAL', - // 'UCS-4-INTERNAL', + 'UCS-2-INTERNAL', + 'UCS-4-INTERNAL', // Locale dependent, in terms of `char' or `wchar_t' (with machine dependent endianness and alignment, // and with OS and locale dependent semantics) - // 'char', - // 'wchar_t', - // '', // The empty encoding name is equivalent to "char": it denotes the locale dependent character encoding. + 'char', + 'wchar_t', + '', // The empty encoding name is equivalent to "char": it denotes the locale dependent character encoding. + */ // When configured with the option --enable-extra-encodings, // it also provides support for a few extra encodings: From 44cf1a4d8a4bf0b3c52ffb572d1b31327903f3fe Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 6 Jan 2013 20:44:51 +0100 Subject: [PATCH 48/51] Use array keys as description for data provider --- .../StringWrapper/CommonStringWrapperTest.php | 190 ++++++++---------- 1 file changed, 81 insertions(+), 109 deletions(-) diff --git a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php index cc58f9a7062..454809b33ad 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php @@ -138,107 +138,80 @@ public function wordWrapProvider() { return array( // Standard cut tests - array('Word wrap cut single-line', - 'utf-8', 'äbüöcß', 2, ' ', true, - 'äb üö cß'), - array('Word wrap cut multi-line', - 'utf-8', 'äbüöc ß äbüöcß', 2, ' ', true, - 'äb üö c ß äb üö cß'), - array('Word wrap cut multi-line short words', - 'utf-8', 'Ä very long wöööööööööööörd.', 8, "\n", true, - "Ä very\nlong\nwööööööö\nööööörd."), - array('Word wrap cut multi-line with previous new lines', - 'utf-8', "Ä very\nlong wöööööööööööörd.", 8, "\n", false, - "Ä very\nlong\nwöööööööööööörd."), - array('Word wrap long break', - 'utf-8', "Ä very
long wöö
öööööööö
öörd.", 8, '
', false, - "Ä very
long wöö
öööööööö
öörd."), + 'word-wrap-cut-single-line' => + array('utf-8', 'äbüöcß', 2, ' ', true, 'äb üö cß'), + 'word-wrap-cut-multi-line' => + array('utf-8', 'äbüöc ß äbüöcß', 2, ' ', true, 'äb üö c ß äb üö cß'), + 'word-wrap-cut-multi-line-short-words' => + array('utf-8', 'Ä very long wöööööööööööörd.', 8, "\n", true, + "Ä very\nlong\nwööööööö\nööööörd."), + 'word-wrap-cut-multi-line-with-previous-new-lines' => + array('utf-8', "Ä very\nlong wöööööööööööörd.", 8, "\n", false, + "Ä very\nlong\nwöööööööööööörd."), + 'word-wrap-long-break' => + array('utf-8', "Ä very
long wöö
öööööööö
öörd.", 8, '
', false, + "Ä very
long wöö
öööööööö
öörd."), // Alternative cut tests - array('Word wrap cut beginning single space', - 'utf-8', ' äüöäöü', 3, ' ', true, - ' äüö äöü'), - array('Word wrap cut ending single space', - 'utf-8', 'äüöäöü ', 3, ' ', true, - 'äüö äöü '), - array('Word wrap cut ending single space with non space divider', - 'utf-8', 'äöüäöü ', 3, '-', true, - 'äöü-äöü-'), - array('Word wrap cut ending two spaces', - 'utf-8', 'äüöäöü ', 3, ' ', true, - 'äüö äöü '), - array('Word wrap no cut ending single space', - 'utf-8', '12345 ', 5, '-', false, - '12345-'), - array('Word wrap no cut ending two spaces', - 'utf-8', '12345 ', 5, '-', false, - '12345- '), - array('Word wrap cut ending three spaces', - 'utf-8', 'äüöäöü ', 3, ' ', true, - 'äüö äöü '), - array('Word wrap cut ending two breaks', - 'utf-8', 'äüöäöü--', 3, '-', true, - 'äüö-äöü--'), - array('Word wrap cut tab', - 'utf-8', "äbü\töcß", 3, ' ', true, - "äbü \töc ß"), - array('Word wrap cut new-line with space', - 'utf-8', "äbü\nößt", 3, ' ', true, - "äbü \nöß t"), - array('Word wrap cut new-line with new-line', - 'utf-8', "äbü\nößte", 3, "\n", true, - "äbü\nößt\ne"), + 'word-wrap-cut-beginning-single-space' => + array('utf-8', ' äüöäöü', 3, ' ', true, ' äüö äöü'), + 'word-wrap-cut-ending-single-space' => + array('utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '), + 'word-wrap-cut-ending-single-space-with-non-space-divider' => + array('utf-8', 'äöüäöü ', 3, '-', true, 'äöü-äöü-'), + 'word-wrap-cut-ending-two-spaces' => + array('utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '), + 'word-wrap-no-cut-ending-single-space' => + array('utf-8', '12345 ', 5, '-', false, '12345-'), + 'word-wrap-no-cut-ending-two-spaces' => + array('utf-8', '12345 ', 5, '-', false, '12345- '), + 'word-wrap-cut-ending-three-spaces' => + array('utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '), + 'word-wrap-cut-ending-two-breaks' => + array('utf-8', 'äüöäöü--', 3, '-', true, 'äüö-äöü--'), + 'word-wrap-cut-tab' => + array('utf-8', "äbü\töcß", 3, ' ', true, "äbü \töc ß"), + 'word-wrap-cut-new-line-with-space' => + array('utf-8', "äbü\nößt", 3, ' ', true, "äbü \nöß t"), + 'word-wrap-cut-new-line-with-new-line' => + array('utf-8', "äbü\nößte", 3, "\n", true, "äbü\nößt\ne"), // Break cut tests - array('Word wrap cut break before', - 'ascii', 'foobar-foofoofoo', 8, '-', true, - 'foobar-foofoofo-o'), - array('Word wrap cut break with', - 'ascii', 'foobar-foobar', 6, '-', true, - 'foobar-foobar'), - array('Word wrap cut break within', - 'ascii', 'foobar-foobar', 7, '-', true, - 'foobar-foobar'), - array('Word wrap cut break within end', - 'ascii', 'foobar-', 7, '-', true, - 'foobar-'), - array('Word wrap cut break after', - 'ascii', 'foobar-foobar', 5, '-', true, - 'fooba-r-fooba-r'), + 'word-wrap-cut-break-before' => + array('ascii', 'foobar-foofoofoo', 8, '-', true, 'foobar-foofoofo-o'), + 'word-wrap-cut-break-with' => + array('ascii', 'foobar-foobar', 6, '-', true, 'foobar-foobar'), + 'word-wrap-cut-break-within' => + array('ascii', 'foobar-foobar', 7, '-', true, 'foobar-foobar'), + 'word-wrap-cut-break-within-end' => + array('ascii', 'foobar-', 7, '-', true, 'foobar-'), + 'word-wrap-cut-break-after' => + array('ascii', 'foobar-foobar', 5, '-', true, 'fooba-r-fooba-r'), // Standard no-cut tests - array('Word wrap no cut single-line', - 'utf-8', 'äbüöcß', 2, ' ', false, - 'äbüöcß'), - array('Word wrap no cut multi-line', - 'utf-8', 'äbüöc ß äbüöcß', 2, "\n", false, - "äbüöc\nß\näbüöcß"), - array('Word wrap no cut multi-word', - 'utf-8', 'äöü äöü äöü', 5, "\n", false, - "äöü\näöü\näöü"), + 'word-wrap-no-cut-single-line' => + array('utf-8', 'äbüöcß', 2, ' ', false, 'äbüöcß'), + 'word-wrap-no-cut-multi-line' => + array('utf-8', 'äbüöc ß äbüöcß', 2, "\n", false, "äbüöc\nß\näbüöcß"), + 'word-wrap-no-cut-multi-word' => + array('utf-8', 'äöü äöü äöü', 5, "\n", false, "äöü\näöü\näöü"), // Break no-cut tests - array('Word wrap no cut break before', - 'ascii', 'foobar-foofoofoo', 8, '-', false, - 'foobar-foofoofoo'), - array('Word wrap no cut break with', - 'ascii', 'foobar-foobar', 6, '-', false, - 'foobar-foobar'), - array('Word wrap no cut break within', - 'ascii', 'foobar-foobar', 7, '-', false, - 'foobar-foobar'), - array('Word wrap no cut break within end', - 'ascii', 'foobar-', 7, '-', false, - 'foobar-'), - array('Word wrap no cut break after', - 'ascii', 'foobar-foobar', 5, '-', false, - 'foobar-foobar'), + 'word-wrap-no-cut-break-before' => + array('ascii', 'foobar-foofoofoo', 8, '-', false, 'foobar-foofoofoo'), + 'word-wrap-no-cut-break-with' => + array('ascii', 'foobar-foobar', 6, '-', false, 'foobar-foobar'), + 'word-wrap-no-cut-break-within' => + array('ascii', 'foobar-foobar', 7, '-', false, 'foobar-foobar'), + 'word-wrap-no-cut-break-within-end' => + array('ascii', 'foobar-', 7, '-', false, 'foobar-'), + 'word-wrap-no-cut-break-after' => + array('ascii', 'foobar-foobar', 5, '-', false, 'foobar-foobar'), ); } /** * @dataProvider wordWrapProvider - * @param string $shortDesc * @param string $encoding * @param string $str * @param integer $width @@ -246,7 +219,7 @@ public function wordWrapProvider() * @param boolean $cut * @param mixed $expected */ - public function testWordWrap($shortDesc, $encoding, $string, $width, $break, $cut, $expected) + public function testWordWrap($encoding, $string, $width, $break, $cut, $expected) { $wrapper = $this->getWrapper($encoding); if (!$wrapper) { @@ -275,34 +248,33 @@ public function strPadProvider() { return array( // single-byte - array('Left padding - single byte', - 'ascii', 'aaa', 5, 'o', STR_PAD_LEFT, 'ooaaa'), - array('Center padding - single byte', - 'ascii', 'aaa', 6, 'o', STR_PAD_BOTH, 'oaaaoo'), - array('Right padding - single byte', - 'ascii', 'aaa', 5, 'o', STR_PAD_RIGHT, 'aaaoo'), + 'left-padding_single-byte' => + array('ascii', 'aaa', 5, 'o', STR_PAD_LEFT, 'ooaaa'), + 'center-padding_single-byte' => + array('ascii', 'aaa', 6, 'o', STR_PAD_BOTH, 'oaaaoo'), + 'right-padding_single-byte' => + array('ascii', 'aaa', 5, 'o', STR_PAD_RIGHT, 'aaaoo'), // multi-byte - array('Left padding - multi-byte', - 'utf-8', 'äää', 5, 'ö', STR_PAD_LEFT, 'ööäää'), - array('Center padding - multi byte', - 'utf-8', 'äää', 6, 'ö', STR_PAD_BOTH, 'öäääöö'), - array('Right padding - multi-byte', - 'utf-8', 'äää', 5, 'ö', STR_PAD_RIGHT, 'äääöö'), + 'left-padding_multi-byte' => + array('utf-8', 'äää', 5, 'ö', STR_PAD_LEFT, 'ööäää'), + 'center-padding_multi-byte' => + array('utf-8', 'äää', 6, 'ö', STR_PAD_BOTH, 'öäääöö'), + 'right-padding_multi-byte' => + array('utf-8', 'äää', 5, 'ö', STR_PAD_RIGHT, 'äääöö'), // ZF-12186 - array('Input longer than pad length', - 'utf-8', 'äääöö', 2, 'ö', STR_PAD_RIGHT, 'äääöö'), - array('Input same as pad length', - 'utf-8', 'äääöö', 5, 'ö', STR_PAD_RIGHT, 'äääöö'), - array('Negative pad length', - 'utf-8', 'äääöö', -2, 'ö', STR_PAD_RIGHT, 'äääöö'), + 'input-longer-than-pad-length' => + array('utf-8', 'äääöö', 2, 'ö', STR_PAD_RIGHT, 'äääöö'), + 'input-same-as-pad-length' => + array('utf-8', 'äääöö', 5, 'ö', STR_PAD_RIGHT, 'äääöö'), + 'negative-pad-length' => + array('utf-8', 'äääöö', -2, 'ö', STR_PAD_RIGHT, 'äääöö'), ); } /** * @dataProvider strPadProvider - * @param string $shortDesc * @param string $encoding * @param string $input * @param integer $padLength @@ -312,7 +284,7 @@ public function strPadProvider() * * @group ZF-12186 */ - public function testStrPad($shortDesc, $encoding, $input, $padLength, $padString, $padType, $expected) + public function testStrPad($encoding, $input, $padLength, $padString, $padType, $expected) { $wrapper = $this->getWrapper($encoding); if (!$wrapper) { From a66dadfda0b4bd2b413d2729859c2de851305530 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 6 Jan 2013 22:11:45 +0100 Subject: [PATCH 49/51] There is no encoding argument of strlen --- .../Feed/Writer/Extension/ITunes/Entry.php | 10 +++++----- .../Feed/Writer/Extension/ITunes/Feed.php | 20 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/library/Zend/Feed/Writer/Extension/ITunes/Entry.php b/library/Zend/Feed/Writer/Extension/ITunes/Entry.php index 72c703c7f14..c39c28e8903 100644 --- a/library/Zend/Feed/Writer/Extension/ITunes/Entry.php +++ b/library/Zend/Feed/Writer/Extension/ITunes/Entry.php @@ -84,7 +84,7 @@ public function setItunesBlock($value) . ' contain alphabetic characters'); } - if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only' . ' contain a maximum of 255 characters'); } @@ -114,7 +114,7 @@ public function addItunesAuthors(array $values) */ public function addItunesAuthor($value) { - if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "author" may only' . ' contain a maximum of 255 characters each'); } @@ -178,7 +178,7 @@ public function setItunesKeywords(array $value) } $concat = implode(',', $value); - if ($this->stringWrapper->strlen($concat, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($concat) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only' . ' have a concatenated length of 255 chars where terms are delimited' . ' by a comma'); @@ -196,7 +196,7 @@ public function setItunesKeywords(array $value) */ public function setItunesSubtitle($value) { - if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "subtitle" may only' . ' contain a maximum of 255 characters'); } @@ -213,7 +213,7 @@ public function setItunesSubtitle($value) */ public function setItunesSummary($value) { - if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 4000) { + if ($this->stringWrapper->strlen($value) > 4000) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "summary" may only' . ' contain a maximum of 4000 characters'); } diff --git a/library/Zend/Feed/Writer/Extension/ITunes/Feed.php b/library/Zend/Feed/Writer/Extension/ITunes/Feed.php index 30b83fe301d..59819f86171 100644 --- a/library/Zend/Feed/Writer/Extension/ITunes/Feed.php +++ b/library/Zend/Feed/Writer/Extension/ITunes/Feed.php @@ -86,7 +86,7 @@ public function setItunesBlock($value) throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only' . ' contain alphabetic characters'); } - if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only' . ' contain a maximum of 255 characters'); } @@ -117,7 +117,7 @@ public function addItunesAuthors(array $values) */ public function addItunesAuthor($value) { - if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "author" may only' . ' contain a maximum of 255 characters each'); } @@ -142,19 +142,19 @@ public function setItunesCategories(array $values) } foreach ($values as $key=>$value) { if (!is_array($value)) { - if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "category" may only' . ' contain a maximum of 255 characters each'); } $this->data['categories'][] = $value; } else { - if ($this->stringWrapper->strlen($key, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($key) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "category" may only' . ' contain a maximum of 255 characters each'); } $this->data['categories'][$key] = array(); foreach ($value as $val) { - if ($this->stringWrapper->strlen($val, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($val) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "category" may only' . ' contain a maximum of 255 characters each'); } @@ -239,7 +239,7 @@ public function setItunesKeywords(array $value) . ' contain a maximum of 12 terms'); } $concat = implode(',', $value); - if ($this->stringWrapper->strlen($concat, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($concat) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only' . ' have a concatenated length of 255 chars where terms are delimited' . ' by a comma'); @@ -292,8 +292,8 @@ public function addItunesOwner(array $value) throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "owner" must' . ' be an array containing keys "name" and "email"'); } - if ($this->stringWrapper->strlen($value['name'], $this->getEncoding()) > 255 - || $this->stringWrapper->strlen($value['email'], $this->getEncoding()) > 255 + if ($this->stringWrapper->strlen($value['name']) > 255 + || $this->stringWrapper->strlen($value['email']) > 255 ) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "owner" may only' . ' contain a maximum of 255 characters each for "name" and "email"'); @@ -314,7 +314,7 @@ public function addItunesOwner(array $value) */ public function setItunesSubtitle($value) { - if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 255) { + if ($this->stringWrapper->strlen($value) > 255) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "subtitle" may only' . ' contain a maximum of 255 characters'); } @@ -331,7 +331,7 @@ public function setItunesSubtitle($value) */ public function setItunesSummary($value) { - if ($this->stringWrapper->strlen($value, $this->getEncoding()) > 4000) { + if ($this->stringWrapper->strlen($value) > 4000) { throw new Writer\Exception\InvalidArgumentException('invalid parameter: "summary" may only' . ' contain a maximum of 4000 characters'); } From 5a823ffe5e6d8b3ac17dbad1642da6612bfe7c52 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Mon, 7 Jan 2013 20:28:07 +0100 Subject: [PATCH 50/51] Make sure StringWrapper::convert don't substring return value on invalid character + test --- library/Zend/Stdlib/StringWrapper/Iconv.php | 5 ++++- .../StringWrapper/CommonStringWrapperTest.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/library/Zend/Stdlib/StringWrapper/Iconv.php b/library/Zend/Stdlib/StringWrapper/Iconv.php index 118b5e082d8..0cc8464eb67 100644 --- a/library/Zend/Stdlib/StringWrapper/Iconv.php +++ b/library/Zend/Stdlib/StringWrapper/Iconv.php @@ -290,6 +290,9 @@ public function convert($str, $reverse = false) $fromEncoding = $reverse ? $convertEncoding : $encoding; $toEncoding = $reverse ? $encoding : $convertEncoding; - return iconv($fromEncoding, $toEncoding, $str); + + // automatically add "//IGNORE" to not stop converting on invalid characters + // invalid characters triggers a notice anyway + return iconv($fromEncoding, $toEncoding . '//IGNORE', $str); } } diff --git a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php index 454809b33ad..8fe180ba3fc 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php @@ -12,6 +12,7 @@ namespace ZendTest\Stdlib\StringWrapper; use PHPUnit_Framework_TestCase as TestCase; +use Zend\Stdlib\ErrorHandler; use Zend\Stdlib\Exception; use Zend\Stdlib\StringWrapper\StringWrapperInterface; @@ -134,6 +135,22 @@ public function testConvert($encoding, $convertEncoding, $str, $expected) $this->assertSame($str, $result); } + public function testConvertDontSubstringsOnInvalidCharacter() + { + $wrapper = $this->getWrapper('UTF-8', 'ASCII'); + if (!$wrapper) { + $this->markTestSkipped("Converting UTF-8 to ASCII not supported"); + } + + // NOTE: This call could trigger a notice/warning/error + // but that isn't part of this test + ErrorHandler::start(E_NOTICE); + $result = $wrapper->convert($wrapper->convert("foo \xDEx bar"), true); + ErrorHandler::stop(); + + $this->assertSame("foo x bar", $result); + } + public function wordWrapProvider() { return array( From c13d55f611387fce5493905c7f07e388c69f20ad Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Mon, 7 Jan 2013 20:29:56 +0100 Subject: [PATCH 51/51] removed unneccissary 'word-wrap-' prefix --- .../StringWrapper/CommonStringWrapperTest.php | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php index 8fe180ba3fc..3640431d81b 100644 --- a/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php +++ b/tests/ZendTest/Stdlib/StringWrapper/CommonStringWrapperTest.php @@ -155,74 +155,74 @@ public function wordWrapProvider() { return array( // Standard cut tests - 'word-wrap-cut-single-line' => + 'cut-single-line' => array('utf-8', 'äbüöcß', 2, ' ', true, 'äb üö cß'), - 'word-wrap-cut-multi-line' => + 'cut-multi-line' => array('utf-8', 'äbüöc ß äbüöcß', 2, ' ', true, 'äb üö c ß äb üö cß'), - 'word-wrap-cut-multi-line-short-words' => + 'cut-multi-line-short-words' => array('utf-8', 'Ä very long wöööööööööööörd.', 8, "\n", true, "Ä very\nlong\nwööööööö\nööööörd."), - 'word-wrap-cut-multi-line-with-previous-new-lines' => + 'cut-multi-line-with-previous-new-lines' => array('utf-8', "Ä very\nlong wöööööööööööörd.", 8, "\n", false, "Ä very\nlong\nwöööööööööööörd."), - 'word-wrap-long-break' => + 'long-break' => array('utf-8', "Ä very
long wöö
öööööööö
öörd.", 8, '
', false, "Ä very
long wöö
öööööööö
öörd."), // Alternative cut tests - 'word-wrap-cut-beginning-single-space' => + 'cut-beginning-single-space' => array('utf-8', ' äüöäöü', 3, ' ', true, ' äüö äöü'), - 'word-wrap-cut-ending-single-space' => + 'cut-ending-single-space' => array('utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '), - 'word-wrap-cut-ending-single-space-with-non-space-divider' => + 'cut-ending-single-space-with-non-space-divider' => array('utf-8', 'äöüäöü ', 3, '-', true, 'äöü-äöü-'), - 'word-wrap-cut-ending-two-spaces' => + 'cut-ending-two-spaces' => array('utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '), - 'word-wrap-no-cut-ending-single-space' => + 'no-cut-ending-single-space' => array('utf-8', '12345 ', 5, '-', false, '12345-'), - 'word-wrap-no-cut-ending-two-spaces' => + 'no-cut-ending-two-spaces' => array('utf-8', '12345 ', 5, '-', false, '12345- '), - 'word-wrap-cut-ending-three-spaces' => + 'cut-ending-three-spaces' => array('utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '), - 'word-wrap-cut-ending-two-breaks' => + 'cut-ending-two-breaks' => array('utf-8', 'äüöäöü--', 3, '-', true, 'äüö-äöü--'), - 'word-wrap-cut-tab' => + 'cut-tab' => array('utf-8', "äbü\töcß", 3, ' ', true, "äbü \töc ß"), - 'word-wrap-cut-new-line-with-space' => + 'cut-new-line-with-space' => array('utf-8', "äbü\nößt", 3, ' ', true, "äbü \nöß t"), - 'word-wrap-cut-new-line-with-new-line' => + 'cut-new-line-with-new-line' => array('utf-8', "äbü\nößte", 3, "\n", true, "äbü\nößt\ne"), // Break cut tests - 'word-wrap-cut-break-before' => + 'cut-break-before' => array('ascii', 'foobar-foofoofoo', 8, '-', true, 'foobar-foofoofo-o'), - 'word-wrap-cut-break-with' => + 'cut-break-with' => array('ascii', 'foobar-foobar', 6, '-', true, 'foobar-foobar'), - 'word-wrap-cut-break-within' => + 'cut-break-within' => array('ascii', 'foobar-foobar', 7, '-', true, 'foobar-foobar'), - 'word-wrap-cut-break-within-end' => + 'cut-break-within-end' => array('ascii', 'foobar-', 7, '-', true, 'foobar-'), - 'word-wrap-cut-break-after' => + 'cut-break-after' => array('ascii', 'foobar-foobar', 5, '-', true, 'fooba-r-fooba-r'), // Standard no-cut tests - 'word-wrap-no-cut-single-line' => + 'no-cut-single-line' => array('utf-8', 'äbüöcß', 2, ' ', false, 'äbüöcß'), - 'word-wrap-no-cut-multi-line' => + 'no-cut-multi-line' => array('utf-8', 'äbüöc ß äbüöcß', 2, "\n", false, "äbüöc\nß\näbüöcß"), - 'word-wrap-no-cut-multi-word' => + 'no-cut-multi-word' => array('utf-8', 'äöü äöü äöü', 5, "\n", false, "äöü\näöü\näöü"), // Break no-cut tests - 'word-wrap-no-cut-break-before' => + 'no-cut-break-before' => array('ascii', 'foobar-foofoofoo', 8, '-', false, 'foobar-foofoofoo'), - 'word-wrap-no-cut-break-with' => + 'no-cut-break-with' => array('ascii', 'foobar-foobar', 6, '-', false, 'foobar-foobar'), - 'word-wrap-no-cut-break-within' => + 'no-cut-break-within' => array('ascii', 'foobar-foobar', 7, '-', false, 'foobar-foobar'), - 'word-wrap-no-cut-break-within-end' => + 'no-cut-break-within-end' => array('ascii', 'foobar-', 7, '-', false, 'foobar-'), - 'word-wrap-no-cut-break-after' => + 'no-cut-break-after' => array('ascii', 'foobar-foobar', 5, '-', false, 'foobar-foobar'), ); }