mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-14 06:11:35 +08:00
Fix #18086: Fix accessing public properties of ArrayAccess
via ArrayHelper::getValue()
This commit is contained in:

committed by
GitHub

parent
6aa0ac1ed0
commit
bfc0698b36
@ -205,13 +205,20 @@ class BaseArrayHelper
|
||||
$key = substr($key, $pos + 1);
|
||||
}
|
||||
|
||||
if (static::isArrayAccess($array)) {
|
||||
return static::keyExists($key, $array) ? $array[$key] : $default;
|
||||
if (static::keyExists($key, $array)) {
|
||||
return $array[$key];
|
||||
}
|
||||
if (is_object($array)) {
|
||||
// this is expected to fail if the property does not exist, or __get() is not implemented
|
||||
// it is not reliably possible to check whether a property is accessible beforehand
|
||||
return $array->$key;
|
||||
try {
|
||||
return $array->$key;
|
||||
} catch (\Exception $e) {
|
||||
if ($array instanceof ArrayAccess) {
|
||||
return $default;
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
return $default;
|
||||
@ -860,22 +867,6 @@ class BaseArrayHelper
|
||||
return is_array($var) || $var instanceof Traversable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a variable is an array or [[\ArrayAccess]].
|
||||
*
|
||||
* This method does the same as the PHP function [is_array()](https://secure.php.net/manual/en/function.is-array.php)
|
||||
* but additionally works on objects that implement the [[\ArrayAccess]] interface.
|
||||
* @param mixed $var The variable being evaluated.
|
||||
* @return bool whether data on $var can be accessed as arrays
|
||||
* @see https://secure.php.net/manual/en/function.is-array.php
|
||||
* @since 2.0.36
|
||||
*/
|
||||
public static function isArrayAccess($var)
|
||||
{
|
||||
return is_array($var) || $var instanceof \ArrayAccess;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks whether an array or [[Traversable]] is a subset of another array or [[Traversable]].
|
||||
*
|
||||
|
Reference in New Issue
Block a user