1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSlant\Blog\Api\Services; |
4
|
|
|
|
5
|
|
|
use Botble\Base\Models\BaseQueryBuilder; |
|
|
|
|
6
|
|
|
use Botble\Language\Facades\Language; |
|
|
|
|
7
|
|
|
use CSlant\Blog\Core\Http\Responses\Base\BaseHttpResponse; |
8
|
|
|
use CSlant\Blog\Core\Models\Post; |
9
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
10
|
|
|
use Illuminate\Database\Eloquent\Builder; |
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
12
|
|
|
use Illuminate\Support\Arr; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class PostService |
16
|
|
|
* |
17
|
|
|
* @package CSlant\Blog\Api\Services |
18
|
|
|
* |
19
|
|
|
* @method BaseHttpResponse httpResponse() |
20
|
|
|
*/ |
21
|
|
|
class PostService |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Get posts by filters. |
25
|
|
|
* |
26
|
|
|
* @param array<string, mixed> $filters |
27
|
|
|
* |
28
|
|
|
* @return LengthAwarePaginator<Post> |
29
|
|
|
*/ |
30
|
|
|
public function getCustomFilters(array $filters): LengthAwarePaginator |
31
|
|
|
{ |
32
|
|
|
$data = Post::query(); |
33
|
|
|
|
34
|
|
|
if ($filters['tags'] !== null) { |
35
|
|
|
$tags = array_filter((array) $filters['tags']); |
36
|
|
|
|
37
|
|
|
$data = $data->whereHas('tags', function (Builder $query) use ($tags): void { |
38
|
|
|
$query->whereIn('tags.id', $tags); |
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($filters['categories'] !== null) { |
43
|
|
|
$categories = array_filter((array) $filters['categories']); |
44
|
|
|
|
45
|
|
|
$data = $data->whereHas('categories', function (Builder $query) use ($categories): void { |
46
|
|
|
$query->whereIn('categories.id', $categories); |
47
|
|
|
}); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($filters['categories_exclude'] !== null) { |
51
|
|
|
$data = $data |
52
|
|
|
->whereHas('categories', function (Builder $query) use ($filters): void { |
53
|
|
|
$query->whereNotIn('categories.id', array_filter((array) $filters['categories_exclude'])); |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($filters['exclude'] !== null) { |
58
|
|
|
$data = $data->whereNotIn('id', array_filter((array) $filters['exclude'])); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($filters['include'] !== null) { |
62
|
|
|
$data = $data->whereNotIn('id', array_filter((array) $filters['include'])); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($filters['author'] !== null) { |
66
|
|
|
$data = $data->whereIn('author_id', array_filter((array) $filters['author'])); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($filters['author_exclude'] !== null) { |
70
|
|
|
$data = $data->whereNotIn('author_id', array_filter((array) $filters['author_exclude'])); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($filters['featured'] !== null) { |
74
|
|
|
$data = $data->where('is_featured', $filters['featured']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($filters['search'] !== null) { |
78
|
|
|
$keyword = isset($filters['search']) ? (string) $filters['search'] : null; |
79
|
|
|
$data = $this->search($data, $keyword); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$orderBy = Arr::get($filters, 'order_by', 'updated_at'); |
83
|
|
|
$order = Arr::get($filters, 'order', 'desc'); |
84
|
|
|
|
85
|
|
|
$data = $data |
86
|
|
|
->wherePublished() |
87
|
|
|
->orderBy($orderBy, $order); |
88
|
|
|
|
89
|
|
|
return $data->paginate((int) $filters['per_page']); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param BaseQueryBuilder|Builder<Model> $model |
94
|
|
|
* @param null|string $keyword |
95
|
|
|
* |
96
|
|
|
* @return BaseQueryBuilder|Builder<Model> |
97
|
|
|
*/ |
98
|
|
|
protected function search(Builder|BaseQueryBuilder $model, ?string $keyword): Builder|BaseQueryBuilder |
99
|
|
|
{ |
100
|
|
|
if (!$model instanceof BaseQueryBuilder || !$keyword) { |
101
|
|
|
return $model; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ( |
105
|
|
|
is_plugin_active('language') && |
106
|
|
|
is_plugin_active('language-advanced') && |
107
|
|
|
Language::getCurrentLocale() != Language::getDefaultLocale() |
108
|
|
|
) { |
109
|
|
|
return $model |
110
|
|
|
->whereHas('translations', function (BaseQueryBuilder $query) use ($keyword): void { |
111
|
|
|
$query |
112
|
|
|
->addSearch('name', $keyword, false, false) |
113
|
|
|
->addSearch('description', $keyword, false); |
114
|
|
|
}); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $model |
118
|
|
|
->where(function (BaseQueryBuilder $query) use ($keyword): void { |
119
|
|
|
$query |
120
|
|
|
->addSearch('name', $keyword, false, false) |
121
|
|
|
->addSearch('description', $keyword, false); |
122
|
|
|
}); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths