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