From a32cfcc8ef025dfe03a98eff675d56c1132eec74 Mon Sep 17 00:00:00 2001 From: Thoulah Date: Sun, 13 May 2018 00:14:39 +0200 Subject: [PATCH] Fixes #16266: Fixed `yii\helpers\BaseStringHelper` where explode would not allow 0 as trim string --- framework/CHANGELOG.md | 1 + framework/helpers/BaseStringHelper.php | 2 +- tests/framework/helpers/StringHelperTest.php | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 792f796..391b6a7 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -22,6 +22,7 @@ Yii Framework 2 Change Log - Bug #16217: Fixed `yii\console\controllers\HelpController` to work well in Windows environment (samdark) - Bug #14636: Views can now use relative paths even when using themed views (sammousa) - Bug #16245: Fixed `__isset()` in `BaseActiveRecord` not catching errors (sammousa) +- Bug #16266: Fixed `yii\helpers\BaseStringHelper` where explode would not allow 0 as trim string (Thoulah) - Enh #16191: Enhanced `yii\helpers\Inflector` to work correctly with UTF-8 (silverfire) - Bug: Fixed bad instnaceof check in `yii\db\Schema::getTableMetadata()` (samdark) diff --git a/framework/helpers/BaseStringHelper.php b/framework/helpers/BaseStringHelper.php index e3a304a..8eff643 100644 --- a/framework/helpers/BaseStringHelper.php +++ b/framework/helpers/BaseStringHelper.php @@ -269,7 +269,7 @@ class BaseStringHelper public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false) { $result = explode($delimiter, $string); - if ($trim) { + if ($trim !== false) { if ($trim === true) { $trim = 'trim'; } elseif (!is_callable($trim)) { diff --git a/tests/framework/helpers/StringHelperTest.php b/tests/framework/helpers/StringHelperTest.php index 5f6416e..1fc49fc 100644 --- a/tests/framework/helpers/StringHelperTest.php +++ b/tests/framework/helpers/StringHelperTest.php @@ -267,6 +267,7 @@ class StringHelperTest extends TestCase $this->assertEquals(['It', 'is', 'a test with trimmed digits', '0', '1', '2'], StringHelper::explode('It, is, a test with trimmed digits, 0, 1, 2', ',', true, true)); $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(['44', '512'], StringHelper::explode('0 0 440 512', ' ', '0', true)); $this->assertEquals(['Здесь', 'multibyte', 'строка'], StringHelper::explode('Здесь我 multibyte我 строка', '我')); $this->assertEquals(['Disable', ' trim ', 'here but ignore empty'], StringHelper::explode('Disable, trim ,,,here but ignore empty', ',', false, true)); $this->assertEquals(['It/', ' is?', ' a', ' test with rtrim'], StringHelper::explode('It/, is?, a , test with rtrim', ',', 'rtrim'));