From cd03a41b98623df52479247818320f155228a7a6 Mon Sep 17 00:00:00 2001 From: Chris Reichel Date: Fri, 21 Feb 2025 12:55:17 +0100 Subject: [PATCH] fix regression in ArrayHelper::map ! fix regression in ArrayHelper::map when trying to use magic methods --- framework/CHANGELOG.md | 1 + framework/helpers/BaseArrayHelper.php | 3 --- tests/framework/helpers/ArrayHelperTest.php | 28 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 4869591ce6f..64183f74d43 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -5,6 +5,7 @@ Yii Framework 2 Change Log ------------------------ - Enh #20309: Add custom attributes support to style tags (nzwz) +- Bug #20327: Fix regression in `\yii\helpers\BaseArrayHelper::map` (chriscpty) 2.0.52 February 13, 2025 diff --git a/framework/helpers/BaseArrayHelper.php b/framework/helpers/BaseArrayHelper.php index c24cc99f970..d9958b72930 100644 --- a/framework/helpers/BaseArrayHelper.php +++ b/framework/helpers/BaseArrayHelper.php @@ -595,9 +595,6 @@ public static function getColumn($array, $name, $keepKeys = true) */ public static function map($array, $from, $to, $group = null) { - if (is_string($from) && is_string($to) && $group === null && strpos($from, '.') === false && strpos($to, '.') === false) { - return array_column($array, $to, $from); - } $result = []; foreach ($array as $element) { $key = static::getValue($element, $from); diff --git a/tests/framework/helpers/ArrayHelperTest.php b/tests/framework/helpers/ArrayHelperTest.php index 4e6b790bebc..f91424245da 100644 --- a/tests/framework/helpers/ArrayHelperTest.php +++ b/tests/framework/helpers/ArrayHelperTest.php @@ -785,6 +785,12 @@ static function (array $group) { '33' => '44', '55' => '66' ], $result); + + $array = [new MagicClass()]; + + $result = ArrayHelper::map($array, 'magic', 'moreMagic'); + + $this->assertEquals([42 => 'ta-da'], $result); } public function testKeyExists() @@ -1864,3 +1870,25 @@ public function getMoreMagic() return 'ta-da'; } } + +class MagicClass +{ + public function __get($name) { + if ($name === 'magic') { + return 42; + } + if ($name === 'moreMagic') { + return 'ta-da'; + } + return $this->$name; + } + + public function __set($name, $value) { + $this->$name = $value; + } + + public function __isset($name) + { + return isset($this->$name); + } +}