-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
302 lines (267 loc) · 6.55 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php namespace Nabeghe;
if (!function_exists('Nabeghe\arr_get')) {
/**
* @param array $array
* @param $key
* @param $default
* @return mixed|null
*/
function arr_get(array $array, $key, $default = null)
{
if (array_key_exists($key, $array)) {
return $array[$key];
}
return $default;
}
}
if (!function_exists('Nabeghe\arr_set')) {
/**
* @param array $array
* @param $key
* @param $value
* @param bool $force
* @return void
*/
function arr_set(array &$array, $key, $value, bool $force = true)
{
if ($force || !array_key_exists($key, $array)) {
$array[$key] = $value;
}
}
}
if (!function_exists('Nabeghe\arr_to_obj')) {
/**
* @param array $array
* @param bool $deep
* @return mixed|object
*/
function arr_to_obj(array $array, bool $deep = true)
{
if (!$deep) {
return (object) $array;
}
return @json_decode(json_encode($array), false);
}
}
if (!function_exists('Nabeghe\calc_execution_metrics')) {
/**
* @param $callback
* @return array
*/
function calc_execution_metrics($callback)
{
$time_start = microtime(true);
$memory_start = memory_get_usage();
$callback();
$time_end = microtime(true);
$memory_end = memory_get_usage();
return [
'execution_time' => $time_end - $time_start,
'memory_usage' => $memory_end - $memory_start,
];
}
}
if (!function_exists('Nabeghe\calc_execution_time')) {
/**
* @param $callback
* @return mixed
*/
function calc_execution_time($callback)
{
$time_start = microtime(true);
$callback();
$time_end = microtime(true);
return $time_end - $time_start;
}
}
if (!function_exists('Nabeghe\calc_memory_usage')) {
/**
* @param $callback
* @return int
*/
function calc_memory_usage($callback)
{
$memory_start = memory_get_usage();
$callback();
$memory_end = memory_get_usage();
return $memory_end - $memory_start;
}
}
if (!function_exists('Nabeghe\constant')) {
/**
* @param $name1
* @param $name2
* @return mixed
*/
function constant($name1, $name2 = null)
{
if ($name2 == '') {
return \constant($name1);
}
return \constant("$name1::$name2");
}
}
if (!function_exists('Nabeghe\define')) {
/**
* @param $name
* @param $value
* @return bool
*/
function define($name, $value)
{
if (!\defined($name)) {
\define($name, $value);
return true;
}
return false;
}
}
if (!function_exists('Nabeghe\defined')) {
/**
* @param $name1
* @param $name2
* @return bool
*/
function defined($name1, $name2 = null)
{
if ($name2 == '') {
return \defined($name1);
}
return \defined("$name1::$name2");
}
}
if (!function_exists('Nabeghe\get_request_execution_time')) {
/**
* Get the time elapsed so far during this PHP script.
* Uses REQUEST_TIME_FLOAT that appeared in PHP 5.4.0.
*
* @return float Seconds since the PHP script started.
* @link https://developer.wordpress.org/reference/functions/timer_float/
*/
function get_request_execution_time()
{
return microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
}
}
if (!function_exists('Nabeghe\join_paths')) {
/**
* Join the given paths together.
*
* @param string|null $basePath
* @param string ...$paths
* @return string
*/
function join_paths($basePath, ...$paths)
{
foreach ($paths as $index => $path) {
if (empty($path) && $path !== '0') {
unset($paths[$index]);
} else {
$paths[$index] = DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR);
}
}
return $basePath.implode('', $paths);
}
}
if (!function_exists('Nabeghe\obj_get')) {
/**
* @param object $object
* @param string $name
* @param $default
* @return mixed|null
*/
function obj_get(object $object, string $name, $default = null)
{
if (property_exists($object, $name)) {
return $object->$name;
}
return $default;
}
}
if (!function_exists('Nabeghe\obj_set')) {
/**
* @param object $object
* @param string $name
* @param $value
* @param bool $force
* @return void
*/
function obj_set(object $object, string $name, $value, bool $force = true)
{
if ($force || !property_exists($object, $name)) {
$object->$name = $value;
}
}
}
if (!function_exists('Nabeghe\percent')) {
/**
* @param $value
* @param $total
* @return float|int
*/
function percent($value, $total)
{
$value = floatval($value);
$total = floatval($total);
if (!$value || !$total) {
return 0;
}
return (($value * 100) / $total);
}
}
if (!function_exists('Nabeghe\sanitize_path')) {
/**
* @param $path
* @param int $blankcount
* @return string
*/
function sanitize_path($path, int &$blankcount = 0): string
{
if (empty($path)) {
return '';
}
$sanitized = '';
$blankcount = 0;
if (is_string($path)) {
$path_parts = explode('/', $path);
} elseif (is_array($path) && count($path) > 0) {
$path_parts = $path;
} else {
return '';
}
foreach ($path_parts as $path_part) {
$path_part = trim($path_part);
if ($path_part === '') {
$blankcount++;
} else {
$sanitized .= $path_part.'/';
}
}
$sanitized = ltrim($sanitized, '/');
$sanitized = rtrim($sanitized, '/');
return $sanitized;
}
}
if (!function_exists('Nabeghe\unpercent')) {
/**
* Calculates a percentage of a total value.<br>
* What is N percent of a total value?
* @param float|int $percent
* @param float|int $total
* @return float|int
*/
function unpercent($percent, $total)
{
return (($percent * $total) / 100);
}
}
if (!function_exists('Nabeghe\zeroone')) {
/**
* @param $value
* @return int
*/
function zeroone($value)
{
return $value ? 1 : 0;
}
}