1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if (!function_exists('pr')) { |
4
|
|
|
/** |
5
|
|
|
* PR |
6
|
|
|
* Prints data wrapped in pre tags. Perfect for reading arrays |
7
|
|
|
* |
8
|
|
|
* @param mixed $data |
9
|
|
|
*/ |
10
|
|
|
function pr($data) { |
11
|
|
|
if (php_sapi_name() === 'cli') { |
12
|
|
|
echo "\n"; |
13
|
|
|
print_r($data); |
14
|
|
|
echo "\n"; |
15
|
|
|
} else { |
16
|
|
|
echo "<pre style='font-size: 8pt; text-align: left; background-color: #ffffff;'>"; |
17
|
|
|
print_r($data); |
18
|
|
|
echo "</pre>"; |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if (!function_exists('array_get')) { |
24
|
|
|
/** |
25
|
|
|
* Get value from array without boilerplate error checking |
26
|
|
|
* |
27
|
|
|
* @param array $array |
28
|
|
|
* @param string $key |
29
|
|
|
* @param mixed $default |
30
|
|
|
* |
31
|
|
|
* @return mixed|null |
32
|
|
|
*/ |
33
|
|
|
function array_get($array, $key, $default = null) { |
34
|
|
|
if (is_array($array) && isset($array[ $key ])) { |
35
|
|
|
return $array[ $key ]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $default; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (!function_exists('studly_case')) { |
43
|
|
|
/** |
44
|
|
|
* Convert string to StudlyCase |
45
|
|
|
* |
46
|
|
|
* @param string $str |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
function studly_case($str) { |
51
|
|
|
$words = str_replace(['_', '-'], ' ', $str); |
52
|
|
|
|
53
|
|
|
return str_replace(' ', '', ucwords($words)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!function_exists('camel_case')) { |
58
|
|
|
/** |
59
|
|
|
* convert string to camelCase |
60
|
|
|
* |
61
|
|
|
* @param string $str |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
function camel_case($str) { |
66
|
|
|
return lcfirst(studly_case($str)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (!function_exists('snake_case')) { |
71
|
|
|
/** |
72
|
|
|
* @param $str |
73
|
|
|
* |
74
|
|
|
* @return null|string|string[] |
75
|
|
|
*/ |
76
|
|
|
function snake_case($str) { |
77
|
|
|
|
78
|
|
|
$str = str_replace(['-', ' '], '_', $str); |
79
|
|
|
$str = preg_replace('/(.)(?=[A-Z])/u', '$1_', $str); |
80
|
|
|
|
81
|
|
|
return strtolower($str); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (!function_exists('merge')) { |
86
|
|
|
/** |
87
|
|
|
* merge n vars into an array without endless boilerplate error checking |
88
|
|
|
* |
89
|
|
|
* example: |
90
|
|
|
* |
91
|
|
|
* merge([1,2],['a' => 'b'],'just a string', null); |
92
|
|
|
* |
93
|
|
|
* [ |
94
|
|
|
* 0 => 1, |
95
|
|
|
* 1 => 2, |
96
|
|
|
* 'a' => 'b', |
97
|
|
|
* 2 => 'just a string |
98
|
|
|
* ] |
99
|
|
|
* |
100
|
|
|
* |
101
|
|
|
* @param array ...$vars |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
function merge(...$vars) { |
106
|
|
|
if (empty($vars)) { |
107
|
|
|
return []; |
108
|
|
|
} |
109
|
|
|
$result = []; |
110
|
|
|
foreach ($vars as $arr) { |
111
|
|
|
if (is_null($arr)) continue; |
112
|
|
|
|
113
|
|
|
$arr = array_wrap($arr); |
114
|
|
|
$result = array_merge($result, $arr); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $result; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!function_exists('merge_non_empty')) { |
122
|
|
|
/** |
123
|
|
|
* merge() and discard all empty() values |
124
|
|
|
* |
125
|
|
|
* @param array ...$vars |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
function merge_non_empty(...$vars) { |
130
|
|
|
if (empty($vars)) { |
131
|
|
|
return []; |
132
|
|
|
} |
133
|
|
|
$result = []; |
134
|
|
|
foreach ($vars as $arr) { |
135
|
|
|
if (!empty($arr)) { |
136
|
|
|
$result = merge($result, $arr); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $result; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (!function_exists('get_object_as_array')) { |
144
|
|
|
/** |
145
|
|
|
* Return an array of all public properties of an object |
146
|
|
|
* |
147
|
|
|
* @param object $obj |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
function get_object_as_array($obj) { |
152
|
|
|
$array = (array)$obj; |
153
|
|
|
foreach ($array as $key => $value) { |
154
|
|
|
// Private and protected property names contain "\0" |
155
|
|
|
// when cast to array. Discard them here |
156
|
|
|
if (strpos($key, "\0") !== false) { |
157
|
|
|
unset($array[ $key ]); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $array; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (!function_exists('array_wrap')) { |
166
|
|
|
/** |
167
|
|
|
* If the given value is not an array |
168
|
|
|
* it will be returned wrapped in an array |
169
|
|
|
* |
170
|
|
|
* If the value is already an array it will |
171
|
|
|
* be returned as is |
172
|
|
|
* |
173
|
|
|
* If the value is null, an empty array will |
174
|
|
|
* be returned |
175
|
|
|
* |
176
|
|
|
* @param mixed $value |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
function array_wrap($value) { |
181
|
|
|
if (is_null($value)) { |
182
|
|
|
return []; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (is_array($value)) { |
186
|
|
|
return $value; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return [$value]; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if (!function_exists('env')) { |
194
|
|
|
function env($var_name, $default = null) { |
195
|
|
|
if (!isset($_ENV[ $var_name ])) { |
196
|
|
|
return $default; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return getenv($var_name); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |