|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Toplan\PhpSms; |
|
4
|
|
|
|
|
5
|
|
|
use SuperClosure\Serializer; |
|
6
|
|
|
|
|
7
|
|
|
class Util |
|
8
|
|
|
{ |
|
9
|
|
|
protected static $closureSerializer = null; |
|
10
|
|
|
|
|
11
|
|
|
public static function operateArray(array &$array, $key, $value = null, $default = null, \Closure $setter = null, $override = false, $willOverride = null, $isSet = false) |
|
12
|
|
|
{ |
|
13
|
|
|
if (!$isSet && ($key === null || is_string($key) || is_int($key)) && $value === null) { |
|
14
|
|
|
return $key === null ? $array : |
|
15
|
|
|
(isset($array[$key]) ? $array[$key] : $default); |
|
16
|
|
|
} |
|
17
|
|
|
if ($override) { |
|
18
|
|
|
if (is_callable($willOverride)) { |
|
19
|
|
|
call_user_func_array($willOverride, [$array]); |
|
20
|
|
|
} |
|
21
|
|
|
$array = []; |
|
22
|
|
|
} |
|
23
|
|
|
if (is_array($key) || is_object($key)) { |
|
24
|
|
|
foreach ($key as $k => $v) { |
|
25
|
|
|
self::operateArray($array, $k, $v, $default, $setter, false, null, true); |
|
26
|
|
|
} |
|
27
|
|
|
} elseif (is_callable($setter)) { |
|
28
|
|
|
call_user_func_array($setter, [$key, $value]); |
|
29
|
|
|
} else { |
|
30
|
|
|
$array[$key] = $value; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return $array; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function pullFromArray(array &$options, $key) |
|
37
|
|
|
{ |
|
38
|
|
|
$value = null; |
|
39
|
|
|
if (!isset($options[$key])) { |
|
40
|
|
|
return $value; |
|
41
|
|
|
} |
|
42
|
|
|
$value = $options[$key]; |
|
43
|
|
|
unset($options[$key]); |
|
44
|
|
|
|
|
45
|
|
|
return $value; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function getClosureSerializer() |
|
49
|
|
|
{ |
|
50
|
|
|
if (empty(self::$closureSerializer)) { |
|
51
|
|
|
self::$closureSerializer = new Serializer(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return self::$closureSerializer; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|