国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
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.

106 lines
2.4KB

  1. <?php
  2. namespace WeChat\Contracts;
  3. if (!defined('DEDEINC')) exit ('dedebiz');
  4. use ArrayAccess;
  5. /**
  6. * Class DataArray
  7. * @package WeChat
  8. */
  9. class DataArray implements ArrayAccess
  10. {
  11. /**
  12. * 当前配置值
  13. * @var array
  14. */
  15. private $config = [];
  16. /**
  17. * Config constructor.
  18. * @param array $options
  19. */
  20. public function __construct(array $options)
  21. {
  22. $this->config = $options;
  23. }
  24. /**
  25. * 设置配置项值
  26. * @param string $offset
  27. * @param string|array|null|integer $value
  28. */
  29. public function set($offset, $value)
  30. {
  31. $this->offsetSet($offset, $value);
  32. }
  33. /**
  34. * 获取配置项参数
  35. * @param string|null $offset
  36. * @return array|string|null
  37. */
  38. public function get($offset = null)
  39. {
  40. return $this->offsetGet($offset);
  41. }
  42. /**
  43. * 合并数据到对象
  44. * @param array $data 需要合并的数据
  45. * @param bool $append 是否追加数据
  46. * @return array
  47. */
  48. public function merge(array $data, $append = false)
  49. {
  50. if ($append) {
  51. return $this->config = array_merge($this->config, $data);
  52. }
  53. return array_merge($this->config, $data);
  54. }
  55. /**
  56. * 设置配置项值
  57. * @param string $offset
  58. * @param string|array|null|integer $value
  59. */
  60. #[\ReturnTypeWillChange]
  61. public function offsetSet($offset, $value)
  62. {
  63. if (is_null($offset)) {
  64. $this->config[] = $value;
  65. } else {
  66. $this->config[$offset] = $value;
  67. }
  68. }
  69. /**
  70. * 判断配置Key是否存在
  71. * @param string $offset
  72. * @return bool
  73. */
  74. #[\ReturnTypeWillChange]
  75. public function offsetExists($offset)
  76. {
  77. return isset($this->config[$offset]);
  78. }
  79. /**
  80. * 清理配置项
  81. * @param string|null $offset
  82. */
  83. #[\ReturnTypeWillChange]
  84. public function offsetUnset($offset = null)
  85. {
  86. if (is_null($offset)) {
  87. $this->config = [];
  88. } else {
  89. unset($this->config[$offset]);
  90. }
  91. }
  92. /**
  93. * 获取配置项参数
  94. * @param string|null $offset
  95. * @return array|string|null
  96. */
  97. #[\ReturnTypeWillChange]
  98. public function offsetGet($offset = null)
  99. {
  100. if (is_null($offset)) {
  101. return $this->config;
  102. }
  103. return isset($this->config[$offset]) ? $this->config[$offset] : null;
  104. }
  105. }
  106. ?>