mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Merge pull request #11545 from filamentphp/number-formatting
chore: Laravel Number:: formatting
This commit is contained in:
@@ -25,11 +25,10 @@ use Illuminate\Foundation\Bus\PendingChain;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Number;
|
||||
use Illuminate\Support\Str;
|
||||
use Livewire\Component;
|
||||
|
||||
use function Filament\Support\format_number;
|
||||
|
||||
trait CanExportRecords
|
||||
{
|
||||
/**
|
||||
@@ -127,7 +126,7 @@ trait CanExportRecords
|
||||
Notification::make()
|
||||
->title(__('filament-actions::export.notifications.max_rows.title'))
|
||||
->body(trans_choice('filament-actions::export.notifications.max_rows.body', $maxRows, [
|
||||
'count' => format_number($maxRows),
|
||||
'count' => Number::format($maxRows),
|
||||
]))
|
||||
->danger()
|
||||
->send();
|
||||
@@ -239,7 +238,7 @@ trait CanExportRecords
|
||||
Notification::make()
|
||||
->title($action->getSuccessNotificationTitle())
|
||||
->body(trans_choice('filament-actions::export.notifications.started.body', $export->total_rows, [
|
||||
'count' => format_number($export->total_rows),
|
||||
'count' => Number::format($export->total_rows),
|
||||
]))
|
||||
->success()
|
||||
->send();
|
||||
|
||||
@@ -26,6 +26,7 @@ use Illuminate\Filesystem\AwsS3V3Adapter;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Number;
|
||||
use League\Csv\Info;
|
||||
use League\Csv\Reader as CsvReader;
|
||||
use League\Csv\Statement;
|
||||
@@ -34,8 +35,6 @@ use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
||||
use SplTempFileObject;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
use function Filament\Support\format_number;
|
||||
|
||||
trait CanImportRecords
|
||||
{
|
||||
/**
|
||||
@@ -181,7 +180,7 @@ trait CanImportRecords
|
||||
Notification::make()
|
||||
->title(__('filament-actions::import.notifications.max_rows.title'))
|
||||
->body(trans_choice('filament-actions::import.notifications.max_rows.body', $maxRows, [
|
||||
'count' => format_number($maxRows),
|
||||
'count' => Number::format($maxRows),
|
||||
]))
|
||||
->danger()
|
||||
->send();
|
||||
@@ -271,7 +270,7 @@ trait CanImportRecords
|
||||
fn (Notification $notification) => $notification->actions([
|
||||
NotificationAction::make('downloadFailedRowsCsv')
|
||||
->label(trans_choice('filament-actions::import.notifications.completed.actions.download_failed_rows_csv.label', $failedRowsCount, [
|
||||
'count' => format_number($failedRowsCount),
|
||||
'count' => Number::format($failedRowsCount),
|
||||
]))
|
||||
->color('danger')
|
||||
->url(route('filament.imports.failed-rows.download', ['import' => $import], absolute: false), shouldOpenInNewTab: true)
|
||||
@@ -285,7 +284,7 @@ trait CanImportRecords
|
||||
Notification::make()
|
||||
->title($action->getSuccessNotificationTitle())
|
||||
->body(trans_choice('filament-actions::import.notifications.started.body', $import->total_rows, [
|
||||
'count' => format_number($import->total_rows),
|
||||
'count' => Number::format($import->total_rows),
|
||||
]))
|
||||
->success()
|
||||
->send();
|
||||
|
||||
@@ -58,17 +58,39 @@ TextEntry::make('created_at')
|
||||
|
||||
## Number formatting
|
||||
|
||||
The `numeric()` method allows you to format an entry as a number, using PHP's `number_format()`:
|
||||
The `numeric()` method allows you to format an entry as a number:
|
||||
|
||||
```php
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
|
||||
TextEntry::make('stock')
|
||||
->numeric(
|
||||
decimalPlaces: 0,
|
||||
decimalSeparator: '.',
|
||||
thousandsSeparator: ',',
|
||||
)
|
||||
->numeric()
|
||||
```
|
||||
|
||||
If you would like to customize the number of decimal places used to format the number with, you can use the `decimalPlaces` argument:
|
||||
|
||||
```php
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
|
||||
TextEntry::make('stock')
|
||||
->numeric(decimalPlaces: 0)
|
||||
```
|
||||
|
||||
By default, your app's locale will be used to format the number suitably. If you would like to customize the locale used, you can pass it to the `locale` argument:
|
||||
|
||||
```php
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
|
||||
TextEntry::make('stock')
|
||||
->numeric(locale: 'nl')
|
||||
```
|
||||
|
||||
Alternatively, you can set the default locale used across your app using the `Number::useLocale()` method in the `boot()` method of a service provider:
|
||||
|
||||
```php
|
||||
use Illuminate\Support\Number;
|
||||
|
||||
Number::useLocale('nl');
|
||||
```
|
||||
|
||||
## Currency formatting
|
||||
@@ -91,6 +113,23 @@ TextEntry::make('price')
|
||||
->money('EUR', divideBy: 100)
|
||||
```
|
||||
|
||||
By default, your app's locale will be used to format the money suitably. If you would like to customize the locale used, you can pass it to the `locale` argument:
|
||||
|
||||
```php
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
|
||||
TextEntry::make('price')
|
||||
->money(currency: 'EUR', locale: 'nl')
|
||||
```
|
||||
|
||||
Alternatively, you can set the default locale used across your app using the `Number::useLocale()` method in the `boot()` method of a service provider:
|
||||
|
||||
```php
|
||||
use Illuminate\Support\Number;
|
||||
|
||||
Number::useLocale('nl');
|
||||
```
|
||||
|
||||
## Limiting text length
|
||||
|
||||
You may `limit()` the length of the entry's value:
|
||||
|
||||
@@ -6,13 +6,13 @@ use Closure;
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
use Filament\Infolists\Infolist;
|
||||
use Filament\Support\Contracts\HasLabel as LabelInterface;
|
||||
use Filament\Support\Enums\ArgumentValue;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Contracts\Support\Htmlable;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Number;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use function Filament\Support\format_money;
|
||||
use function Filament\Support\format_number;
|
||||
|
||||
trait CanFormatState
|
||||
{
|
||||
protected ?Closure $formatStateUsing = null;
|
||||
@@ -99,28 +99,11 @@ trait CanFormatState
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function money(string | Closure | null $currency = null, int $divideBy = 0): static
|
||||
public function money(string | Closure | null $currency = null, int $divideBy = 0, string | Closure | null $locale = null): static
|
||||
{
|
||||
$this->isMoney = true;
|
||||
|
||||
$this->formatStateUsing(static function (TextEntry $component, $state) use ($currency, $divideBy): ?string {
|
||||
if (blank($state)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$currency = $component->evaluate($currency) ?? Infolist::$defaultCurrency;
|
||||
|
||||
return format_money($state, $currency, $divideBy);
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function numeric(int | Closure | null $decimalPlaces = null, string | Closure | null $decimalSeparator = '.', string | Closure | null $thousandsSeparator = ','): static
|
||||
{
|
||||
$this->isNumeric = true;
|
||||
|
||||
$this->formatStateUsing(static function (TextEntry $component, $state) use ($decimalPlaces, $decimalSeparator, $thousandsSeparator): ?string {
|
||||
$this->formatStateUsing(static function (TextEntry $component, $state) use ($currency, $divideBy, $locale): ?string {
|
||||
if (blank($state)) {
|
||||
return null;
|
||||
}
|
||||
@@ -129,16 +112,47 @@ trait CanFormatState
|
||||
return $state;
|
||||
}
|
||||
|
||||
if ($decimalPlaces === null) {
|
||||
return format_number($state);
|
||||
$currency = $component->evaluate($currency) ?? Table::$defaultCurrency;
|
||||
|
||||
if ($divideBy) {
|
||||
$state /= $divideBy;
|
||||
}
|
||||
|
||||
return number_format(
|
||||
$state,
|
||||
$component->evaluate($decimalPlaces),
|
||||
$component->evaluate($decimalSeparator),
|
||||
$component->evaluate($thousandsSeparator),
|
||||
);
|
||||
return Number::currency($state, $currency, $component->evaluate($locale));
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function numeric(int | Closure | null $decimalPlaces = null, string | Closure | null | ArgumentValue $decimalSeparator = ArgumentValue::Default, string | Closure | null | ArgumentValue $thousandsSeparator = ArgumentValue::Default, int | Closure | null $maxDecimalPlaces = null, string | Closure | null $locale = null): static
|
||||
{
|
||||
$this->isNumeric = true;
|
||||
|
||||
$this->formatStateUsing(static function (TextEntry $component, $state) use ($decimalPlaces, $decimalSeparator, $locale, $maxDecimalPlaces, $thousandsSeparator): ?string {
|
||||
if (blank($state)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! is_numeric($state)) {
|
||||
return $state;
|
||||
}
|
||||
|
||||
$decimalSeparator = $component->evaluate($decimalSeparator);
|
||||
$thousandsSeparator = $component->evaluate($thousandsSeparator);
|
||||
|
||||
if (
|
||||
($decimalSeparator !== ArgumentValue::Default) ||
|
||||
($thousandsSeparator !== ArgumentValue::Default)
|
||||
) {
|
||||
return number_format(
|
||||
$state,
|
||||
$component->evaluate($decimalPlaces),
|
||||
$decimalSeparator === ArgumentValue::Default ? '.' : $decimalSeparator,
|
||||
$thousandsSeparator === ArgumentValue::Default ? ',' : $thousandsSeparator,
|
||||
);
|
||||
}
|
||||
|
||||
return Number::format($state, $decimalPlaces, $maxDecimalPlaces, $component->evaluate($locale));
|
||||
});
|
||||
|
||||
return $this;
|
||||
|
||||
@@ -51,9 +51,9 @@
|
||||
'filament::components/pagination.overview',
|
||||
$paginator->total(),
|
||||
[
|
||||
'first' => \Filament\Support\format_number($paginator->firstItem() ?? 0),
|
||||
'last' => \Filament\Support\format_number($paginator->lastItem() ?? 0),
|
||||
'total' => \Filament\Support\format_number($paginator->total()),
|
||||
'first' => \Illuminate\Support\Number::format($paginator->firstItem() ?? 0),
|
||||
'last' => \Illuminate\Support\Number::format($paginator->lastItem() ?? 0),
|
||||
'total' => \Illuminate\Support\Number::format($paginator->total()),
|
||||
],
|
||||
)
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Support\Enums;
|
||||
|
||||
enum ArgumentValue
|
||||
{
|
||||
case Default;
|
||||
}
|
||||
@@ -8,30 +8,32 @@ use Illuminate\Contracts\Support\Htmlable;
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Database\Query\Expression;
|
||||
use Illuminate\Support\HtmlString;
|
||||
use Illuminate\Support\Number;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Translation\MessageSelector;
|
||||
use Illuminate\View\ComponentAttributeBag;
|
||||
use NumberFormatter;
|
||||
|
||||
if (! function_exists('Filament\Support\format_money')) {
|
||||
/**
|
||||
* @deprecated Use `Illuminate\Support\Number::currency()` instead.
|
||||
*/
|
||||
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);
|
||||
return Number::currency($money, $currency);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('Filament\Support\format_number')) {
|
||||
/**
|
||||
* @deprecated Use `Illuminate\Support\Number::format()` instead.
|
||||
*/
|
||||
function format_number(float | int $number): string
|
||||
{
|
||||
$formatter = new NumberFormatter(app()->getLocale(), NumberFormatter::DECIMAL);
|
||||
|
||||
return $formatter->format($number);
|
||||
return Number::format($number);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -84,17 +84,39 @@ TextColumn::make('created_at')
|
||||
|
||||
## Number formatting
|
||||
|
||||
The `numeric()` method allows you to format a column as a number, using PHP's `number_format()`:
|
||||
The `numeric()` method allows you to format an entry as a number:
|
||||
|
||||
```php
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
|
||||
TextColumn::make('stock')
|
||||
->numeric(
|
||||
decimalPlaces: 0,
|
||||
decimalSeparator: '.',
|
||||
thousandsSeparator: ',',
|
||||
)
|
||||
->numeric()
|
||||
```
|
||||
|
||||
If you would like to customize the number of decimal places used to format the number with, you can use the `decimalPlaces` argument:
|
||||
|
||||
```php
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
|
||||
TextColumn::make('stock')
|
||||
->numeric(decimalPlaces: 0)
|
||||
```
|
||||
|
||||
By default, your app's locale will be used to format the number suitably. If you would like to customize the locale used, you can pass it to the `locale` argument:
|
||||
|
||||
```php
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
|
||||
TextColumn::make('stock')
|
||||
->numeric(locale: 'nl')
|
||||
```
|
||||
|
||||
Alternatively, you can set the default locale used across your app using the `Number::useLocale()` method in the `boot()` method of a service provider:
|
||||
|
||||
```php
|
||||
use Illuminate\Support\Number;
|
||||
|
||||
Number::useLocale('nl');
|
||||
```
|
||||
|
||||
## Currency formatting
|
||||
@@ -117,6 +139,23 @@ TextColumn::make('price')
|
||||
->money('EUR', divideBy: 100)
|
||||
```
|
||||
|
||||
By default, your app's locale will be used to format the money suitably. If you would like to customize the locale used, you can pass it to the `locale` argument:
|
||||
|
||||
```php
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
|
||||
TextColumn::make('price')
|
||||
->money('EUR', locale: 'nl')
|
||||
```
|
||||
|
||||
Alternatively, you can set the default locale used across your app using the `Number::useLocale()` method in the `boot()` method of a service provider:
|
||||
|
||||
```php
|
||||
use Illuminate\Support\Number;
|
||||
|
||||
Number::useLocale('nl');
|
||||
```
|
||||
|
||||
## Limiting text length
|
||||
|
||||
You may `limit()` the length of the cell's value:
|
||||
|
||||
@@ -221,7 +221,17 @@ In this example, the table will calculate how many posts are published.
|
||||
|
||||
### Number formatting
|
||||
|
||||
The `numeric()` method allows you to format a summary as a number, using PHP's `number_format()`:
|
||||
The `numeric()` method allows you to format an entry as a number:
|
||||
|
||||
```php
|
||||
use Filament\Tables\Columns\Summarizers\Average;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
|
||||
TextColumn::make('rating')
|
||||
->summarize(Average::make()->numeric())
|
||||
```
|
||||
|
||||
If you would like to customize the number of decimal places used to format the number with, you can use the `decimalPlaces` argument:
|
||||
|
||||
```php
|
||||
use Filament\Tables\Columns\Summarizers\Average;
|
||||
@@ -230,11 +240,29 @@ use Filament\Tables\Columns\TextColumn;
|
||||
TextColumn::make('rating')
|
||||
->summarize(Average::make()->numeric(
|
||||
decimalPlaces: 0,
|
||||
decimalSeparator: '.',
|
||||
thousandsSeparator: ',',
|
||||
))
|
||||
```
|
||||
|
||||
By default, your app's locale will be used to format the number suitably. If you would like to customize the locale used, you can pass it to the `locale` argument:
|
||||
|
||||
```php
|
||||
use Filament\Tables\Columns\Summarizers\Average;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
|
||||
TextColumn::make('rating')
|
||||
->summarize(Average::make()->numeric(
|
||||
locale: 'nl',
|
||||
))
|
||||
```
|
||||
|
||||
Alternatively, you can set the default locale used across your app using the `Number::useLocale()` method in the `boot()` method of a service provider:
|
||||
|
||||
```php
|
||||
use Illuminate\Support\Number;
|
||||
|
||||
Number::useLocale('nl');
|
||||
```
|
||||
|
||||
### Currency formatting
|
||||
|
||||
The `money()` method allows you to easily format monetary values, in any currency:
|
||||
@@ -257,6 +285,24 @@ TextColumn::make('price')
|
||||
->summarize(Sum::make()->money('EUR', divideBy: 100))
|
||||
```
|
||||
|
||||
By default, your app's locale will be used to format the money suitably. If you would like to customize the locale used, you can pass it to the `locale` argument:
|
||||
|
||||
```php
|
||||
use Filament\Tables\Columns\Summarizers\Average;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
|
||||
TextColumn::make('price')
|
||||
->summarize(Sum::make()->money(currency: 'EUR', locale: 'nl'))
|
||||
```
|
||||
|
||||
Alternatively, you can set the default locale used across your app using the `Number::useLocale()` method in the `boot()` method of a service provider:
|
||||
|
||||
```php
|
||||
use Illuminate\Support\Number;
|
||||
|
||||
Number::useLocale('nl');
|
||||
```
|
||||
|
||||
### Limiting text length
|
||||
|
||||
You may `limit()` the length of the summary's value:
|
||||
|
||||
@@ -4,15 +4,14 @@ namespace Filament\Tables\Columns\Concerns;
|
||||
|
||||
use Closure;
|
||||
use Filament\Support\Contracts\HasLabel as LabelInterface;
|
||||
use Filament\Support\Enums\ArgumentValue;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Contracts\Support\Htmlable;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Number;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use function Filament\Support\format_money;
|
||||
use function Filament\Support\format_number;
|
||||
|
||||
trait CanFormatState
|
||||
{
|
||||
protected ?Closure $formatStateUsing = null;
|
||||
@@ -99,28 +98,11 @@ trait CanFormatState
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function money(string | Closure | null $currency = null, int $divideBy = 0): static
|
||||
public function money(string | Closure | null $currency = null, int $divideBy = 0, string | Closure | null $locale = null): static
|
||||
{
|
||||
$this->isMoney = true;
|
||||
|
||||
$this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $divideBy): ?string {
|
||||
if (blank($state)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$currency = $column->evaluate($currency) ?? Table::$defaultCurrency;
|
||||
|
||||
return format_money($state, $currency, $divideBy);
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function numeric(int | Closure | null $decimalPlaces = null, string | Closure | null $decimalSeparator = '.', string | Closure | null $thousandsSeparator = ','): static
|
||||
{
|
||||
$this->isNumeric = true;
|
||||
|
||||
$this->formatStateUsing(static function (TextColumn $column, $state) use ($decimalPlaces, $decimalSeparator, $thousandsSeparator): ?string {
|
||||
$this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $divideBy, $locale): ?string {
|
||||
if (blank($state)) {
|
||||
return null;
|
||||
}
|
||||
@@ -129,16 +111,47 @@ trait CanFormatState
|
||||
return $state;
|
||||
}
|
||||
|
||||
if ($decimalPlaces === null) {
|
||||
return format_number($state);
|
||||
$currency = $column->evaluate($currency) ?? Table::$defaultCurrency;
|
||||
|
||||
if ($divideBy) {
|
||||
$state /= $divideBy;
|
||||
}
|
||||
|
||||
return number_format(
|
||||
$state,
|
||||
$column->evaluate($decimalPlaces),
|
||||
$column->evaluate($decimalSeparator),
|
||||
$column->evaluate($thousandsSeparator),
|
||||
);
|
||||
return Number::currency($state, $currency, $column->evaluate($locale));
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function numeric(int | Closure | null $decimalPlaces = null, string | Closure | null | ArgumentValue $decimalSeparator = ArgumentValue::Default, string | Closure | null | ArgumentValue $thousandsSeparator = ArgumentValue::Default, int | Closure | null $maxDecimalPlaces = null, string | Closure | null $locale = null): static
|
||||
{
|
||||
$this->isNumeric = true;
|
||||
|
||||
$this->formatStateUsing(static function (TextColumn $column, $state) use ($decimalPlaces, $decimalSeparator, $locale, $maxDecimalPlaces, $thousandsSeparator): ?string {
|
||||
if (blank($state)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! is_numeric($state)) {
|
||||
return $state;
|
||||
}
|
||||
|
||||
$decimalSeparator = $column->evaluate($decimalSeparator);
|
||||
$thousandsSeparator = $column->evaluate($thousandsSeparator);
|
||||
|
||||
if (
|
||||
($decimalSeparator !== ArgumentValue::Default) ||
|
||||
($thousandsSeparator !== ArgumentValue::Default)
|
||||
) {
|
||||
return number_format(
|
||||
$state,
|
||||
$column->evaluate($decimalPlaces),
|
||||
$decimalSeparator === ArgumentValue::Default ? '.' : $decimalSeparator,
|
||||
$thousandsSeparator === ArgumentValue::Default ? ',' : $thousandsSeparator,
|
||||
);
|
||||
}
|
||||
|
||||
return Number::format($state, $decimalPlaces, $maxDecimalPlaces, locale: $column->evaluate($locale));
|
||||
});
|
||||
|
||||
return $this;
|
||||
|
||||
@@ -3,14 +3,12 @@
|
||||
namespace Filament\Tables\Columns\Summarizers\Concerns;
|
||||
|
||||
use Closure;
|
||||
use Filament\Support\Enums\ArgumentValue;
|
||||
use Filament\Tables\Columns\Summarizers\Summarizer;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Number;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use function Filament\Support\format_money;
|
||||
use function Filament\Support\format_number;
|
||||
|
||||
trait CanFormatState
|
||||
{
|
||||
protected ?Closure $formatStateUsing = null;
|
||||
@@ -26,83 +24,67 @@ trait CanFormatState
|
||||
|
||||
public function limit(int $length = 100, ?string $end = '...'): static
|
||||
{
|
||||
$this->formatStateUsing(static function ($state) use ($end, $length) {
|
||||
$isArrayState = is_array($state);
|
||||
|
||||
$state = array_map(function ($state) use ($end, $length) {
|
||||
if (blank($state)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Str::limit(strval($state), $length, $end ?? '');
|
||||
}, Arr::wrap($state));
|
||||
|
||||
if (! $isArrayState) {
|
||||
return Arr::first($state);
|
||||
$this->formatStateUsing(static function ($state) use ($end, $length): ?string {
|
||||
if (blank($state)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $state;
|
||||
return Str::limit(strval($state), $length, $end ?? '');
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function money(string | Closure | null $currency = null, int $divideBy = 0): static
|
||||
public function money(string | Closure | null $currency = null, int $divideBy = 0, string | Closure | null $locale = null): static
|
||||
{
|
||||
$this->formatStateUsing(static function ($state, Summarizer $summarizer) use ($currency, $divideBy) {
|
||||
$isArrayState = is_array($state);
|
||||
|
||||
$state = array_map(function ($state) use ($currency, $divideBy, $summarizer) {
|
||||
if (blank($state)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$currency = $summarizer->evaluate($currency) ?? Table::$defaultCurrency;
|
||||
|
||||
return format_money($state, $currency, $divideBy);
|
||||
}, Arr::wrap($state));
|
||||
|
||||
if (! $isArrayState) {
|
||||
return Arr::first($state);
|
||||
$this->formatStateUsing(static function ($state, Summarizer $summarizer) use ($currency, $divideBy, $locale): ?string {
|
||||
if (blank($state)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $state;
|
||||
if (! is_numeric($state)) {
|
||||
return $state;
|
||||
}
|
||||
|
||||
$currency = $summarizer->evaluate($currency) ?? Table::$defaultCurrency;
|
||||
|
||||
if ($divideBy) {
|
||||
$state /= $divideBy;
|
||||
}
|
||||
|
||||
return Number::currency($state, $currency, $summarizer->evaluate($locale));
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function numeric(int | Closure | null $decimalPlaces = null, string | Closure | null $decimalSeparator = '.', string | Closure | null $thousandsSeparator = ','): static
|
||||
public function numeric(int | Closure | null $decimalPlaces = null, string | Closure | null | ArgumentValue $decimalSeparator = ArgumentValue::Default, string | Closure | null | ArgumentValue $thousandsSeparator = ArgumentValue::Default, int | Closure | null $maxDecimalPlaces = null, string | Closure | null $locale = null): static
|
||||
{
|
||||
$this->formatStateUsing(static function ($state, Summarizer $summarizer) use ($decimalPlaces, $decimalSeparator, $thousandsSeparator) {
|
||||
$isArrayState = is_array($state);
|
||||
$this->formatStateUsing(static function ($state, Summarizer $summarizer) use ($decimalPlaces, $decimalSeparator, $locale, $maxDecimalPlaces, $thousandsSeparator): ?string {
|
||||
if (blank($state)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$state = array_map(function ($state) use ($decimalPlaces, $decimalSeparator, $summarizer, $thousandsSeparator) {
|
||||
if (blank($state)) {
|
||||
return null;
|
||||
}
|
||||
if (! is_numeric($state)) {
|
||||
return $state;
|
||||
}
|
||||
|
||||
if (! is_numeric($state)) {
|
||||
return $state;
|
||||
}
|
||||
|
||||
if ($decimalPlaces === null) {
|
||||
return format_number($state);
|
||||
}
|
||||
$decimalSeparator = $summarizer->evaluate($decimalSeparator);
|
||||
$thousandsSeparator = $summarizer->evaluate($thousandsSeparator);
|
||||
|
||||
if (
|
||||
($decimalSeparator !== ArgumentValue::Default) ||
|
||||
($thousandsSeparator !== ArgumentValue::Default)
|
||||
) {
|
||||
return number_format(
|
||||
$state,
|
||||
$summarizer->evaluate($decimalPlaces),
|
||||
$summarizer->evaluate($decimalSeparator),
|
||||
$summarizer->evaluate($thousandsSeparator),
|
||||
$decimalSeparator === ArgumentValue::Default ? '.' : $decimalSeparator,
|
||||
$thousandsSeparator === ArgumentValue::Default ? ',' : $thousandsSeparator,
|
||||
);
|
||||
}, Arr::wrap($state));
|
||||
|
||||
if (! $isArrayState) {
|
||||
return Arr::first($state);
|
||||
}
|
||||
|
||||
return $state;
|
||||
return Number::format($state, $decimalPlaces, $maxDecimalPlaces, locale: $summarizer->evaluate($locale));
|
||||
});
|
||||
|
||||
return $this;
|
||||
|
||||
+2
-3
@@ -6,8 +6,7 @@ use Filament\Forms\Components\Component;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Tables\Filters\QueryBuilder\Constraints\Operators\Operator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
use function Filament\Support\format_number;
|
||||
use Illuminate\Support\Number;
|
||||
|
||||
class EqualsOperator extends Operator
|
||||
{
|
||||
@@ -35,7 +34,7 @@ class EqualsOperator extends Operator
|
||||
'filament-tables::filters/query-builder.operators.number.equals.summary.direct',
|
||||
[
|
||||
'attribute' => $this->getAttributeLabel(),
|
||||
'number' => format_number($this->getSettings()['number']),
|
||||
'number' => Number::format($this->getSettings()['number']),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
+2
-3
@@ -6,8 +6,7 @@ use Filament\Forms\Components\Component;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Tables\Filters\QueryBuilder\Constraints\Operators\Operator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
use function Filament\Support\format_number;
|
||||
use Illuminate\Support\Number;
|
||||
|
||||
class IsMaxOperator extends Operator
|
||||
{
|
||||
@@ -35,7 +34,7 @@ class IsMaxOperator extends Operator
|
||||
'filament-tables::filters/query-builder.operators.number.is_max.summary.direct',
|
||||
[
|
||||
'attribute' => $this->getAttributeLabel(),
|
||||
'number' => format_number($this->getSettings()['number']),
|
||||
'number' => Number::format($this->getSettings()['number']),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
+2
-3
@@ -6,8 +6,7 @@ use Filament\Forms\Components\Component;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Tables\Filters\QueryBuilder\Constraints\Operators\Operator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
use function Filament\Support\format_number;
|
||||
use Illuminate\Support\Number;
|
||||
|
||||
class IsMinOperator extends Operator
|
||||
{
|
||||
@@ -35,7 +34,7 @@ class IsMinOperator extends Operator
|
||||
'filament-tables::filters/query-builder.operators.number.is_min.summary.direct',
|
||||
[
|
||||
'attribute' => $this->getAttributeLabel(),
|
||||
'number' => format_number($this->getSettings()['number']),
|
||||
'number' => Number::format($this->getSettings()['number']),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user