taktik - laravel
This commit is contained in:
52
app/Services/QueryRequestModifiers/Post/PostFilter.php
Normal file
52
app/Services/QueryRequestModifiers/Post/PostFilter.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Post;
|
||||
|
||||
use App\Http\Requests\Post\ListPostRequest;
|
||||
use App\Models\Post;
|
||||
use App\Services\QueryRequestModifiers\Filterable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class PostFilter
|
||||
{
|
||||
/**
|
||||
* @use Filterable<Post, PostFilterDTO>
|
||||
*/
|
||||
use Filterable;
|
||||
|
||||
/**
|
||||
* @param Builder<Post> $query
|
||||
* @return Builder<Post>
|
||||
*/
|
||||
public function apply(Builder $query, ?PostFilterDTO $filters): Builder
|
||||
{
|
||||
return $this->applyFilterable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListPostRequest $request): ?PostFilterDTO
|
||||
{
|
||||
return $this->makeFilterableFromRequest($request, PostFilterDTO::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function validateRules(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'sometimes|string',
|
||||
'content' => 'sometimes|string',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected static function keys(): array
|
||||
{
|
||||
return ['title', 'content'];
|
||||
}
|
||||
}
|
||||
22
app/Services/QueryRequestModifiers/Post/PostFilterDTO.php
Normal file
22
app/Services/QueryRequestModifiers/Post/PostFilterDTO.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Post;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
|
||||
readonly class PostFilterDTO implements CacheKeyInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $filters
|
||||
*/
|
||||
public function __construct(public array $filters)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return http_build_query($this->filters, '', '|');
|
||||
}
|
||||
}
|
||||
33
app/Services/QueryRequestModifiers/Post/PostOrder.php
Normal file
33
app/Services/QueryRequestModifiers/Post/PostOrder.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Post;
|
||||
|
||||
use App\Http\Requests\Post\ListPostRequest;
|
||||
use App\Models\Post;
|
||||
use App\Services\QueryRequestModifiers\Orderable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class PostOrder
|
||||
{
|
||||
/**
|
||||
* @use Orderable<Post, PostOrderDTO>
|
||||
*/
|
||||
use Orderable;
|
||||
|
||||
|
||||
/**
|
||||
* @param Builder<Post> $query
|
||||
* @return Builder<Post>
|
||||
*/
|
||||
public function apply(Builder $query, ?PostOrderDTO $filters): Builder
|
||||
{
|
||||
return $this->applyOrderable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListPostRequest $request): ?PostOrderDTO
|
||||
{
|
||||
return $this->makeOrderableFromRequest($request, PostOrderDTO::class);
|
||||
}
|
||||
}
|
||||
42
app/Services/QueryRequestModifiers/Post/PostOrderDTO.php
Normal file
42
app/Services/QueryRequestModifiers/Post/PostOrderDTO.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Post;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
use App\Services\QueryRequestModifiers\SortDirection;
|
||||
use App\Services\QueryRequestModifiers\OrderableDTO;
|
||||
|
||||
/**
|
||||
* @implements OrderableDTO<PostOrderDTO>
|
||||
*/
|
||||
readonly class PostOrderDTO implements CacheKeyInterface, OrderableDTO
|
||||
{
|
||||
public function __construct(public string $column, public SortDirection $direction)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return $this->column . '|' . $this->direction->value;
|
||||
}
|
||||
|
||||
public function getColumn(): string
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
|
||||
public function getDirection(): SortDirection
|
||||
{
|
||||
return $this->direction;
|
||||
}
|
||||
|
||||
public static function createFromValues(string $column, SortDirection $sortDirection): OrderableDTO
|
||||
{
|
||||
return new self(
|
||||
$column,
|
||||
$sortDirection
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user