|
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
|
|
|
public static function formatMobiles($target) |
|
58
|
|
|
{ |
|
59
|
|
|
if (!is_array($target)) { |
|
60
|
|
|
return [$target]; |
|
61
|
|
|
} |
|
62
|
|
|
$list = []; |
|
63
|
|
|
$nation = $number = null; |
|
64
|
|
|
$count = count($target); |
|
65
|
|
|
if ($count === 2) { |
|
66
|
|
|
$firstItem = $target[0]; |
|
67
|
|
|
if (is_int($firstItem) && $firstItem > 0 && $firstItem <= 9999) { |
|
68
|
|
|
$nation = $firstItem; |
|
69
|
|
|
$number = $target[1]; |
|
70
|
|
|
} |
|
71
|
|
|
if (is_string($firstItem) && strlen($firstItem = trim($firstItem)) <= 4) { |
|
72
|
|
|
$nation = $firstItem; |
|
73
|
|
|
$number = $target[1]; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
if (!is_null($nation)) { |
|
77
|
|
|
return [compact('nation', 'number')]; |
|
78
|
|
|
} |
|
79
|
|
|
foreach ($target as $childTarget) { |
|
80
|
|
|
$childList = self::formatMobiles($childTarget); |
|
81
|
|
|
foreach ($childList as $childListItem) { |
|
82
|
|
|
array_push($list, $childListItem); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $list; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|