Helpers.php 940 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace GuzzleHttp\Tests;
  3. class Helpers
  4. {
  5. public static function readObjectAttribute(object $object, string $attributeName)
  6. {
  7. $reflector = new \ReflectionObject($object);
  8. do {
  9. try {
  10. $attribute = $reflector->getProperty($attributeName);
  11. if (!$attribute || $attribute->isPublic()) {
  12. return $object->$attributeName;
  13. }
  14. $attribute->setAccessible(true);
  15. try {
  16. return $attribute->getValue($object);
  17. } finally {
  18. $attribute->setAccessible(false);
  19. }
  20. } catch (\ReflectionException $e) {
  21. // do nothing
  22. }
  23. } while ($reflector = $reflector->getParentClass());
  24. throw new \Exception(
  25. sprintf('Attribute "%s" not found in object.', $attributeName)
  26. );
  27. }
  28. }