From a9fdea6a794afe5d82808d181a0fd069a7d7fb73 Mon Sep 17 00:00:00 2001 From: Patrick Weh <40495041+patrickweh@users.noreply.github.com> Date: Sat, 3 Aug 2024 18:26:34 +0200 Subject: [PATCH] add formatting for money cast add formatting for float cast add try catch for SupportsRelations.php add getSelectedModelQuery method to SupportsSelecting.php --- src/Casts/BcFloat.php | 9 +++- src/Casts/Money.php | 9 +++- src/Traits/DataTables/SupportsRelations.php | 53 ++++++++++----------- src/Traits/DataTables/SupportsSelecting.php | 10 ++-- 4 files changed, 49 insertions(+), 32 deletions(-) diff --git a/src/Casts/BcFloat.php b/src/Casts/BcFloat.php index 8cda6f4..121fb4c 100644 --- a/src/Casts/BcFloat.php +++ b/src/Casts/BcFloat.php @@ -3,6 +3,7 @@ namespace TeamNiftyGmbH\DataTable\Casts; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; +use Illuminate\Support\Number; use TeamNiftyGmbH\DataTable\Contracts\HasFrontendFormatter; class BcFloat implements CastsAttributes, HasFrontendFormatter @@ -19,7 +20,13 @@ public function get($model, string $key, $value, array $attributes): mixed return $model->getAttributeValue($key); } - return $value; + $value = Number::trim($value ?? 0); + + return fmod($value, 1) === 0.0 + // not a decimal number, pad with 2 decimal places + ? (float) number_format($value, 2, '.', '') + // a decimal number, return as is + : $value; } /** diff --git a/src/Casts/Money.php b/src/Casts/Money.php index cbe745e..62ab298 100644 --- a/src/Casts/Money.php +++ b/src/Casts/Money.php @@ -3,6 +3,7 @@ namespace TeamNiftyGmbH\DataTable\Casts; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; +use Illuminate\Support\Number; use TeamNiftyGmbH\DataTable\Contracts\HasFrontendFormatter; class Money implements CastsAttributes, HasFrontendFormatter @@ -19,7 +20,13 @@ public function get($model, string $key, $value, array $attributes): mixed return $model->getAttributeValue($key); } - return $value; + $value = Number::trim($value ?? 0); + + return fmod($value, 1) === 0.0 + // not a decimal number, pad with 2 decimal places + ? (float) number_format($value, 2, '.', '') + // a decimal number, return as is + : $value; } /** diff --git a/src/Traits/DataTables/SupportsRelations.php b/src/Traits/DataTables/SupportsRelations.php index 9bd20f4..f241581 100644 --- a/src/Traits/DataTables/SupportsRelations.php +++ b/src/Traits/DataTables/SupportsRelations.php @@ -263,41 +263,40 @@ protected function getModelRelations($modelInfo): array if ($reflection->getModifiers() !== ReflectionMethod::IS_PUBLIC) { continue; } - } catch (\ReflectionException $e) { - } - - $relationInstance = $modelQuery->{$relation->name}(); + $relationInstance = $modelQuery->{$relation->name}(); - // exclude morph relations - if ($relationInstance instanceof MorphToMany || $relationInstance instanceof MorphTo) { - continue; - } + // exclude morph relations + if ($relationInstance instanceof MorphToMany || $relationInstance instanceof MorphTo) { + continue; + } - $currentPath = $relation->name; + $currentPath = $relation->name; - data_set($modelRelations, $currentPath . '.model', $relation->related); - data_set($modelRelations, $currentPath . '.label', __(Str::headline($relation->name))); - data_set($modelRelations, $currentPath . '.name', $relation->name); - data_set($modelRelations, $currentPath . '.type', $relation->type); + data_set($modelRelations, $currentPath . '.model', $relation->related); + data_set($modelRelations, $currentPath . '.label', __(Str::headline($relation->name))); + data_set($modelRelations, $currentPath . '.name', $relation->name); + data_set($modelRelations, $currentPath . '.type', $relation->type); - if (method_exists($relationInstance, 'getOwnerKeyName')) { - data_set($modelRelations, $currentPath . '.keys.owner', $relationInstance->getOwnerKeyName()); - } + if (method_exists($relationInstance, 'getOwnerKeyName')) { + data_set($modelRelations, $currentPath . '.keys.owner', $relationInstance->getOwnerKeyName()); + } - if (method_exists($relationInstance, 'getForeignKeyName')) { - data_set($modelRelations, $currentPath . '.keys.foreign', $relationInstance->getForeignKeyName()); - } + if (method_exists($relationInstance, 'getForeignKeyName')) { + data_set($modelRelations, $currentPath . '.keys.foreign', $relationInstance->getForeignKeyName()); + } - if (method_exists($relationInstance, 'getRelatedPivotKeyName')) { - data_set($modelRelations, $currentPath . '.keys.owner', $relationInstance->getRelatedPivotKeyName()); - } + if (method_exists($relationInstance, 'getRelatedPivotKeyName')) { + data_set($modelRelations, $currentPath . '.keys.owner', $relationInstance->getRelatedPivotKeyName()); + } - if (method_exists($relationInstance, 'getForeignPivotKeyName')) { - data_set($modelRelations, $currentPath . '.keys.foreign', $relationInstance->getForeignPivotKeyName()); - } + if (method_exists($relationInstance, 'getForeignPivotKeyName')) { + data_set($modelRelations, $currentPath . '.keys.foreign', $relationInstance->getForeignPivotKeyName()); + } - if (method_exists($relationInstance, 'getMorphType')) { - data_set($modelRelations, $currentPath . '.keys.foreign', $relationInstance->getMorphType()); + if (method_exists($relationInstance, 'getMorphType')) { + data_set($modelRelations, $currentPath . '.keys.foreign', $relationInstance->getMorphType()); + } + } catch (\ReflectionException|\BadMethodCallException) { } } diff --git a/src/Traits/DataTables/SupportsSelecting.php b/src/Traits/DataTables/SupportsSelecting.php index e2b0a16..70c98f3 100644 --- a/src/Traits/DataTables/SupportsSelecting.php +++ b/src/Traits/DataTables/SupportsSelecting.php @@ -2,6 +2,7 @@ namespace TeamNiftyGmbH\DataTable\Traits\DataTables; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\View\ComponentAttributeBag; use Livewire\Attributes\Locked; @@ -38,9 +39,12 @@ protected function getSelectedValues(): array protected function getSelectedModels(): Collection { - return $this->getModel()::query() - ->whereIntegerInRaw($this->modelKeyName, $this->getSelectedValues()) - ->get(); + return $this->getSelectedModelsQuery()->get(); + } + + protected function getSelectedModelsQuery(): Builder + { + return $this->getModel()::query()->whereIntegerInRaw($this->modelKeyName, $this->getSelectedValues()); } #[Renderless]