ArrayAccessible.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the overtrue/wechat.
  9. *
  10. * (c) overtrue <i@overtrue.me>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace ByteDance\Kernel\Support;
  16. use ArrayAccess;
  17. use ArrayIterator;
  18. use IteratorAggregate;
  19. use ByteDance\Kernel\Contracts\Arrayable;
  20. /**
  21. * Class ArrayAccessible.
  22. *
  23. * @author overtrue <i@overtrue.me>
  24. */
  25. class ArrayAccessible implements ArrayAccess, IteratorAggregate, Arrayable
  26. {
  27. private $array;
  28. public function __construct(array $array = [])
  29. {
  30. $this->array = $array;
  31. }
  32. public function offsetExists($offset)
  33. {
  34. return array_key_exists($offset, $this->array);
  35. }
  36. public function offsetGet($offset)
  37. {
  38. return $this->array[$offset];
  39. }
  40. public function offsetSet($offset, $value)
  41. {
  42. if (null === $offset) {
  43. $this->array[] = $value;
  44. } else {
  45. $this->array[$offset] = $value;
  46. }
  47. }
  48. public function offsetUnset($offset)
  49. {
  50. unset($this->array[$offset]);
  51. }
  52. public function getIterator()
  53. {
  54. return new ArrayIterator($this->array);
  55. }
  56. public function toArray()
  57. {
  58. return $this->array;
  59. }
  60. }