Skip to content

Commit

Permalink
refactor: update filter base type method to work with PHP 8.3
Browse files Browse the repository at this point in the history
  • Loading branch information
tomshaw committed Aug 3, 2024
1 parent c4d33cf commit e72c170
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 67 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ use App\Models\Order;

class OrdersTable extends Component
{
public array $computedColumns = ['posts_count'];

public function builder(): Builder
{
return Order::with(['user' => function ($query) {
$query->select('id', 'name', 'email');
}])->select('orders.id', 'orders.user_id', 'orders.status', 'orders.total', 'orders.invoiced', 'orders.created_at', 'orders.updated_at');
return User::with(['roles', 'profile'])->withCount('posts');
}
}
```
Expand Down
126 changes: 63 additions & 63 deletions src/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,69 +234,6 @@ private function hasStartOrEndKey(array $value): bool
return isset($value['start']) || isset($value['end']);
}

public function filter(array $filters): void
{
foreach ($filters as $type => $values) {
match ($type) {
'text' => $this->handleText($values),
'number' => $this->handleNumber($values),
'select' => $this->handleSelect($values),
'multiselect' => $this->handleMultiSelect($values),
'boolean' => $this->handleBoolean($values),
'timepicker' => $this->handleTimePicker($values),
'datepicker' => $this->handleDatePicker($values),
'datetimepicker' => $this->handleDateTimePicker($values),
'search_term' => $this->handleSearchTerm($values),
'search_letter' => $this->handleSelectLetter($values),
default => throw InvalidFilterHandler::make($type),
};
}
}

private function handleSearchTerm(array $values): self
{
foreach ($values as $columnName => $searchTerm) {
if (strpos($columnName, '.')) {
[$relation, $subColumnName] = $this->parseColumnString($columnName);
if ($relation !== null) {
$this->query->whereHas($relation, function ($query) use ($subColumnName, $searchTerm) {
$query->where($subColumnName, 'like', '%'.$searchTerm.'%');
});
}
} else {
$qualifiedColumnName = $this->resolveTableNames($columnName);
if ($qualifiedColumnName) {
$this->query->where($qualifiedColumnName, 'like', '%'.$searchTerm.'%');
} else {
$this->query->where($columnName, 'like', '%'.$searchTerm.'%');
}
}
}

return $this;
}

private function handleSelectLetter(array $values): void
{
foreach ($values as $columnName => $value) {
if (strpos($columnName, '.')) {
[$relation, $subColumnName] = $this->parseColumnString($columnName);
if ($relation !== null) {
$this->query->whereHas($relation, function ($query) use ($subColumnName, $value) {
$query->where($subColumnName, 'like', $value.'%');
});
}
} else {
$qualifiedColumnName = $this->resolveTableNames($columnName);
if ($qualifiedColumnName) {
$this->query->where($qualifiedColumnName, 'like', $value.'%');
} else {
$this->query->where($columnName, 'like', $value.'%');
}
}
}
}

public function orderBy(string $columnName, string $sortDirection): self
{
if (strpos($columnName, '.')) {
Expand Down Expand Up @@ -362,6 +299,69 @@ private function orderByManyToManyRelation($relationQuery, string $tableName, st
->orderBy("$relatedTable.$columnName", $sortDirection);
}

public function filter(array $filters): void
{
foreach ($filters as $type => $values) {
match ($type) {
'text' => $this->handleText($values),
'number' => $this->handleNumber($values),
'select' => $this->handleSelect($values),
'multiselect' => $this->handleMultiSelect($values),
'boolean' => $this->handleBoolean($values),
'timepicker' => $this->handleTimePicker($values),
'datepicker' => $this->handleDatePicker($values),
'datetimepicker' => $this->handleDateTimePicker($values),
'search_term' => $this->handleSearchTerm($values),
'search_letter' => $this->handleSelectLetter($values),
default => throw InvalidFilterHandler::make($type),
};
}
}

private function handleSearchTerm(array $values): self
{
foreach ($values as $columnName => $searchTerm) {
if (strpos($columnName, '.')) {
[$relation, $subColumnName] = $this->parseColumnString($columnName);
if ($relation !== null) {
$this->query->whereHas($relation, function ($query) use ($subColumnName, $searchTerm) {
$query->where($subColumnName, 'like', '%'.$searchTerm.'%');
});
}
} else {
$qualifiedColumnName = $this->resolveTableNames($columnName);
if ($qualifiedColumnName) {
$this->query->where($qualifiedColumnName, 'like', '%'.$searchTerm.'%');
} else {
$this->query->where($columnName, 'like', '%'.$searchTerm.'%');
}
}
}

return $this;
}

private function handleSelectLetter(array $values): void
{
foreach ($values as $columnName => $value) {
if (strpos($columnName, '.')) {
[$relation, $subColumnName] = $this->parseColumnString($columnName);
if ($relation !== null) {
$this->query->whereHas($relation, function ($query) use ($subColumnName, $value) {
$query->where($subColumnName, 'like', $value.'%');
});
}
} else {
$qualifiedColumnName = $this->resolveTableNames($columnName);
if ($qualifiedColumnName) {
$this->query->where($qualifiedColumnName, 'like', $value.'%');
} else {
$this->query->where($columnName, 'like', $value.'%');
}
}
}
}

private function handleText(array $values): void
{
foreach ($values as $columnName => $value) {
Expand Down
5 changes: 4 additions & 1 deletion src/Filters/FilterBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TomShaw\ElectricGrid\Filters;

use ReflectionClass;
use TomShaw\ElectricGrid\Filters\Traits\WithAttributes;

class FilterBase
Expand All @@ -15,6 +16,8 @@ public function __construct(

public function type(string $name): bool
{
return basename(get_called_class()) === $name;
$reflection = new ReflectionClass($this);

return $reflection->getShortName() === $name;
}
}

0 comments on commit e72c170

Please sign in to comment.