adrema/app/Lib/Filter.php

42 lines
887 B
PHP
Raw Normal View History

2023-03-14 23:27:15 +01:00
<?php
namespace App\Lib;
use Illuminate\Database\Eloquent\Model;
2024-01-28 11:42:32 +01:00
use Laravel\Scout\Builder;
2023-03-14 23:27:15 +01:00
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-01-28 18:28:09 +01:00
* @return Builder
2023-03-14 23:27:15 +01:00
*/
2024-01-28 11:42:32 +01:00
abstract public function getQuery(): Builder;
protected Builder $query;
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
}
}