-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
CustomHelper.php
77 lines (66 loc) · 2.04 KB
/
CustomHelper.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
<?php
use Ramsey\Uuid\Uuid;
$GLOBALS['correlationId'] = Uuid::uuid4()->toString();
/**
* @return string The current correlationID of the request.
*/
function correlationId(): string
{
return $GLOBALS['correlationId'];
}
/**
* Checks if a specific alert is already dismissed and thus should not be rendered anymore.
*
* @param $id string The ID of the alert
* @return bool True if the user dismissed it, false if they did not yet.
*/
function isAlertDismissed(string $id): bool
{
return isset($_COOKIE['alert-dismiss-' . $id]);
}
/**
* Get the initials from a name
*
* @param $name string
*/
function initials(string $name): string
{
$explode = explode(' ', $name);
if (count($explode) > 1) {
$explode = array_filter($explode, static fn($element) => !empty($element));
$result = implode('', array_map(static fn($element) => $element[0], $explode));
} else {
$result = substr($name, 0, 2);
}
return strtoupper($result);
}
/**
* @link https://stackoverflow.com/a/10797086
*/
function isValidBase64(string $string): bool
{
return base64_encode(base64_decode($string, true)) === $string;
}
/**
* @return array|array[][]|false[][]|string[][]|string[][][]
*/
function str_getcsv_assoc(string $csv_string, string $delimiter = ',', bool $skip_empty_lines = true, bool $trim_fields = true)
{
$enc = preg_replace('/(?<!")""/', '!!Q!!', $csv_string);
$enc = preg_replace_callback(
'/"(.*?)"/s',
static fn($field) => urlencode(utf8_encode($field[1])),
(string)$enc
);
$lines = preg_split($skip_empty_lines ? ($trim_fields ? '/( *\R)+/s' : '/\R+/s') : '/\R/s', (string)$enc);
return array_map(
static function ($line) use ($delimiter, $trim_fields) {
$fields = $trim_fields ? array_map('trim', explode($delimiter, $line)) : explode($delimiter, $line);
return array_map(
static fn($field) => str_replace('!!Q!!', '"', utf8_decode(urldecode($field))),
$fields
);
},
$lines
);
}