public static function getObjectAttribute($object, $attributeName)
{
if (!is_object($object)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'object');
}
if (!is_string($attributeName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
}
try {
$attribute = new ReflectionProperty($object, $attributeName);
} catch (ReflectionException $e) {
$reflector = new ReflectionObject($object);
while ($reflector = $reflector->getParentClass()) {
try {
$attribute = $reflector->getProperty($attributeName);
break;
} catch (ReflectionException $e) {
}
}
}
if (isset($attribute)) {
if ($attribute == NULL || $attribute->isPublic()) {
return $object->{$attributeName};
} else {
$array = (array) $object;
$protectedName = "*" . $attributeName;
if (array_key_exists($protectedName, $array)) {
return $array[$protectedName];
} else {
$classes = self::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 PHPUnit_Framework_Exception(sprintf('Attribute "%s" not found in object.', $attributeName));
}