-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.php
489 lines (431 loc) · 9.55 KB
/
helpers.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
use App\Auth\Auth;
use App\Support\Config;
use App\Support\Facades\Facade;
use App\Support\LoadEnvironmentVariables;
use App\Support\Url;
use App\Translation\Translation;
use App\Util\Cart;
use App\Util\Response;
use Carbon\Carbon;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;
/**
* Get something out of the environment, if the given key
* was not set in the environment, return null or the given
* default value
*
* @param $key
* @param null $fallback
* @return string|null
*/
function env($key, $fallback = null)
{
if (!LoadEnvironmentVariables::hasBeenBootstrapped()) {
(new LoadEnvironmentVariables)->bootstrap(__DIR__);
}
if (isset($_ENV[$key])) {
return $_ENV[$key];
}
return $fallback;
}
/**
* Helper function for starting the session if it is
* not yet started
*
* @return void
*/
function start_session_if_not_exists()
{
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
}
/**
* Helper function to determine if an option inside a <select> should be active
*
* @param $parameter
* @param $option
* @param string $if_true
* @param string $if_false
* @return mixed|string
*/
function option_is_active($parameter, $option, $if_true = 'selected', $if_false = '')
{
if ($parameter === $option) {
return $if_true;
}
return $if_false;
}
/**
* Get a parameter from $_GET with null checking
*
* @param $key
* @param null $fallback
* @return mixed|null
*/
function get_parameter($key, $fallback = null)
{
if (isset($_GET[$key]) && !empty($_GET[$key])) {
return $_GET[$key];
}
return $fallback;
}
/**
* Get a parameter from $_POST with null checking
*
* @param $key
* @param null $fallback
* @return mixed|null
*/
function post_parameter($key, $fallback = null)
{
if (isset($_POST[$key]) && !empty($_POST[$key])) {
return $_POST[$key];
}
return $fallback;
}
/**
* Generate a query string from $params and exclude values in $except
*
* @param array $params
* @param array $except
* @return array
*/
function params_except($params = [], $except = [])
{
if (is_null($params)) {
$params = $_GET;
}
$bag = [];
foreach ($params as $key => $value) {
if (is_array($value)) {
foreach ($value as $request_key => $request_value) {
if (!str_arr_contains($key, $except)) {
$bag[$key . "[$request_key]"] = $request_value;
}
}
}
if (!in_array($key, $except)) {
$bag[$key] = $value;
}
};
return $bag;
}
/**
* Include existing get parameters by using hidden inputs (Ugh..)
*
* We should probably find a better way to do this
* but when creating this it was 02:00 at night
* so I could not be bothered.
*
* @param array $params
*/
function include_existing_parameters($params = [])
{
foreach ($params as $key => $value) {
if (is_array($value)) {
continue;
}
$key = htmlspecialchars($key);
$value = htmlspecialchars($value);
print("<input type='hidden' name='$key' value='$value'/>");
}
}
/**
* Translate a string
*
* @param $key
* @param array $replace
* @param null $fallback
* @return mixed
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function __($key, array $replace = [], $fallback = null): mixed
{
return app(Translation::class)->translate($key, $replace, $fallback);
}
/**
* Call isset and empty at once
*
* @param $arg
* @param $key
* @return bool
*/
function isset_not_empty($arg, $key)
{
return isset($arg[$key]) && !empty($arg[$key]);
}
/**
* Get the stock string
*
* @param $stock
* @return string
*/
function get_stock_string($stock)
{
if ($stock < 500) {
return 'stock.quantity.limited';
}
if ($stock > 1000) {
return 'stock.quantity.many';
}
return 'stock.quantity.plenty';
}
/**
* Get color associated with stock
*
* @param $stock
* @return string
*/
function get_stock_color($stock)
{
if ($stock < 500) {
return 'danger';
}
return 'primary';
}
/**
* Test if string contains value in array
*
* @param $needle
* @param $haystack
* @return bool
*/
function str_arr_contains($needle, $haystack)
{
foreach ($haystack as $value) {
if (str_contains($value, $needle)) {
return true;
}
}
return false;
}
/**
* Filter all special characters from string
*
* @param $arg
* @return string|string[]|null
*/
function sanitize_param($arg)
{
return preg_replace("/[^a-zA-Z0-9]/", "", $arg);
}
/**
* Prettify an custom field
*
* Example input: MySuperCoolInput
* Example Output: My super cool input
*
* @param $value
* @return string|string[]|null
*/
function prettify_custom_field($value)
{
return ucfirst(ltrim(strtolower(preg_replace('/(?<!\ )[A-Z]/', ' $0', $value))));
}
/**
* Get the product image or a backup image
*
* @param $item
* @param $key
* @param string $backup
* @return mixed|string
*/
function product_image_or_backup($item, $key, $backup = '/assets/img/products/no-image.jpg')
{
if (isset_not_empty($item, $key)) {
return '/assets/img/products/' . $item[$key];
}
return $backup;
}
/**
* Format price in Dutch format (€ 9.999,99)
* because Dutch is the only right format
*
* @param $number
* @param float $tax_rate
* @return string
*/
function format_price_with_tax($number, $tax_rate = 0.21)
{
return number_format(round($number * (1 + $tax_rate), 2), 2, ',', '.');
}
/**
* Wrap an item in an array
*
* @param $item
* @return array
*/
function array_wrap($item)
{
if (!is_array($item)) {
return [$item];
}
return $item;
}
/**
* Convert Fahrenheit to Celsius
*
* @param $given_value
* @return mixed
*/
function fahrenheit_to_celsius($given_value)
{
$celsius = 5 / 9 * ($given_value - 32);
return $celsius;
}
/**
* Shorthand for returning a (twig) response
*
* @param string|null $content
* @param int $status
* @param array $headers
* @return \Symfony\Component\HttpFoundation\Response | Response
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function response(string $content = null, int $status = 200, array $headers = []): \Symfony\Component\HttpFoundation\Response|Response
{
$response = app()->make(Response::class);
if (is_null($content)) {
return $response;
}
return $response->view($content, $status, $headers);
}
/**
* Shorthand for accessing cart
*
* @return Cart
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function cart(): Cart
{
return app(Cart::class);
}
/**
* Shorthand function to contract something from container
*
* @param string|null $accessor
* @return mixed|\Psr\Container\ContainerInterface
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function app(string $accessor = null)
{
if (is_null($accessor)) {
return Facade::getAppAccessor();
}
return Facade::getAppAccessor()->get($accessor);
}
/**
* Shorthand function to access request
*
* @param string|null $key
* @return Request
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function request()
{
return app(Request::class);
}
/**
* Shorthand function to access session from request
*
* @param string|null $key
* @return mixed
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function session(string $key = null)
{
if (is_null($key)) {
return app(Request::class)->getSession();
}
return app(Request::class)->get($key);
}
/**
* Shorthand function to access auth
*
* @return Auth
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function auth()
{
return app(Auth::class);
}
/**
* Shorthand function to create an URL instance
*
* @return Url
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function url(): Url
{
return app()->make(Url::class);
}
/**
* Get value from config
*
* @param null $key
* @return mixed|Config
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function config($key = null, $fallback = null): mixed
{
if (is_null($key)) {
return app(Config::class);
}
return app(Config::class)->get($key, $fallback);
}
/**
* Shorthand function to create a db instance
*
* @return mixed|EntityManager
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function db()
{
return app(EntityManager::class);
}
/**
* Utility function for getting only certain functions out of request parameters
*
* @param Request $request
* @param array $keys
* @return array
*/
function only(Request $request, array $keys)
{
$bag = [];
foreach ($keys as $key) {
$bag[$key] = $request->get($key);
}
return $bag;
}
/**
* Tap an instance of object, allows interacting with the object and returns the object with changes
*
* @param mixed $value
* @param callable $callback
* @return mixed
*/
function tap(mixed $value, Closure $callback)
{
$callback($value);
return $value;
}
function now($date = null)
{
if (is_null($date)) {
$date = Carbon::now();
}
return Carbon::make($date);
}