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