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\Category; |
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 CategoryService |
16
|
|
|
* |
17
|
|
|
* @package CSlant\Blog\Api\Services |
18
|
|
|
* |
19
|
|
|
* @method BaseHttpResponse httpResponse() |
20
|
|
|
*/ |
21
|
|
|
class CategoryService |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Get categories by filters. |
25
|
|
|
* |
26
|
|
|
* @param array<string, mixed> $filters |
27
|
|
|
* |
28
|
|
|
* @return LengthAwarePaginator<Category> |
29
|
|
|
*/ |
30
|
|
|
public function getCustomFilters(array $filters): LengthAwarePaginator |
31
|
|
|
{ |
32
|
|
|
$data = Category::query() |
33
|
|
|
->withCount('posts'); |
34
|
|
|
|
35
|
|
|
if ($filters['search'] !== null) { |
36
|
|
|
$keyword = isset($filters['search']) ? (string) $filters['search'] : null; |
37
|
|
|
$data = $this->search($data, $keyword); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$orderBy = Arr::get($filters, 'order_by', 'posts_count'); |
41
|
|
|
$order = Arr::get($filters, 'order', 'desc'); |
42
|
|
|
|
43
|
|
|
$data = $data |
44
|
|
|
->wherePublished() |
45
|
|
|
->orderBy($orderBy, $order); |
46
|
|
|
|
47
|
|
|
return $data->paginate((int) $filters['per_page']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param BaseQueryBuilder|Builder<Model> $model |
52
|
|
|
* @param null|string $keyword |
53
|
|
|
* |
54
|
|
|
* @return BaseQueryBuilder|Builder<Model> |
55
|
|
|
*/ |
56
|
|
|
protected function search(Builder|BaseQueryBuilder $model, ?string $keyword): Builder|BaseQueryBuilder |
57
|
|
|
{ |
58
|
|
|
if (!$model instanceof BaseQueryBuilder || !$keyword) { |
59
|
|
|
return $model; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (is_plugin_active('language') |
63
|
|
|
&& is_plugin_active('language-advanced') |
64
|
|
|
&& Language::getCurrentLocale() != Language::getDefaultLocale() |
65
|
|
|
) { |
66
|
|
|
return $model |
67
|
|
|
->whereHas('translations', function (BaseQueryBuilder $query) use ($keyword): void { |
68
|
|
|
$query->addSearch('name', $keyword, false, false); |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $model |
73
|
|
|
->where(function (BaseQueryBuilder $query) use ($keyword): void { |
74
|
|
|
$query->addSearch('name', $keyword, false, false); |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
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