-
Notifications
You must be signed in to change notification settings - Fork 0
/
Params.php
294 lines (272 loc) · 7.65 KB
/
Params.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* User: Andrey Mistulov
* Company: Aristos
* Email: [email protected]
* Date: 16.10.2018 20:35
*/
namespace prowebcraft\yii2params;
trait Params
{
/**
* Override a model-level rule set, "Make Params Safe Again" ©
* @inheritdoc
*/
public function rules()
{
$rules = parent::rules();
foreach ($rules as $k => $rule) {
if (is_array($rule[0]) && (($params = array_search('params', $rule[0])) !== false)) {
unset($rule[0][$params]);
$rules[$k] = $rule;
}
}
return array_merge($rules, [
[['params'], 'safe'], // not use string
]);
}
/**
* Local storage of params data
* @var array
*/
protected $paramsArray = null;
function __set($name, $value)
{
if ($name === 'params') {
$this->setParams($value);
} else {
parent::__set($name, $value);
}
}
function __get($name)
{
if ($name === 'params') {
return $this->getParams();
}
return parent::__get($name);
}
/**
* Initialise params data
*/
protected function initParams()
{
if ($this->paramsArray === null) {
if (!empty($this->getAttribute('params'))) {
$this->paramsArray = json_decode($this->getAttribute('params'), true) ?: [];
} else {
$this->paramsArray = [];
}
}
}
/**
* Get Params JSON
* @param bool $prettyPrint
* @return null|string
*/
public function getParamsAsJsonString($prettyPrint = false)
{
$this->initParams();
$params = $this->paramsArray;
$flags = $prettyPrint ? JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE : JSON_UNESCAPED_UNICODE;
if (empty($params)) {
return null;
}
return is_array($params) || is_object($params) ? json_encode($params, $flags) : (string)$params;
}
/**
* Serialise Params Object
* @param $insert
* @return mixed
*/
public function beforeSave($insert)
{
$this->setAttribute('params', $this->getParamsAsJsonString());
return parent::beforeSave($insert);
}
/**
* Init Object after find
* @return void
*/
public function afterFind()
{
$this->initParams();
parent::afterFind();
}
/**
* Check param key exist
* @param $key
* @return bool
*/
public function hasParam($key): bool
{
return isset($this->paramsArray[$key]);
}
/**
* Get Parameter by key
* @param string $key
* Key to retrieve. You can use dot access like "car.info.age"
* @return mixed|null
* Default value
*/
public function getParam($key, $default = null): mixed
{
$this->initParams();
$array = $this->paramsArray;
if (isset($this->paramsArray[$key])) {
return $this->paramsArray[$key];
}
$keys = explode('.', $key);
foreach ($keys as $k) {
if (isset($array[$k])) {
$array = $array[$k];
} else {
return $default;
}
}
return $array;
}
/**
* Store a parameter by key
* @param array|string $key
* Key to store. You can use dot access like "car.info.age"
* @param int|float|string|array $value
* Value to store.
* @param $merge bool
* Merge array values
* @param $recursive bool
* Merge array value recursively
* @return $this
*/
public function setParam(array|string $key, mixed $value, bool $merge = false, bool $recursive = false)
{
$this->initParams();
$target = &$this->paramsArray;
if (is_string($key)) {
// Iterate path
$keys = explode('.', $key);
foreach ($keys as $k) {
if (!isset($target[$k]) || !is_array($target[$k])) {
$target[$k] = [];
}
$target = &$target[$k];
}
// Set value to path
} elseif (is_array($key)) {
// Iterate array of paths and values
foreach ($key as $k => $v) {
$this->setParam($k, $v, $merge, $recursive);
}
}
if ($merge && is_array($value)) {
$function = $recursive ? 'array_replace_recursive' : 'array_replace';
$target = $function($target, $value);
} else {
$target = $value;
}
$this->setAttribute('params', $this->getParamsAsJsonString());
return $this;
}
/**
* Get params object
* @return array
*/
public function getParams()
{
$this->initParams();
return $this->paramsArray;
}
/**
* Set params object
* @param array $params
* Params object
* @param bool $merge
* Merge array values
* @param bool $recursive
* Merge array value recursively
* @return $this
*/
public function setParams($params, $merge = true, $recursive = false)
{
$this->initParams();
$function = $recursive ? 'array_replace_recursive' : 'array_replace';
if (is_array($params)) {
$this->paramsArray = $merge ? $function($this->paramsArray, $params) : $params;
}
$this->setAttribute('params', $this->getParamsAsJsonString());
return $this;
}
/**
* Unset param by key
* @param string $key
* Key to unset. You can use dot access like "car.info.age"
* @return $this
*/
public function unsetParam($key)
{
$root = &$this->paramsArray;
if (strpos($key, '.') !== false) {
$keys = explode('.', $key);
for ($k = 0; $k < count($keys) - 1; $k++) {
$key = $keys[$k];
if (!isset($root[$key]) || !is_array($root[$key])) return $this;
$root = &$root[$key];
}
$key = $keys[$k];
}
if (isset($root[$key])) {
unset($root[$key]);
$this->setAttribute('params', $this->getParamsAsJsonString());
};
return $this;
}
/**
* Unset params
* @param $keys array|string
* Null, Array or comma-separated string of keys to unset. If null - unset all keys.
* @return $this
* @throws \Exception
*/
public function unsetParams($keys = null)
{
if (empty($keys)) {
$this->paramsArray = [];
} else {
if (is_string($keys)) {
$keys = explode(',', $keys);
$keys = array_map(function ($v) {
return trim($v);
}, $keys);
}
foreach ($keys as $key) {
if (isset($this->paramsArray[$key])) {
unset($this->paramsArray[$key]);
}
}
}
$this->setAttribute('params', $this->getParamsAsJsonString());
return $this;
}
/**
* Add param to array by key
* @param string $key
* Key to store. You can use dot access like "car.info.age"
* @param mixed $value
* @return $this
*/
public function addParam($key, $value)
{
$this->initParams();
$array = &$this->paramsArray;
$keys = explode('.', $key);
foreach ($keys as $k) {
if (!isset($array[$k]) || !is_array($array[$k])) {
$array[$k] = [];
}
$array = &$array[$k];
}
// Add value to path
$array[] = $value;
$this->setAttribute('params', $this->getParamsAsJsonString());
return $this;
}
}