ArrayAccessible.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Support;
  11. use ArrayAccess;
  12. use ArrayIterator;
  13. use EasyWeChat\Kernel\Contracts\Arrayable;
  14. use IteratorAggregate;
  15. /**
  16. * Class ArrayAccessible.
  17. *
  18. * @author overtrue <i@overtrue.me>
  19. */
  20. class ArrayAccessible implements ArrayAccess, IteratorAggregate, Arrayable
  21. {
  22. private $array;
  23. public function __construct(array $array = [])
  24. {
  25. $this->array = $array;
  26. }
  27. public function offsetExists($offset)
  28. {
  29. return array_key_exists($offset, $this->array);
  30. }
  31. public function offsetGet($offset)
  32. {
  33. return $this->array[$offset];
  34. }
  35. public function offsetSet($offset, $value)
  36. {
  37. if (null === $offset) {
  38. $this->array[] = $value;
  39. } else {
  40. $this->array[$offset] = $value;
  41. }
  42. }
  43. public function offsetUnset($offset)
  44. {
  45. unset($this->array[$offset]);
  46. }
  47. public function getIterator()
  48. {
  49. return new ArrayIterator($this->array);
  50. }
  51. public function toArray()
  52. {
  53. return $this->array;
  54. }
  55. }