adrema/app/Lib/Filter.php

50 lines
1.0 KiB
PHP
Raw Permalink Normal View History

2023-03-14 23:27:15 +01:00
<?php
namespace App\Lib;
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
{
/**
* @param Builder<T> $query
* @return Builder<T>
2023-03-14 23:27:15 +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();
}
2024-05-14 09:38:18 +02:00
/**
* @return static(self<T>)
*/
2024-05-14 01:29:39 +02:00
public function toDefault(): self
{
return $this;
2023-03-14 23:27:15 +01:00
}
}