/**
* Returns the value of an object's attribute.
* This also works for attributes that are declared protected or private.
*
* @param object $object
* @param string $attributeName
* @return mixed
* @throws InvalidArgumentException
* @access public
* @static
* @since Method available since Release 3.1.0
*/
public static function getObjectAttribute($object, $attributeName)
{
if (!is_object($object) || !is_string($attributeName)) {
throw new InvalidArgumentException();
}
self::assertObjectHasAttribute($attributeName, $object);
if (property_exists($object, $attributeName)) {
return $object->{$attributeName};
} else {
$array = (array) $object;
$protectedName = "*" . $attributeName;
if (array_key_exists($protectedName, $array)) {
return $array[$protectedName];
} else {
$classes = PHPUnit_Util_Class::getHierarchy(get_class($object));
foreach ($classes as $class) {
$privateName = sprintf("%s%s", $class, $attributeName);
if (array_key_exists($privateName, $array)) {
return $array[$privateName];
}
}
}
}
throw new RuntimeException(sprintf('Attribute "%s" not found in object.', $attributeName));
}