Skip to content

Commit b18e200

Browse files
committed
feat: add date and boolean formatting functions to helpers
1 parent 9e5846a commit b18e200

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

common/helpers.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,54 @@
22

33
if (!function_exists('color_value_format')) {
44
/**
5-
* @param string $value
6-
* @param string $color
5+
* Format a value with an optional color.
76
*
7+
* @param string $value
8+
* @param string|null $color
89
* @return string
910
*/
10-
function color_value_format(string $value, string $color): string
11+
function color_value_format(string $value, ?string $color = null): string
1112
{
12-
$value = str_replace(' ', ' \space ', $value);
13+
if (empty($color)) {
14+
return $value;
15+
}
1316

1417
return '$${\color{'.$color.'}'.$value.'}$$';
1518
}
1619
}
20+
21+
if (!function_exists('format_date')) {
22+
/**
23+
* Format a date string.
24+
*
25+
* @param string|null $date
26+
* @param string $format
27+
*
28+
* @return string|null
29+
*/
30+
function format_date(?string $date, string $format = 'Y-m-d'): ?string
31+
{
32+
if (!$date) {
33+
return null;
34+
}
35+
36+
try {
37+
return \Carbon\Carbon::parse($date)->format($format);
38+
} catch (\Exception $e) {
39+
return $date;
40+
}
41+
}
42+
}
43+
44+
if (!function_exists('format_boolean')) {
45+
/**
46+
* Format a boolean value.
47+
*
48+
* @param mixed $value
49+
* @return string
50+
*/
51+
function format_boolean($value): string
52+
{
53+
return filter_var($value, FILTER_VALIDATE_BOOLEAN) ? '' : '';
54+
}
55+
}

0 commit comments

Comments
 (0)