Fix #18086: Fix accessing public properties of ArrayAccess via ArrayHelper::getValue()

This commit is contained in:
Alexander Makarov
2020-07-05 18:27:17 +03:00
committed by GitHub
parent 6aa0ac1ed0
commit bfc0698b36
3 changed files with 23 additions and 20 deletions

View File

@ -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]].
*