-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEngine.php
More file actions
113 lines (94 loc) · 2.92 KB
/
Engine.php
File metadata and controls
113 lines (94 loc) · 2.92 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
<?php
namespace Kettasoft\Filterable\Engines\Foundation;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Kettasoft\Filterable\Contracts\FilterableContext;
use Kettasoft\Filterable\Engines\Contracts\Executable;
use Kettasoft\Filterable\Engines\Contracts\HasAllowedFieldChecker;
use Kettasoft\Filterable\Engines\Contracts\HasFieldMap;
use Kettasoft\Filterable\Engines\Contracts\HasInteractsWithOperators;
use Kettasoft\Filterable\Engines\Contracts\Strictable;
use Kettasoft\Filterable\Filterable;
use Kettasoft\Filterable\Foundation\Resources;
abstract class Engine implements HasInteractsWithOperators, HasFieldMap, Strictable, Executable, HasAllowedFieldChecker
{
/**
* Create Engine instance.
* @param \Kettasoft\Filterable\Contracts\FilterableContext $context
*/
public function __construct(protected FilterableContext $context)
{
$resources = $this->context->getResources()
->setOperators($this->allowedOperators());
$resources->operators->setDefault($this->defaultOperator());
}
/**
* Apply filters to the query.
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return Builder
*/
abstract public function execute(Builder $builder);
/**
* Check if the strict mode is enable in an engine config.
* @return bool
*/
abstract protected function isStrictFromConfig(): bool;
abstract protected function getAllowedFieldsFromConfig(): array;
abstract protected function isIgnoredEmptyValuesFromConfig(): bool;
abstract public function getEngineName(): string;
public function isIgnoredEmptyValues(): bool
{
return $this->isIgnoredEmptyValuesFromConfig() || $this->context->hasIgnoredEmptyValues();
}
public function getAllowedFields(): array
{
return array_merge($this->getAllowedFieldsFromConfig(), $this->context->getAllowedFields());
}
/**
* @inheritDoc
*/
public function allowedOperators(): array
{
if (empty($this->context->getAllowedOperators())) {
return $this->getOperatorsFromConfig();
}
return Arr::only($this->getOperatorsFromConfig(), $this->context->getAllowedOperators());
}
/**
* @inheritDoc
*/
public function getFieldsMap(): array
{
return $this->context->getFieldsMap();
}
/**
* @inheritDoc
*/
public function isStrict(): bool
{
return is_bool($this->context->isStrict()) ? $this->context->isStrict() : $this->isStrictFromConfig();
}
/**
* Get the context instance.
* @return Filterable
*/
public function getContext(): Filterable
{
return $this->context;
}
public function getResources(): Resources
{
return $this->context->getResources();
}
/**
* Sanitize the given value using the sanitizer instance.
*
* @param mixed $filed
* @param mixed $value
*/
final protected function sanitizeValue($filed, $value)
{
$sanitizer = $this->context->getSanitizerInstance();
return $sanitizer->handle($filed, $value);
}
}