ObjectReflector.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/object-reflector.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\ObjectReflector;
  11. use function count;
  12. use function explode;
  13. use function get_class;
  14. use function is_object;
  15. class ObjectReflector
  16. {
  17. /**
  18. * @param object $object
  19. *
  20. * @throws InvalidArgumentException
  21. */
  22. public function getAttributes($object): array
  23. {
  24. if (!is_object($object)) {
  25. throw new InvalidArgumentException;
  26. }
  27. $attributes = [];
  28. $className = get_class($object);
  29. foreach ((array) $object as $name => $value) {
  30. $name = explode("\0", (string) $name);
  31. if (count($name) === 1) {
  32. $name = $name[0];
  33. } else {
  34. if ($name[1] !== $className) {
  35. $name = $name[1] . '::' . $name[2];
  36. } else {
  37. $name = $name[2];
  38. }
  39. }
  40. $attributes[$name] = $value;
  41. }
  42. return $attributes;
  43. }
  44. }