-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRuleset.php
More file actions
101 lines (83 loc) · 2.46 KB
/
Ruleset.php
File metadata and controls
101 lines (83 loc) · 2.46 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
<?php
namespace Kettasoft\Filterable\Engines;
use Illuminate\Database\Eloquent\Builder;
use Kettasoft\Filterable\Support\Payload;
use Kettasoft\Filterable\Traits\FieldNormalizer;
use Kettasoft\Filterable\Engines\Foundation\Clause;
use Kettasoft\Filterable\Engines\Foundation\Engine;
use Kettasoft\Filterable\Engines\Foundation\ClauseApplier;
use Kettasoft\Filterable\Engines\Foundation\ClauseFactory;
use Kettasoft\Filterable\Engines\Foundation\Appliers\Applier;
use Kettasoft\Filterable\Exceptions\NotAllowedFieldException;
use Kettasoft\Filterable\Engines\Foundation\Parsers\Dissector;
class Ruleset extends Engine
{
use FieldNormalizer;
/**
* Engine name.
* @var string
*/
protected $name = 'ruleset';
/**
* Apply filters to the query.
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return Builder
*/
public function execute(Builder $builder): Builder
{
$data = $this->context->getData();
foreach ($data as $field => $dissector) {
$dissector = Dissector::parse($dissector, $this->defaultOperator());
$clause = (new ClauseFactory($this))->make(
new Payload($field, $dissector->operator, $this->sanitizeValue($field, $dissector->value), $dissector->value)
);
if (! $clause->validated) continue;
Applier::apply(new ClauseApplier($clause), $builder);
}
return $builder;
}
/**
* Check if normalize field option is enable in engine.
* @return bool
*/
protected function hasNormalizeFieldCondition(): bool
{
return config('filterable.engines.ruleset.normalize_keys', false);
}
/**
* Get engine default operator.
* @return string
*/
public function defaultOperator(): string
{
return config('filterable.engines.ruleset.default_operator', 'eq');
}
/**
* Get allowed fields to filtering.
* @return array
*/
protected function getAllowedFieldsFromConfig(): array
{
return config('filterable.engines.ruleset.allowed_fields', []);
}
public function getOperatorsFromConfig(): array
{
return config('filterable.engines.ruleset.allowed_operators', []);
}
public function isStrictFromConfig(): bool
{
return config('filterable.engines.ruleset.strict', true);
}
protected function isIgnoredEmptyValuesFromConfig(): bool
{
return config('filterable.engines.ruleset.ignore_empty_values', false);
}
/**
* Get engine name.
* @return string
*/
public function getEngineName(): string
{
return $this->name;
}
}