taktik - laravel
This commit is contained in:
51
app/Services/QueryRequestModifiers/Comment/CommentFilter.php
Normal file
51
app/Services/QueryRequestModifiers/Comment/CommentFilter.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Comment;
|
||||
|
||||
use App\Http\Requests\Comment\ListCommentRequest;
|
||||
use App\Models\Comment;
|
||||
use App\Services\QueryRequestModifiers\Filterable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CommentFilter
|
||||
{
|
||||
/**
|
||||
* @use Filterable<Comment, CommentFilterDTO>
|
||||
*/
|
||||
use Filterable;
|
||||
|
||||
/**
|
||||
* @param Builder<Comment> $query
|
||||
* @return Builder<Comment>
|
||||
*/
|
||||
public function apply(Builder $query, ?CommentFilterDTO $filters): Builder
|
||||
{
|
||||
return $this->applyFilterable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListCommentRequest $request): ?CommentFilterDTO
|
||||
{
|
||||
return $this->makeFilterableFromRequest($request, CommentFilterDTO::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function validateRules(): array
|
||||
{
|
||||
return [
|
||||
'content' => 'sometimes|string',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected static function keys(): array
|
||||
{
|
||||
return ['content'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Comment;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
|
||||
readonly class CommentFilterDTO 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/Comment/CommentOrder.php
Normal file
33
app/Services/QueryRequestModifiers/Comment/CommentOrder.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Comment;
|
||||
|
||||
use App\Http\Requests\Comment\ListCommentRequest;
|
||||
use App\Models\Comment;
|
||||
use App\Services\QueryRequestModifiers\Orderable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CommentOrder
|
||||
{
|
||||
/**
|
||||
* @use Orderable<Comment, CommentOrderDTO>
|
||||
*/
|
||||
use Orderable;
|
||||
|
||||
|
||||
/**
|
||||
* @param Builder<Comment> $query
|
||||
* @return Builder<Comment>
|
||||
*/
|
||||
public function apply(Builder $query, ?CommentOrderDTO $filters): Builder
|
||||
{
|
||||
return $this->applyOrderable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListCommentRequest $request): ?CommentOrderDTO
|
||||
{
|
||||
return $this->makeOrderableFromRequest($request, CommentOrderDTO::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Comment;
|
||||
|
||||
use App\Services\QueryRequestModifiers\OrderableDTO;
|
||||
use App\Services\QueryRequestModifiers\SortDirection;
|
||||
|
||||
/**
|
||||
* @implements OrderableDTO<CommentOrderDTO>
|
||||
*/
|
||||
readonly class CommentOrderDTO implements 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