DataArray.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WeChat\Contracts;
  16. use ArrayAccess;
  17. /**
  18. * Class DataArray
  19. * @package WeChat
  20. */
  21. class DataArray implements ArrayAccess
  22. {
  23. /**
  24. * 当前配置值
  25. * @var array
  26. */
  27. private $config = [];
  28. /**
  29. * Config constructor.
  30. * @param array $options
  31. */
  32. public function __construct(array $options)
  33. {
  34. $this->config = $options;
  35. }
  36. /**
  37. * 设置配置项值
  38. * @param string $offset
  39. * @param string|array|null|integer $value
  40. */
  41. public function set($offset, $value)
  42. {
  43. $this->offsetSet($offset, $value);
  44. }
  45. /**
  46. * 获取配置项参数
  47. * @param string|null $offset
  48. * @return array|string|null|mixed
  49. */
  50. public function get($offset = null)
  51. {
  52. return $this->offsetGet($offset);
  53. }
  54. /**
  55. * 合并数据到对象
  56. * @param array $data 需要合并的数据
  57. * @param bool $append 是否追加数据
  58. * @return array
  59. */
  60. public function merge(array $data, $append = false)
  61. {
  62. if ($append) {
  63. return $this->config = array_merge($this->config, $data);
  64. }
  65. return array_merge($this->config, $data);
  66. }
  67. /**
  68. * 设置配置项值
  69. * @param string $offset
  70. * @param string|array|null|integer $value
  71. */
  72. #[\ReturnTypeWillChange]
  73. public function offsetSet($offset, $value)
  74. {
  75. if (is_null($offset)) {
  76. $this->config[] = $value;
  77. } else {
  78. $this->config[$offset] = $value;
  79. }
  80. }
  81. /**
  82. * 判断配置Key是否存在
  83. * @param string $offset
  84. * @return bool
  85. */
  86. #[\ReturnTypeWillChange]
  87. public function offsetExists($offset)
  88. {
  89. return isset($this->config[$offset]);
  90. }
  91. /**
  92. * 清理配置项
  93. * @param string|null $offset
  94. */
  95. #[\ReturnTypeWillChange]
  96. public function offsetUnset($offset = null)
  97. {
  98. if (is_null($offset)) {
  99. $this->config = [];
  100. } else {
  101. unset($this->config[$offset]);
  102. }
  103. }
  104. /**
  105. * 获取配置项参数
  106. * @param string|null $offset
  107. * @return array|string|null|mixed
  108. */
  109. #[\ReturnTypeWillChange]
  110. public function offsetGet($offset = null)
  111. {
  112. if (is_null($offset)) {
  113. return $this->config;
  114. }
  115. return isset($this->config[$offset]) ? $this->config[$offset] : null;
  116. }
  117. }