Arrayable.php 508 B

12345678910111213141516171819202122232425
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Supports\Traits;
  4. use ReflectionClass;
  5. use Yansongda\Supports\Str;
  6. trait Arrayable
  7. {
  8. public function toArray(): array
  9. {
  10. $result = [];
  11. foreach ((new ReflectionClass($this))->getProperties() as $item) {
  12. $k = $item->getName();
  13. $method = 'get'.Str::studly($k);
  14. $result[Str::snake($k)] = method_exists($this, $method) ? $this->{$method}() : $this->{$k};
  15. }
  16. return $result;
  17. }
  18. }