-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInvokeable.php
More file actions
128 lines (104 loc) · 2.99 KB
/
Invokeable.php
File metadata and controls
128 lines (104 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace Kettasoft\Filterable\Engines;
use Illuminate\Support\Str;
use Kettasoft\Filterable\Support\Payload;
use Illuminate\Support\Traits\ForwardsCalls;
use Illuminate\Database\Eloquent\Builder;
use Kettasoft\Filterable\Engines\Foundation\Engine;
use Kettasoft\Filterable\Support\ConditionNormalizer;
class Invokeable extends Engine
{
use ForwardsCalls;
/**
* Engine name.
* @var string
*/
protected $name = 'invokable';
/**
* The Eloquent builder instance.
* @var Builder
*/
protected Builder $builder;
/**
* Apply filters to the query.
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return Builder
*/
public function execute(Builder $builder): Builder
{
$this->builder = $builder;
foreach ($this->context->getFilterAttributes() as $filter) {
$value = $this->context->getRequest()->get($filter);
if (($this->context->hasIgnoredEmptyValues() || config('filterable.engines.invokable.ignore_empty_values')) && !$value) {
continue;
}
$method = $this->getMethodName($filter);
$this->initializeFilters($filter, $method, $value);
}
return $this->builder;
}
/**
* Initialize the filter methods and resolve value.
* @param string $method
* @param mixed $value
* @return void
*/
protected function initializeFilters(string $key, string $method, mixed $value): void
{
$clause = ConditionNormalizer::normalize($value, '=');
$operator = $clause['operator'];
$val = $clause['value'];
if (method_exists($this->context, $method)) {
$payload = new Payload($key, $operator, $this->sanitizeValue($key, $val), $val);
$this->forwardCallTo($this->context, $method, [$payload]);
}
}
/**
* Get method name.
* @param string $filter
* @return string
*/
protected function getMethodName(string $filter): string
{
if (array_key_exists($filter, $this->context->getMentors())) {
return $this->context->getMentors()[$filter];
}
return $this->context->getRequest()->has($filter) ? Str::camel($filter) : 'default' . Str::studly($filter);
}
/**
* Get allowed fields to filtering.
* @return array
*/
protected function getAllowedFieldsFromConfig(): array
{
return config('filterable.engines.invokable.allowed_fields', []);
}
public function getOperatorsFromConfig(): array
{
return config('filterable.engines.invokable.allowed_operators', []);
}
public function isStrictFromConfig(): bool
{
return config('filterable.engines.invokable.strict', true);
}
public function isIgnoredEmptyValuesFromConfig(): bool
{
return config('filterable.engines.invokable.ignore_empty_values', false);
}
/**
* Get engine default operator.
* @return string
*/
public function defaultOperator(): string
{
return config('filterable.engines.invokable.default_operator', 'eq');
}
/**
* Get engine name.
* @return string
*/
public function getEngineName(): string
{
return $this->name;
}
}