-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
helpers.php
218 lines (180 loc) · 6.65 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
<?php
namespace Filament\Support;
use Filament\Support\Facades\FilamentColor;
use Filament\Support\Facades\FilamentView;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Illuminate\Translation\MessageSelector;
use Illuminate\View\ComponentAttributeBag;
use NumberFormatter;
if (! function_exists('Filament\Support\format_money')) {
function format_money(float | int $money, string $currency, int $divideBy = 0): string
{
$formatter = new NumberFormatter(app()->getLocale(), NumberFormatter::CURRENCY);
if ($divideBy) {
$money /= $divideBy;
}
return $formatter->formatCurrency($money, $currency);
}
}
if (! function_exists('Filament\Support\format_number')) {
function format_number(float | int $number): string
{
$formatter = new NumberFormatter(app()->getLocale(), NumberFormatter::DECIMAL);
return $formatter->format($number);
}
}
if (! function_exists('Filament\Support\get_model_label')) {
function get_model_label(string $model): string
{
return (string) str(class_basename($model))
->kebab()
->replace('-', ' ');
}
}
if (! function_exists('Filament\Support\locale_has_pluralization')) {
function locale_has_pluralization(): bool
{
return (new MessageSelector())->getPluralIndex(app()->getLocale(), 10) > 0;
}
}
if (! function_exists('Filament\Support\get_color_css_variables')) {
/**
* @param string | array{50: string, 100: string, 200: string, 300: string, 400: string, 500: string, 600: string, 700: string, 800: string, 900: string, 950: string} | null $color
* @param array<int> $shades
*/
function get_color_css_variables(string | array | null $color, array $shades, ?string $alias = null): ?string
{
if ($color === null) {
return null;
}
if ($alias !== null) {
if (($overridingShades = FilamentColor::getOverridingShades($alias)) !== null) {
$shades = $overridingShades;
}
if ($addedShades = FilamentColor::getAddedShades($alias)) {
$shades = [...$shades, ...$addedShades];
}
if ($removedShades = FilamentColor::getRemovedShades($alias)) {
$shades = array_diff($shades, $removedShades);
}
}
$variables = [];
if (is_string($color)) {
foreach ($shades as $shade) {
$variables[] = "--c-{$shade}:var(--{$color}-{$shade})";
}
}
if (is_array($color)) {
foreach ($shades as $shade) {
$variables[] = "--c-{$shade}:{$color[$shade]}";
}
}
return implode(';', $variables);
}
}
if (! function_exists('Filament\Support\prepare_inherited_attributes')) {
function prepare_inherited_attributes(ComponentAttributeBag $attributes): ComponentAttributeBag
{
$originalAttributes = $attributes->getAttributes();
$attributes->setAttributes(
collect($originalAttributes)
->filter(fn ($value, string $name): bool => ! str($name)->startsWith(['x-', 'data-']))
->mapWithKeys(fn ($value, string $name): array => [Str::camel($name) => $value])
->merge($originalAttributes)
->all(),
);
return $attributes;
}
}
if (! function_exists('Filament\Support\is_slot_empty')) {
function is_slot_empty(?Htmlable $slot): bool
{
if ($slot === null) {
return true;
}
return trim(
str_replace(
[
'<!-- __BLOCK__ -->',
'<!-- __ENDBLOCK__ -->',
'<!--[if BLOCK]><![endif]-->',
'<!--[if ENDBLOCK]><![endif]-->',
],
'',
$slot->toHtml()
),
) === '';
}
}
if (! function_exists('Filament\Support\is_app_url')) {
function is_app_url(string $url): bool
{
return str($url)->startsWith(request()->root());
}
}
if (! function_exists('Filament\Support\generate_href_html')) {
function generate_href_html(?string $url, bool $shouldOpenInNewTab = false): Htmlable
{
if (blank($url)) {
return new HtmlString('');
}
$html = "href=\"{$url}\"";
if ($shouldOpenInNewTab) {
$html .= ' target="_blank"';
} elseif (FilamentView::hasSpaMode() && is_app_url($url)) {
$html .= ' wire:navigate';
}
return new HtmlString($html);
}
}
if (! function_exists('Filament\Support\generate_search_column_expression')) {
/**
* @internal This function is only to be used internally by Filament and is subject to change at any time. Please do not use this function in your own code.
*/
function generate_search_column_expression(string $column, ?bool $isSearchForcedCaseInsensitive, Connection $databaseConnection): string | Expression
{
$driverName = $databaseConnection->getDriverName();
$column = match ($driverName) {
'pgsql' => "{$column}::text",
default => $column,
};
$isSearchForcedCaseInsensitive ??= match ($driverName) {
'pgsql' => true,
default => str($column)->contains('json_extract('),
};
if ($isSearchForcedCaseInsensitive) {
$column = "lower({$column})";
}
$collation = $databaseConnection->getConfig('search_collation');
if (filled($collation)) {
$column = "{$column} collate {$collation}";
}
if (
str($column)->contains('(') || // This checks if the column name probably contains a raw expression like `lower()` or `json_extract()`.
filled($collation)
) {
return new Expression($column);
}
return $column;
}
}
if (! function_exists('Filament\Support\generate_search_term_expression')) {
/**
* @internal This function is only to be used internally by Filament and is subject to change at any time. Please do not use this function in your own code.
*/
function generate_search_term_expression(string $search, ?bool $isSearchForcedCaseInsensitive, Connection $databaseConnection): string
{
$isSearchForcedCaseInsensitive ??= match ($databaseConnection->getDriverName()) {
'pgsql' => true,
default => false,
};
if (! $isSearchForcedCaseInsensitive) {
return $search;
}
return Str::lower($search);
}
}