Skip to content

Commit 36547ae

Browse files
authored
Merge pull request #35 from kettasoft/refactor/engine-refactoring
Refactor Engine config access to unified pattern
2 parents 03813b0 + 1a64c2a commit 36547ae

File tree

5 files changed

+47
-149
lines changed

5 files changed

+47
-149
lines changed

src/Engines/Expression.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
namespace Kettasoft\Filterable\Engines;
44

55
use Kettasoft\Filterable\Support\Payload;
6-
use Kettasoft\Filterable\Traits\FieldNormalizer;
76
use Illuminate\Contracts\Database\Eloquent\Builder;
8-
use Kettasoft\Filterable\Engines\Foundation\Clause;
97
use Kettasoft\Filterable\Engines\Foundation\Engine;
108
use Kettasoft\Filterable\Support\ConditionNormalizer;
119
use Kettasoft\Filterable\Support\ValidateTableColumns;
1210
use Kettasoft\Filterable\Engines\Foundation\ClauseApplier;
1311
use Kettasoft\Filterable\Engines\Foundation\ClauseFactory;
14-
use Kettasoft\Filterable\Engines\Foundation\Enums\Operators;
1512
use Kettasoft\Filterable\Engines\Foundation\Appliers\Applier;
16-
use Kettasoft\Filterable\Exceptions\InvalidOperatorException;
17-
use Kettasoft\Filterable\Exceptions\NotAllowedFieldException;
1813
use Kettasoft\Filterable\Engines\Foundation\Parsers\Dissector;
1914

2015
class Expression extends Engine
@@ -57,11 +52,6 @@ public function execute(Builder $builder)
5752
return $builder;
5853
}
5954

60-
public function getAllowedFieldsFromConfig(): array
61-
{
62-
return config('filterable.engines.expression.allowed_fields', []);
63-
}
64-
6555
/**
6656
* Check if the given column is registered in schema.
6757
* @param mixed $column
@@ -76,16 +66,6 @@ protected function validateTableColumns($builder, $column)
7666
return true;
7767
}
7868

79-
public function getOperatorsFromConfig(): array
80-
{
81-
return config('filterable.engines.expression.allowed_operators', []);
82-
}
83-
84-
public function isStrictFromConfig(): bool
85-
{
86-
return config('filterable.engines.expression.strict', true);
87-
}
88-
8969
/**
9070
* Get engine default operator.
9171
* @return string
@@ -95,11 +75,6 @@ public function defaultOperator(): string
9575
return config('filterable.engines.expression.default_operator', 'eq');
9676
}
9777

98-
public function isIgnoredEmptyValuesFromConfig(): bool
99-
{
100-
return config('filterable.engines.expression.ignore_empty_values', false);
101-
}
102-
10378
/**
10479
* Get engine name.
10580
* @return string

src/Engines/Foundation/Engine.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ abstract class Engine implements HasInteractsWithOperators, HasFieldMap, Stricta
1717
{
1818
/**
1919
* Create Engine instance.
20-
* @param \Kettasoft\Filterable\Contracts\FilterableContext $context
20+
* @param Filterable $context
2121
*/
22-
public function __construct(protected FilterableContext $context)
23-
{
24-
$resources = $this->context->getResources()
25-
->setOperators($this->allowedOperators());
22+
public function __construct(protected Filterable $context) {}
2623

27-
$resources->operators->setDefault($this->defaultOperator());
28-
}
24+
/**
25+
* Get engine name.
26+
* @return string
27+
*/
28+
abstract public function getEngineName(): string;
2929

3030
/**
3131
* Apply filters to the query.
@@ -35,16 +35,40 @@ public function __construct(protected FilterableContext $context)
3535
abstract public function execute(Builder $builder);
3636

3737
/**
38-
* Check if the strict mode is enable in an engine config.
39-
* @return bool
38+
* Get allowed fields to filtering.
39+
* @return array
4040
*/
41-
abstract protected function isStrictFromConfig(): bool;
41+
protected function getAllowedFieldsFromConfig(): array
42+
{
43+
return config("filterable.engines.{$this->getEngineName()}.allowed_fields", []);
44+
}
4245

43-
abstract protected function getAllowedFieldsFromConfig(): array;
46+
/**
47+
* Check if empty values are ignored from engine config.
48+
* @return bool
49+
*/
50+
protected function isIgnoredEmptyValuesFromConfig(): bool
51+
{
52+
return config("filterable.engines.{$this->getEngineName()}.ignore_empty_values", false);
53+
}
4454

