ObjectReflector.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /*
  3. * This file is part of 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. declare(strict_types=1);
  11. namespace SebastianBergmann\ObjectReflector;
  12. class ObjectReflector
  13. {
  14. /**
  15. * @param object $object
  16. *
  17. * @return array
  18. *
  19. * @throws InvalidArgumentException
  20. */
  21. public function getAttributes($object): array
  22. {
  23. if (!is_object($object)) {
  24. throw new InvalidArgumentException;
  25. }
  26. $attributes = [];
  27. $className = get_class($object);
  28. foreach ((array) $object as $name => $value) {
  29. $name = explode("\0", (string) $name);
  30. if (count($name) === 1) {
  31. $name = $name[0];
  32. } else {
  33. if ($name[1] !== $className) {
  34. $name = $name[1] . '::' . $name[2];
  35. } else {
  36. $name = $name[2];
  37. }
  38. }
  39. $attributes[$name] = $value;
  40. }
  41. return $attributes;
  42. }
  43. }