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