Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocfe committed Mar 27, 2024
1 parent 088984a commit cb00859
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Filament\Forms\Components\TextInput;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;

class Money extends TextInput
{
Expand All @@ -27,13 +28,10 @@ protected function setUp(): void
}',

'x-on:keyup' => 'function() {
var money = $el.value.replace(/\D/g, "");
money = (money / 100).toFixed(2) + "";
money = money.replace(".", ",");
money = money.replace(/(\d)(\d{3})(\d{3}),/g, "$1.$2.$3,");
money = money.replace(/(\d)(\d{3}),/g, "$1.$2,");
$el.value = money;
var money = $el.value;
money = money.replace(/\D/g, \'\');
money = (parseFloat(money) / 100).toLocaleString(\'pt-BR\', { minimumFractionDigits: 2 });
$el.value = money === \'NaN\' ? \'0,00\' : money;
}',
])
->dehydrateMask()
Expand All @@ -43,20 +41,10 @@ protected function setUp(): void

public function dehydrateMask(bool|Closure $condition = true): static
{

if ($condition) {
$this->dehydrateStateUsing(
fn ($state): ?float => $state ?
floatval(
Str::of($state)
->replace('.', '')
->replace(',', '.')
->toString()
) * 10 :
null
);
$this->dehydrateStateUsing(fn (?string $state): ?float => $this->convertToFloat($state));

Check failure on line 45 in src/Money.php

View workflow job for this annotation

GitHub Actions / phpstan

Anonymous function never returns null so it can be removed from the return type.
} else {
$this->dehydrateStateUsing(null);
$this->dehydrateStateUsing(fn (?string $state): ?string => $this->convertToNumberFormat($state));

Check failure on line 47 in src/Money.php

View workflow job for this annotation

GitHub Actions / phpstan

Anonymous function never returns null so it can be removed from the return type.
}

return $this;
Expand All @@ -68,4 +56,40 @@ public function initialValue(null|string|int|float|Closure $value = '0,00'): sta

return $this;
}

private function sanitizeState(?string $state): ?Stringable

Check failure on line 60 in src/Money.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Leandrocfe\FilamentPtbrFormFields\Money::sanitizeState() never returns null so it can be removed from the return type.
{
$state = Str::of($state)
->replace('.', '')
->replace(',', '');

return $state ?? null;

Check failure on line 66 in src/Money.php

View workflow job for this annotation

GitHub Actions / phpstan

Variable $state on left side of ?? always exists and is not nullable.
}

private function convertToFloat(Stringable|string|null $state): float
{
$state = $this->sanitizeState($state);

if (! $state) {
return 0;
}

if ($state->length() > 2) {
$state = $state
->substr(0, $state->length() - 2)
->append('.')
->append($state->substr($state->length() - 2, 2));
} else {
$state = $state->prepend('0.');
}

return floatval($state->toString()) ?? 0;

Check failure on line 86 in src/Money.php

View workflow job for this annotation

GitHub Actions / phpstan

Expression on left side of ?? is not nullable.
}

private function convertToNumberFormat(string $state): string
{
$state = $this->convertToFloat($state);

return number_format($state, 2, ',', '.') ?? 0;

Check failure on line 93 in src/Money.php

View workflow job for this annotation

GitHub Actions / phpstan

Expression on left side of ?? is not nullable.
}
}

0 comments on commit cb00859

Please sign in to comment.