File: Helpers.php

package info (click to toggle)
php-guzzlehttp-psr7 1.7.0-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 780 kB
  • sloc: php: 7,816; makefile: 37
file content (35 lines) | stat: -rw-r--r-- 886 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php

namespace GuzzleHttp\Tests\Psr7;

class Helpers
{
    /**
     * @param object $object
     * @param string $attributeName
     */
    public static function readObjectAttribute($object, $attributeName)
    {
        $reflector = new \ReflectionObject($object);

        do {
            try {
                $attribute = $reflector->getProperty($attributeName);

                if (!$attribute || $attribute->isPublic()) {
                    return $object->$attributeName;
                }

                $attribute->setAccessible(true);

                return $attribute->getValue($object);
            } catch (\ReflectionException $e) {
                // do nothing
            }
        } while ($reflector = $reflector->getParentClass());

        throw new \Exception(
            sprintf('Attribute "%s" not found in object.', $attributeName)
        );
    }
}