酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

58 lines
1.9 KiB

  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace EasyWeChat\Kernel;
  11. use EasyWeChat\Kernel\Contracts\Arrayable;
  12. use EasyWeChat\Kernel\Exceptions\RuntimeException;
  13. use EasyWeChat\Kernel\Support\Arr;
  14. use EasyWeChat\Kernel\Support\Collection;
  15. function data_get($data, $key, $default = null)
  16. {
  17. switch (true) {
  18. case is_array($data):
  19. return Arr::get($data, $key, $default);
  20. case $data instanceof Collection:
  21. return $data->get($key, $default);
  22. case $data instanceof Arrayable:
  23. return Arr::get($data->toArray(), $key, $default);
  24. case $data instanceof \ArrayIterator:
  25. return $data->getArrayCopy()[$key] ?? $default;
  26. case $data instanceof \ArrayAccess:
  27. return $data[$key] ?? $default;
  28. case $data instanceof \IteratorAggregate && $data->getIterator() instanceof \ArrayIterator:
  29. return $data->getIterator()->getArrayCopy()[$key] ?? $default;
  30. case is_object($data):
  31. return $data->{$key} ?? $default;
  32. default:
  33. throw new RuntimeException(sprintf('Can\'t access data with key "%s"', $key));
  34. }
  35. }
  36. function data_to_array($data)
  37. {
  38. switch (true) {
  39. case is_array($data):
  40. return $data;
  41. case $data instanceof Collection:
  42. return $data->all();
  43. case $data instanceof Arrayable:
  44. return $data->toArray();
  45. case $data instanceof \IteratorAggregate && $data->getIterator() instanceof \ArrayIterator:
  46. return $data->getIterator()->getArrayCopy();
  47. case $data instanceof \ArrayIterator:
  48. return $data->getArrayCopy();
  49. default:
  50. throw new RuntimeException(sprintf('Can\'t transform data to array'));
  51. }
  52. }