Skip to content

Commit

Permalink
add formatting for money cast
Browse files Browse the repository at this point in the history
add formatting for float cast

add try catch for SupportsRelations.php

add getSelectedModelQuery method to SupportsSelecting.php
  • Loading branch information
patrickweh committed Aug 3, 2024
1 parent 1e0af50 commit a9fdea6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 32 deletions.
9 changes: 8 additions & 1 deletion src/Casts/BcFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Check failure on line 23 in src/Casts/BcFloat.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method Illuminate\Support\Number::trim().

Check failure on line 23 in src/Casts/BcFloat.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method Illuminate\Support\Number::trim().

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;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Casts/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Check failure on line 23 in src/Casts/Money.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method Illuminate\Support\Number::trim().

Check failure on line 23 in src/Casts/Money.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method Illuminate\Support\Number::trim().

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;
}

/**
Expand Down
53 changes: 26 additions & 27 deletions src/Traits/DataTables/SupportsRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/Traits/DataTables/SupportsSelecting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit a9fdea6

Please sign in to comment.