Skip to content

Commit

Permalink
Merge pull request #66 from pelmered/remove-getLabel
Browse files Browse the repository at this point in the history
Move traits into a folder
  • Loading branch information
pelmered authored Nov 6, 2024
2 parents ceb7a04 + ac196db commit 8fafe70
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/Concerns/FormatsAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Pelmered\FilamentMoneyField\Concerns;

use Illuminate\Support\Str;

trait FormatsAttributes
{
public function formatAttribute(string $attribute): string
{
return Str::of($attribute)->afterLast('.')->snake(' ')->title();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Pelmered\FilamentMoneyField;
namespace Pelmered\FilamentMoneyField\Concerns;

use Closure;
use Filament\Infolists\Infolist;
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Components/MoneyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Filament\Forms\Components\TextInput;
use Filament\Support\RawJs;
use Pelmered\FilamentMoneyField\Concerns\HasMoneyAttributes;
use Pelmered\FilamentMoneyField\Forms\Rules\MaxValueRule;
use Pelmered\FilamentMoneyField\Forms\Rules\MinValueRule;
use Pelmered\FilamentMoneyField\HasMoneyAttributes;
use Pelmered\FilamentMoneyField\MoneyFormatter;

class MoneyInput extends TextInput
Expand Down
8 changes: 5 additions & 3 deletions src/Forms/Rules/MaxValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Str;
use Money\Exception\ParserException;
use Pelmered\FilamentMoneyField\Concerns\FormatsAttributes;
use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput;
use Pelmered\FilamentMoneyField\MoneyFormatter;

readonly class MaxValueRule implements ValidationRule
{
use FormatsAttributes;

public function __construct(private int $max, private MoneyInput $component) {}

public function validate(string $attribute, mixed $value, Closure $fail): void
Expand All @@ -30,7 +32,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
strtr(
'The {attribute} must be less than or equal to {value}.',
[
'{attribute}' => Str::of($attribute)->afterLast('.')->snake(' ')->title(),
'{attribute}' => $this->formatAttribute($attribute),
'{value}' => MoneyFormatter::formatAsDecimal($this->max, $currencyCode, $locale),
]
)
Expand All @@ -41,7 +43,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
strtr(
'The {attribute} must be a valid numeric value.',
[
'{attribute}' => Str::of($attribute)->afterLast('.')->snake(' ')->title(),
'{attribute}' => $this->formatAttribute($attribute),
]
)
);
Expand Down
8 changes: 5 additions & 3 deletions src/Forms/Rules/MinValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Str;
use Money\Exception\ParserException;
use Pelmered\FilamentMoneyField\Concerns\FormatsAttributes;
use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput;
use Pelmered\FilamentMoneyField\MoneyFormatter;

readonly class MinValueRule implements ValidationRule
{
use FormatsAttributes;

public function __construct(private int $min, private MoneyInput $component) {}

public function validate(string $attribute, mixed $value, Closure $fail): void
Expand All @@ -30,7 +32,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
strtr(
'The {attribute} must be at least {value}.',
[
'{attribute}' => Str::of($attribute)->afterLast('.')->snake(' ')->title(),
'{attribute}' => $this->formatAttribute($attribute),
'{value}' => MoneyFormatter::formatAsDecimal($this->min, $currencyCode, $locale),
]
)
Expand All @@ -41,7 +43,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
strtr(
'The {attribute} must be a valid numeric value.',
[
'{attribute}' => Str::of($attribute)->afterLast('.')->snake(' ')->title(),
'{attribute}' => $this->formatAttribute($attribute),
]
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/Infolists/Components/MoneyEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Pelmered\FilamentMoneyField\Infolists\Components;

use Filament\Infolists\Components\TextEntry;
use Pelmered\FilamentMoneyField\HasMoneyAttributes;
use Pelmered\FilamentMoneyField\Concerns\HasMoneyAttributes;
use Pelmered\FilamentMoneyField\MoneyFormatter;

class MoneyEntry extends TextEntry
Expand Down
2 changes: 1 addition & 1 deletion src/Tables/Columns/MoneyColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Pelmered\FilamentMoneyField\Tables\Columns;

use Filament\Tables\Columns\TextColumn;
use Pelmered\FilamentMoneyField\HasMoneyAttributes;
use Pelmered\FilamentMoneyField\Concerns\HasMoneyAttributes;
use Pelmered\FilamentMoneyField\MoneyFormatter;

class MoneyColumn extends TextColumn
Expand Down

0 comments on commit 8fafe70

Please sign in to comment.