taktik - laravel
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Category;
|
||||
|
||||
use App\Http\Requests\Category\ListCategoryRequest;
|
||||
use App\Models\Category;
|
||||
use App\Services\QueryRequestModifiers\Filterable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CategoryFilter
|
||||
{
|
||||
/**
|
||||
* @use Filterable<Category, CategoryFilterDTO>
|
||||
*/
|
||||
use Filterable;
|
||||
|
||||
/**
|
||||
* @param Builder<Category> $query
|
||||
* @return Builder<Category>
|
||||
*/
|
||||
public function apply(Builder $query, ?CategoryFilterDTO $filters): Builder
|
||||
{
|
||||
return $this->applyFilterable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListCategoryRequest $request): ?CategoryFilterDTO
|
||||
{
|
||||
return $this->makeFilterableFromRequest($request, CategoryFilterDTO::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function validateRules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'sometimes|string|max:255',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected static function keys(): array
|
||||
{
|
||||
return ['name'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Category;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
|
||||
class CategoryFilterDTO implements CacheKeyInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $filters
|
||||
*/
|
||||
public function __construct(public array $filters)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return http_build_query($this->filters, '', '|');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Category;
|
||||
|
||||
use App\Http\Requests\Category\ListCategoryRequest;
|
||||
use App\Models\Category;
|
||||
use App\Services\QueryRequestModifiers\Orderable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CategoryOrder
|
||||
{
|
||||
/**
|
||||
* @use Orderable<Category, CategoryOrderDTO>
|
||||
*/
|
||||
use Orderable;
|
||||
|
||||
|
||||
/**
|
||||
* @param Builder<Category> $query
|
||||
* @return Builder<Category>
|
||||
*/
|
||||
public function apply(Builder $query, ?CategoryOrderDTO $filters): Builder
|
||||
{
|
||||
return $this->applyOrderable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListCategoryRequest $request): ?CategoryOrderDTO
|
||||
{
|
||||
return $this->makeOrderableFromRequest($request, CategoryOrderDTO::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Category;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
use App\Services\QueryRequestModifiers\OrderableDTO;
|
||||
use App\Services\QueryRequestModifiers\SortDirection;
|
||||
|
||||
/**
|
||||
* @implements OrderableDTO<CategoryOrderDTO>
|
||||
*/
|
||||
class CategoryOrderDTO 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
|
||||
);
|
||||
}
|
||||
}
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
52
app/Services/QueryRequestModifiers/Filterable.php
Normal file
52
app/Services/QueryRequestModifiers/Filterable.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
/**
|
||||
* @template C of Model
|
||||
* @template D
|
||||
*/
|
||||
trait Filterable
|
||||
{
|
||||
abstract public static function keys();
|
||||
|
||||
/**
|
||||
* @param Builder<C> $query
|
||||
* @return Builder<C>
|
||||
*/
|
||||
protected function applyFilterable(Builder $query, ?object $filters): Builder
|
||||
{
|
||||
if ($filters === null) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
foreach (self::keys() as $filterName) {
|
||||
if (isset($filters->filters[$filterName])) {
|
||||
$query->where($filterName, '=', $filters->filters[$filterName]);
|
||||
}
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<D> $className
|
||||
* @return D|null
|
||||
*/
|
||||
protected function makeFilterableFromRequest(FormRequest $request, string $className): ?object
|
||||
{
|
||||
$keys = $request->all(self::keys());
|
||||
|
||||
// no filtering
|
||||
if (count($keys) === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new $className($keys);
|
||||
}
|
||||
}
|
||||
75
app/Services/QueryRequestModifiers/Orderable.php
Normal file
75
app/Services/QueryRequestModifiers/Orderable.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
/**
|
||||
* @template C of Model
|
||||
* @template D of OrderableDTO
|
||||
*/
|
||||
trait Orderable
|
||||
{
|
||||
/**
|
||||
* @param Builder<C> $query
|
||||
* @param ?OrderableDTO<D> $orderDef
|
||||
* @return Builder<C>
|
||||
*/
|
||||
protected function applyOrderable(Builder $query, ?OrderableDTO $orderDef): Builder
|
||||
{
|
||||
if ($orderDef !== null) {
|
||||
$query->orderBy($orderDef->getColumn(), $orderDef->getDirection()->value);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<D> $className
|
||||
* @return D|null
|
||||
*/
|
||||
protected function makeOrderableFromRequest(FormRequest $request, string $className): ?object
|
||||
{
|
||||
$keys = $request->all(self::keys());
|
||||
|
||||
if (!isset($keys['order']) || !isset($keys['direction'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$validator = Validator::make($keys, $this->validateRules());
|
||||
|
||||
if ($validator->fails()) {
|
||||
// this should never happen... Invalid request
|
||||
throw new \InvalidArgumentException($validator->errors()->first());
|
||||
}
|
||||
|
||||
$column = $keys['order'];
|
||||
$direction = $keys['direction'];
|
||||
|
||||
return call_user_func([$className, 'createFromValues'], $column, SortDirection::from($direction));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function validateRules(): array
|
||||
{
|
||||
return [
|
||||
'order' => 'sometimes|string|in:title',
|
||||
'direction' => 'sometimes|string|in:asc,desc',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected static function keys(): array
|
||||
{
|
||||
return ['order', 'direction'];
|
||||
}
|
||||
}
|
||||
19
app/Services/QueryRequestModifiers/OrderableDTO.php
Normal file
19
app/Services/QueryRequestModifiers/OrderableDTO.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers;
|
||||
|
||||
/**
|
||||
* @template T of OrderableDTO
|
||||
*/
|
||||
interface OrderableDTO
|
||||
{
|
||||
public function getColumn(): string;
|
||||
public function getDirection(): SortDirection;
|
||||
|
||||
/**
|
||||
* @return T
|
||||
*/
|
||||
public static function createFromValues(string $column, SortDirection $sortDirection): self;
|
||||
}
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
11
app/Services/QueryRequestModifiers/SortDirection.php
Normal file
11
app/Services/QueryRequestModifiers/SortDirection.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers;
|
||||
|
||||
enum SortDirection: string
|
||||
{
|
||||
case ASC = "asc";
|
||||
case DESC = "desc";
|
||||
}
|
||||
Reference in New Issue
Block a user