Fixes #9915: yii\helpers\ArrayHelper::getValue() was erroring instead of returning null for non-existing object properties

This commit is contained in:
Vadim
2015-10-14 12:36:33 +03:00
committed by Alexander Makarov
parent c128d008d3
commit c00b97a12c
3 changed files with 9 additions and 1 deletions

View File

@ -24,6 +24,7 @@ Yii Framework 2 Change Log
- 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 #9915: `yii\helpers\ArrayHelper::getValue()` was erroring instead of returning `null` for non-existing object properties (totaldev, samdark)
- 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)

View File

@ -198,7 +198,7 @@ class BaseArrayHelper
$key = substr($key, $pos + 1);
}
if (is_object($array)) {
if (is_object($array) && isset($array->$key)) {
return $array->$key;
} elseif (is_array($array)) {
return array_key_exists($key, $array) ? $array[$key] : $default;

View File

@ -385,6 +385,13 @@ class ArrayHelperTest extends TestCase
$this->assertEquals($expected, ArrayHelper::getValue($array, $key, $default));
}
public function testGetValueObjects()
{
$object = new Post1();
$this->assertEquals(23, ArrayHelper::getValue($object, 'id'));
$this->assertEquals(null, ArrayHelper::getValue($object, 'nonExisting'));
}
public function testIsAssociative()
{
$this->assertFalse(ArrayHelper::isAssociative('test'));