adrema/app/Lib/Filter.php

42 lines
918 B
PHP
Raw 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-01-28 11:42:32 +01:00
return static::withoutMagicalCreationFrom($post ?: []);
2023-03-14 23:27:15 +01:00
}
}