2023-03-14 23:27:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Lib;
|
|
|
|
|
2024-03-08 02:19:07 +01:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2023-03-14 23:27:15 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Spatie\LaravelData\Data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T of Model
|
2024-01-28 11:42:32 +01:00
|
|
|
* @property Builder $query
|
2023-03-14 23:27:15 +01:00
|
|
|
*/
|
|
|
|
abstract class Filter extends Data
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2024-03-08 02:19:07 +01:00
|
|
|
* @param Builder<T> $query
|
|
|
|
* @return Builder<T>
|
2023-03-14 23:27:15 +01:00
|
|
|
*/
|
2024-03-08 02:19:07 +01:00
|
|
|
abstract public function apply(Builder $query): Builder;
|
2023-03-14 23:27:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, mixed>|string|null $request
|
|
|
|
*/
|
|
|
|
public static function fromRequest(array|string|null $request = null): static
|
|
|
|
{
|
|
|
|
$payload = is_string($request)
|
2023-03-22 23:37:36 +01:00
|
|
|
? json_decode(rawurldecode(base64_decode($request)), true)
|
2023-03-14 23:27:15 +01:00
|
|
|
: $request;
|
|
|
|
|
|
|
|
return static::fromPost($payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, mixed> $post
|
|
|
|
*/
|
|
|
|
public static function fromPost(?array $post = null): static
|
|
|
|
{
|
2024-05-14 01:29:39 +02:00
|
|
|
return static::withoutMagicalCreationFrom($post ?: [])->toDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toDefault(): self
|
|
|
|
{
|
|
|
|
return $this;
|
2023-03-14 23:27:15 +01:00
|
|
|
}
|
|
|
|
}
|