diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index ebcefd9..e6e3546 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -17,6 +17,7 @@ Yii Framework 2 Change Log - Enh #18493: Faster request parsing for REST UrlRule with prefix handling (bizley) - Enh #18487: Allow creating URLs for non-GET-verb rules (bizley) - Bug #8750: Fix MySQL support when running in `ANSI`/`ANSI_QUOTES` modes (brandonkelly) +- Bug #18505: Fixed `yii\helpers\ArrayHelper::getValue()` for ArrayAccess objects with explicitly defined properties (samdark) 2.0.40 December 23, 2020 diff --git a/framework/helpers/BaseArrayHelper.php b/framework/helpers/BaseArrayHelper.php index 6e7d645..8bc7ef2 100644 --- a/framework/helpers/BaseArrayHelper.php +++ b/framework/helpers/BaseArrayHelper.php @@ -196,6 +196,10 @@ class BaseArrayHelper $key = $lastKey; } + if (is_object($array) && property_exists($array, $key)) { + return $array->$key; + } + if (static::keyExists($key, $array)) { return $array[$key]; } diff --git a/tests/framework/helpers/ArrayHelperTest.php b/tests/framework/helpers/ArrayHelperTest.php index a5f8aae..fae9d02 100644 --- a/tests/framework/helpers/ArrayHelperTest.php +++ b/tests/framework/helpers/ArrayHelperTest.php @@ -10,6 +10,7 @@ namespace yiiunit\framework\helpers; use ArrayAccess; use Iterator; use yii\base\BaseObject; +use yii\base\Model; use yii\data\Sort; use yii\helpers\ArrayHelper; use yiiunit\TestCase; @@ -122,6 +123,23 @@ class TraversableArrayAccessibleObject extends ArrayAccessibleObject implements } } +class MagicModel extends Model +{ + protected $magic; + + public function getMagic() + { + return 42; + } + + private $moreMagic; + + public function getMoreMagic() + { + return 'ta-da'; + } +} + /** * @group helpers */ @@ -1536,4 +1554,15 @@ class ArrayHelperTest extends TestCase $this->assertEquals(123, ArrayHelper::getValue($data, 'value')); $this->assertEquals('bar1', ArrayHelper::getValue($data, 'name')); } + + /** + * https://github.com/yiisoft/yii2/commit/35fb9c624893855317e5fe52e6a21f6518a9a31c changed the way + * ArrayHelper works with existing object properties in case of ArrayAccess. + */ + public function testArrayAccessWithMagicProperty() + { + $model = new MagicModel(); + $this->assertEquals(42, ArrayHelper::getValue($model, 'magic')); + $this->assertEquals('ta-da', ArrayHelper::getValue($model, 'moreMagic')); + } }