From 12fe0c7b85de041fc2465531fa0b6b3e4eb8be5f Mon Sep 17 00:00:00 2001 From: SilverFire Date: Thu, 5 Mar 2015 14:57:29 +0100 Subject: [PATCH] Fixes #7488: Added `StringHelper::explode` to perform explode with trimming and skipping of empty elements --- framework/CHANGELOG.md | 2 +- framework/helpers/BaseStringHelper.php | 31 +++++++++++++++++++ .../framework/helpers/StringHelperTest.php | 9 ++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 108f4ad3b9..3cbc52d4b2 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -5,7 +5,7 @@ Yii Framework 2 Change Log ----------------------- - Bug #7529: Fixed `yii\web\Response::sendContentAsFile()` that was broken in 2.0.3 (samdark) - +- Enh #7488: Added `StringHelper::explode` to perform explode with trimming and skipping of empty elements (SilverFire, nineinchnick, creocoder, samdark) 2.0.3 March 01, 2015 -------------------- diff --git a/framework/helpers/BaseStringHelper.php b/framework/helpers/BaseStringHelper.php index f9a4607f86..3e3e6838c3 100644 --- a/framework/helpers/BaseStringHelper.php +++ b/framework/helpers/BaseStringHelper.php @@ -234,4 +234,35 @@ class BaseStringHelper return mb_strtolower(mb_substr($string, -$bytes, null, '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset); } } + + /** + * Explodes string into array, optionally trims values and skips empty ones + * + * @param string $string String to be exploded. + * @param string $delimiter Delimiter. Default is ','. + * @param mixed $trim Whether to trim each element. Can be: + * - boolean - to trim normally; + * - string - custom characters to trim. Will be passed as a second argument to `trim()` function. + * - callable - will be called for each value instead of trim. Takes the only argument - value. + * @param boolean $skipEmpty Whether to skip empty strings between delimiters. Default is false. + * @return array + */ + public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false) { + $result = explode($delimiter, $string); + if ($trim) { + if ($trim === true) { + $trim = 'trim'; + } elseif (!is_callable($trim)) { + $trim = function($v) use ($trim) { + return trim($v, $trim); + }; + } + $result = array_map($trim, $result); + } + if ($skipEmpty) { + // Wrapped with array_values to make array keys sequential after empty values removing + $result = array_values(array_filter($result)); + } + return $result; + } } diff --git a/tests/unit/framework/helpers/StringHelperTest.php b/tests/unit/framework/helpers/StringHelperTest.php index bed726f5ac..b305aaf97c 100644 --- a/tests/unit/framework/helpers/StringHelperTest.php +++ b/tests/unit/framework/helpers/StringHelperTest.php @@ -227,4 +227,13 @@ class StringHelperTest extends TestCase $this->assertTrue(StringHelper::endsWith('string', 'nG', false)); $this->assertTrue(StringHelper::endsWith('BüЯйΨ', 'ÜяЙΨ', false)); } + + public function testExplode() + { + $this->assertEquals(['It', 'is', 'a first', 'test'], StringHelper::explode("It, is, a first, test")); + $this->assertEquals(['It', 'is', 'a second', 'test'], StringHelper::explode("It+ is+ a second+ test", '+')); + $this->assertEquals(['Save', '', '', 'empty trimmed string'], StringHelper::explode("Save, ,, empty trimmed string", ',')); + $this->assertEquals(['Здесь', 'multibyte', 'строка'], StringHelper::explode("Здесь我 multibyte我 строка", '我')); + $this->assertEquals(['Disable', ' trim ', 'here but ignore empty'], StringHelper::explode("Disable, trim ,,,here but ignore empty", ',', false, true)); + } }