45-
abstract protected function isIgnoredEmptyValuesFromConfig(): bool;
55+
/**
56+
* Get allowed operators to filtering.
57+
* @return array
58+
*/
59+
public function getOperatorsFromConfig(): array
60+
{
61+
return config("filterable.engines.{$this->getEngineName()}.allowed_operators", []);
62+
}
4663

47-
abstract public function getEngineName(): string;
64+
/**
65+
* Check if the strict mode is enable in an engine config.
66+
* @return bool
67+
*/
68+
protected function isStrictFromConfig(): bool
69+
{
70+
return config("filterable.engines.{$this->getEngineName()}.strict", false);
71+
}
4872

4973
public function isIgnoredEmptyValues(): bool
5074
{

src/Engines/Invokable.php

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
namespace Kettasoft\Filterable\Engines;
44

55
use Illuminate\Support\Str;
6+
use Kettasoft\Filterable\Filterable;
67
use Illuminate\Database\Eloquent\Builder;
7-
use Kettasoft\Filterable\Engines\Foundation\Attributes\AttributeRegistry;
88
use Kettasoft\Filterable\Support\Payload;
99
use Illuminate\Support\Traits\ForwardsCalls;
1010
use Kettasoft\Filterable\Engines\Foundation\Engine;
11-
use Kettasoft\Filterable\Engines\Foundation\Attributes\AttributeContext;
12-
use Kettasoft\Filterable\Engines\Foundation\Attributes\AttributePipeline;
13-
use Kettasoft\Filterable\Engines\Foundation\Clause;
1411
use Kettasoft\Filterable\Engines\Foundation\ClauseFactory;
1512
use Kettasoft\Filterable\Engines\Foundation\Parsers\Dissector;
16-
use Kettasoft\Filterable\Filterable;
13+
use Kettasoft\Filterable\Engines\Foundation\Attributes\AttributeContext;
14+
use Kettasoft\Filterable\Engines\Foundation\Attributes\AttributePipeline;
15+
use Kettasoft\Filterable\Engines\Foundation\Attributes\AttributeRegistry;
1716

1817
class Invokable extends Engine
1918
{
@@ -109,45 +108,6 @@ protected function getMethodName(string $filter): string
109108
return $this->context->getRequest()->has($filter) ? Str::camel($filter) : 'default' . Str::studly($filter);
110109
}
111110

112-
/**
113-
* Get allowed fields to filtering.
114-
* @return array
115-
*/
116-
protected function getAllowedFieldsFromConfig(): array
117-
{
118-
return config('filterable.engines.invokable.allowed_fields', []);
119-
}
120-
121-
/**
122-
* Get allowed operators to filtering.
123-
*
124-
* @return array
125-
*/
126-
public function getOperatorsFromConfig(): array
127-
{
128-
return config('filterable.engines.invokable.allowed_operators', []);
129-
}
130-
131-
/**
132-
* Check if filter has strict mode.
133-
*
134-
* @return bool
135-
*/
136-
public function isStrictFromConfig(): bool
137-
{
138-
return config('filterable.engines.invokable.strict', true);
139-
}
140-
141-
/**
142-
* Check if empty values are ignored from config.
143-
*
144-
* @return bool
145-
*/
146-
public function isIgnoredEmptyValuesFromConfig(): bool
147-
{
148-
return config('filterable.engines.invokable.ignore_empty_values', false);
149-
}
150-
151111
/**
152112
* Get engine default operator.
153113
* @return string

src/Engines/Ruleset.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
use Illuminate\Database\Eloquent\Builder;
66
use Kettasoft\Filterable\Support\Payload;
77
use Kettasoft\Filterable\Traits\FieldNormalizer;
8-
use Kettasoft\Filterable\Engines\Foundation\Clause;
98
use Kettasoft\Filterable\Engines\Foundation\Engine;
109
use Kettasoft\Filterable\Engines\Foundation\ClauseApplier;
1110
use Kettasoft\Filterable\Engines\Foundation\ClauseFactory;
1211
use Kettasoft\Filterable\Engines\Foundation\Appliers\Applier;
13-
use Kettasoft\Filterable\Exceptions\NotAllowedFieldException;
1412
use Kettasoft\Filterable\Engines\Foundation\Parsers\Dissector;
1513

1614
class Ruleset extends Engine
@@ -68,30 +66,6 @@ public function defaultOperator(): string
6866
return config('filterable.engines.ruleset.default_operator', 'eq');
6967
}
7068

71-
/**
72-
* Get allowed fields to filtering.
73-
* @return array
74-
*/
75-
protected function getAllowedFieldsFromConfig(): array
76-
{
77-
return config('filterable.engines.ruleset.allowed_fields', []);
78-
}
79-
80-
public function getOperatorsFromConfig(): array
81-
{
82-
return config('filterable.engines.ruleset.allowed_operators', []);
83-
}
84-
85-
public function isStrictFromConfig(): bool
86-
{
87-
return config('filterable.engines.ruleset.strict', true);
88-
}
89-
90-
protected function isIgnoredEmptyValuesFromConfig(): bool
91-
{
92-
return config('filterable.engines.ruleset.ignore_empty_values', false);
93-
}
94-
9569
/**
9670
* Get engine name.
9771
* @return string

src/Engines/Tree.php

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,10 @@
66
use Kettasoft\Filterable\Support\Payload;
77
use Kettasoft\Filterable\Support\TreeNode;
88
use Kettasoft\Filterable\Traits\FieldNormalizer;
9-
use Kettasoft\Filterable\Engines\Foundation\Clause;
109
use Kettasoft\Filterable\Engines\Foundation\Engine;
11-
use Kettasoft\Filterable\Support\RelationPathParser;
12-
use Kettasoft\Filterable\Support\AllowedFieldChecker;
1310
use Kettasoft\Filterable\Engines\Foundation\ClauseApplier;
1411
use Kettasoft\Filterable\Engines\Foundation\ClauseFactory;
15-
use Kettasoft\Filterable\Support\TreeBasedRelationsResolver;
1612
use Kettasoft\Filterable\Engines\Foundation\Appliers\Applier;
17-
use Kettasoft\Filterable\Engines\Contracts\TreeFilterableContext;
18-
use Kettasoft\Filterable\Engines\Contracts\HasAllowedFieldChecker;
19-
use Kettasoft\Filterable\Support\TreeBasedSignelConditionResolver;
20-
use Kettasoft\Filterable\Engines\Contracts\HasInteractsWithOperators;
2113

2214
class Tree extends Engine
2315
{
@@ -41,6 +33,12 @@ public function execute(Builder $builder)
4133
return $this->applyNode($builder, TreeNode::parse($data));
4234
}
4335

36+
/**
37+
* Apply tree node to the query builder.
38+
* @param \Illuminate\Database\Eloquent\Builder $builder
39+
* @param \Kettasoft\Filterable\Support\TreeNode $node
40+
* @return Builder
41+
*/
4442
private function applyNode(Builder $builder, TreeNode $node)
4543
{
4644
if ($node->isGroup()) {
@@ -84,24 +82,6 @@ protected function hasNormalizeFieldCondition(): bool
8482
return config('filterable.engines.tree.normalize_keys', false);
8583
}
8684

87-
/**
88-
* Get allowed fields to filtering.
89-
* @return array
90-
*/
91-
protected function getAllowedFieldsFromConfig(): array
92-
{
93-
return config('filterable.engines.tree.allowed_fields', []);
94-
}
95-
96-
/**
97-
* Get all operators.
98-
* @return array
99-
*/
100-
public function operators(): array
101-
{
102-
return config('filterable.engines.tree.allowed_operators', []);
103-
}
104-
10585
/**
10686
* Default operator for use.
10787
* @return mixed|\Illuminate\Config\Repository
@@ -111,21 +91,6 @@ public function defaultOperator()
11191
return config('filterable.engines.tree.default_operator', null);
11292
}
11393

114-
public function getOperatorsFromConfig(): array
115-
{
116-
return config('filterable.engines.tree.allowed_operators', []);
117-
}
118-
119-
public function isStrictFromConfig(): bool
120-
{
121-
return config('filterable.engines.tree.strict', true);
122-
}
123-
124-
public function isIgnoredEmptyValuesFromConfig(): bool
125-
{
126-
return config('filterable.engines.tree.ignore_empty_values', false);
127-
}
128-
12994
/**
13095
* Get engine name.
13196
* @return string

0 commit comments

Comments
 (0)