From ccd20793d43d05104230885704683c4137125cfb Mon Sep 17 00:00:00 2001 From: SilverFire - Dima Naumenko Date: Wed, 14 Oct 2015 00:07:29 +0300 Subject: [PATCH] Fixed #9911 - StringHelper::explode with skip_empty option removed item eq to 0 --- framework/CHANGELOG.md | 1 + framework/helpers/BaseStringHelper.php | 4 +++- tests/framework/helpers/StringHelperTest.php | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index a9e6a5c..af55475 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -23,6 +23,7 @@ Yii Framework 2 Change Log - Bug #9791: Fixed endless loop on file creation for non-existing device letters on windows (lukos, cebe) - Bug #9874: Fixed outputting exception stacktrace in non-debug mode when `Response::FORMAT_RAW` is used (nainoon) - Bug #9883: Passing a single `yii\db\Expression` to `Query::select()` or `::addSelect()` was not handled correctly in all cases (cebe) +- Bug #9911: Fixed `yii\helpers\BaseStringHelper::explode()` code so it does not remove items eq to 0 with skip_empty attribute (silverfire, kidol) - Bug: Fixed generation of canonical URLs for `ViewAction` pages (samdark) - Enh #7581: Added ability to specify range using anonymous function in `RangeValidator` (RomeroMsk) - Enh #8613: `yii\widgets\FragmentCache` will not store empty content anymore which fixes some problems related to `yii\filters\PageCache` (kidol) diff --git a/framework/helpers/BaseStringHelper.php b/framework/helpers/BaseStringHelper.php index de22c2d..d3a7ab1 100644 --- a/framework/helpers/BaseStringHelper.php +++ b/framework/helpers/BaseStringHelper.php @@ -264,7 +264,9 @@ class BaseStringHelper } if ($skipEmpty) { // Wrapped with array_values to make array keys sequential after empty values removing - $result = array_values(array_filter($result)); + $result = array_values(array_filter($result, function ($value) { + return $value !== ''; + })); } return $result; } diff --git a/tests/framework/helpers/StringHelperTest.php b/tests/framework/helpers/StringHelperTest.php index b305aaf..6ea0fb8 100644 --- a/tests/framework/helpers/StringHelperTest.php +++ b/tests/framework/helpers/StringHelperTest.php @@ -231,6 +231,7 @@ class StringHelperTest extends TestCase public function testExplode() { $this->assertEquals(['It', 'is', 'a first', 'test'], StringHelper::explode("It, is, a first, test")); + $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(['Здесь', 'multibyte', 'строка'], StringHelper::explode("Здесь我 multibyte我 строка", '我'));