Skip to content

Commit 5fd83f2

Browse files
author
Abdalrhman Emad Saad
committed
refactor: add specific interface contexts per engine for better decoupling
- Created dedicated interface contexts for each filter engine defining only the required contract. - Update each engine to depend on its specific interface context instead of a general Filterable class. - Implemented these interfaces in Filterable to provide required methods per engine. - This enhances modularity, clarity, and testability of filter engines
1 parent 11ed35d commit 5fd83f2

File tree

9 files changed

+205
-39
lines changed

9 files changed

+205
-39
lines changed

src/Contracts/FilterableContext.php

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,11 @@
22

33
namespace Kettasoft\Filterable\Contracts;
44

5-
interface FilterableContext
6-
{
7-
/**
8-
* Get current data.
9-
* @return array
10-
*/
11-
public function getData(): mixed;
12-
13-
/**
14-
* Fetch all relevant filters from the filter API class.
15-
*
16-
* @return array
17-
*/
18-
public function getFilterAttributes(): array;
19-
20-
/**
21-
* Get mentors.
22-
* @return array
23-
*/
24-
public function getMentors(): array;
25-
26-
/**
27-
* Check if current filterable class has ignored empty values.
28-
* @return bool
29-
*/
30-
public function hasIgnoredEmptyValues(): bool;
31-
}
5+
use Kettasoft\Filterable\Engines\Contracts\{
6+
TreeFilterableContext,
7+
RulesetFilterableContect,
8+
ExpressionEngineContext,
9+
InvokableEngineContext
10+
};
11+
12+
interface FilterableContext extends TreeFilterableContext, RulesetFilterableContect, ExpressionEngineContext, InvokableEngineContext {}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Kettasoft\Filterable\Engines\Contracts;
4+
5+
use Kettasoft\Filterable\Sanitization\Sanitizer;
6+
7+
interface ExpressionEngineContext
8+
{
9+
/**
10+
* Get current data.
11+
* @return array
12+
*/
13+
public function getData(): mixed;
14+
15+
/**
16+
* Get sanitizer instance.
17+
* @return Sanitizer
18+
*/
19+
public function getSanitizerInstance(): Sanitizer;
20+
21+
/**
22+
* Get allowed fields to apply filtering.
23+
* @return array
24+
*/
25+
public function getAllowedFields(): array;
26+
27+
/**
28+
* Check if a given relation is allowed for filtering.
29+
* @param string $relation
30+
* @return bool
31+
*/
32+
public function isRelationAllowed(string $relation, $field): bool;
33+
34+
/**
35+
* Get columns wrapper.
36+
* @return array
37+
*/
38+
public function getFieldsMap(): array;
39+
40+
/**
41+
* List of supported SQL operators you want to allow when parsing the expressions.
42+
* @return array
43+
*/
44+
public function getAllowedOperators(): array;
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Kettasoft\Filterable\Engines\Contracts;
4+
5+
use Illuminate\Http\Request;
6+
use Kettasoft\Filterable\Sanitization\Sanitizer;
7+
8+
interface InvokableEngineContext
9+
{
10+
/**
11+
* Fetch all relevant filters from the filter API class.
12+
*
13+
* @return array
14+
*/
15+
public function getFilterAttributes(): array;
16+
17+
/**
18+
* Get the current request instance.
19+
* @return Request
20+
*/
21+
public function getRequest(): Request;
22+
23+
/**
24+
* Check if current filterable class has ignored empty values.
25+
* @return bool
26+
*/
27+
public function hasIgnoredEmptyValues(): bool;
28+
29+
/**
30+
* Get sanitizer instance.
31+
* @return Sanitizer
32+
*/
33+
public function getSanitizerInstance(): Sanitizer;
34+
35+
/**
36+
* Get mentors.
37+
* @return array
38+
*/
39+
public function getMentors(): array;
40+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Kettasoft\Filterable\Engines\Contracts;
4+
5+
use Kettasoft\Filterable\Sanitization\Sanitizer;
6+
7+
interface RulesetFilterableContect
8+
{
9+
/**
10+
* Get current data.
11+
* @return array
12+
*/
13+
public function getData(): mixed;
14+
15+
/**
16+
* Check if current filterable class has ignored empty values.
17+
* @return bool
18+
*/
19+
public function hasIgnoredEmptyValues(): bool;
20+
21+
/**
22+
* Get columns wrapper.
23+
* @return array
24+
*/
25+
public function getFieldsMap(): array;
26+
27+
/**
28+
* Get sanitizer instance.
29+
* @return Sanitizer
30+
*/
31+
public function getSanitizerInstance(): Sanitizer;
32+
33+
/**
34+
* Get allowed fields to apply filtering.
35+
* @return array
36+
*/
37+
public function getAllowedFields(): array;
38+
39+
/**
40+
* List of supported SQL operators you want to allow when parsing the expressions.
41+
* @return array
42+
*/
43+
public function getAllowedOperators(): array;
44+
45+
/**
46+
* Check if filter has strict mode.
47+
* @return mixed
48+
*/
49+
public function isStrict();
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Kettasoft\Filterable\Engines\Contracts;
4+
5+
use Kettasoft\Filterable\Sanitization\Sanitizer;
6+
7+
interface TreeFilterableContext
8+
{
9+
/**
10+
* Get current data.
11+
* @return array
12+
*/
13+
public function getData(): mixed;
14+
15+
/**
16+
* Check if current filterable class has ignored empty values.
17+
* @return bool
18+
*/
19+
public function hasIgnoredEmptyValues(): bool;
20+
21+
/**
22+
* Get columns wrapper.
23+
* @return array
24+
*/
25+
public function getFieldsMap(): array;
26+
27+
/**
28+
* Get sanitizer instance.
29+
* @return Sanitizer
30+
*/
31+
public function getSanitizerInstance(): Sanitizer;
32+
33+
/**
34+
* Get allowed fields to apply filtering.
35+
* @return array
36+
*/
37+
public function getAllowedFields(): array;
38+
39+
/**
40+
* List of supported SQL operators you want to allow when parsing the expressions.
41+
* @return array
42+
*/
43+
public function getAllowedOperators(): array;
44+
45+
/**
46+
* Check if filter has strict mode.
47+
* @return mixed
48+
*/
49+
public function isStrict();
50+
}

src/Engines/Expression.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
use Kettasoft\Filterable\Traits\FieldNormalizer;
66
use Kettasoft\Filterable\Engines\Contracts\Engine;
77
use Illuminate\Contracts\Database\Eloquent\Builder;
8-
use Kettasoft\Filterable\Contracts\FilterableContext;
98
use Kettasoft\Filterable\Support\ConditionNormalizer;
109
use Kettasoft\Filterable\Support\ValidateTableColumns;
1110
use Kettasoft\Filterable\Exceptions\InvalidOperatorException;
1211
use Kettasoft\Filterable\Exceptions\NotAllowedFieldException;
12+
use Kettasoft\Filterable\Engines\Contracts\ExpressionEngineContext;
1313

1414
class Expression implements Engine
1515
{
1616
use FieldNormalizer;
1717

1818
/**
1919
* Create Engine instance.
20-
* @param \Kettasoft\Filterable\Contracts\FilterableContext $context
20+
* @param \Kettasoft\Filterable\Engines\Contracts\ExpressionEngineContext $context
2121
*/
22-
public function __construct(protected FilterableContext $context) {}
22+
public function __construct(protected ExpressionEngineContext $context) {}
2323

2424
/**
2525
* Apply filters to the query.

src/Engines/Invokeable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Illuminate\Support\Traits\ForwardsCalls;
88
use Kettasoft\Filterable\Engines\Contracts\Engine;
99
use Illuminate\Contracts\Database\Eloquent\Builder;
10-
use Kettasoft\Filterable\Contracts\FilterableContext;
10+
use Kettasoft\Filterable\Engines\Contracts\InvokableEngineContext;
1111
use Kettasoft\Filterable\Support\ConditionNormalizer;
1212

1313
class Invokeable implements Engine
@@ -22,9 +22,9 @@ class Invokeable implements Engine
2222

2323
/**
2424
* Create Engine instance.
25-
* @param \Kettasoft\Filterable\Contracts\FilterableContext $context
25+
* @param \Kettasoft\Filterable\Engines\Contracts\InvokableEngineContext $context
2626
*/
27-
public function __construct(protected FilterableContext $context) {}
27+
public function __construct(protected InvokableEngineContext $context) {}
2828

2929
/**
3030
* Apply filters to the query.

src/Engines/Ruleset.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
use Kettasoft\Filterable\Traits\FieldNormalizer;
66
use Kettasoft\Filterable\Engines\Contracts\Engine;
77
use Illuminate\Contracts\Database\Eloquent\Builder;
8-
use Kettasoft\Filterable\Contracts\FilterableContext;
98
use Kettasoft\Filterable\Exceptions\InvalidOperatorException;
109
use Kettasoft\Filterable\Exceptions\NotAllowedFieldException;
10+
use Kettasoft\Filterable\Engines\Contracts\RulesetFilterableContect;
1111

1212
class Ruleset implements Engine
1313
{
1414
use FieldNormalizer;
1515

1616
/**
1717
* Create Engine instance.
18-
* @param \Kettasoft\Filterable\Contracts\FilterableContext $context
18+
* @param \Kettasoft\Filterable\Engines\Contracts\RulesetFilterableContect $context
1919
*/
20-
public function __construct(protected FilterableContext $context) {}
20+
public function __construct(protected RulesetFilterableContect $context) {}
2121

2222
/**
2323
* Apply filters to the query.

src/Engines/Tree.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use Kettasoft\Filterable\Engines\Contracts\Engine;
99
use Illuminate\Contracts\Database\Eloquent\Builder;
1010
use Kettasoft\Filterable\Support\RelationPathParser;
11-
use Kettasoft\Filterable\Contracts\FilterableContext;
1211
use Kettasoft\Filterable\Support\AllowedFieldChecker;
1312
use Kettasoft\Filterable\Support\TreeBasedRelationsResolver;
13+
use Kettasoft\Filterable\Engines\Contracts\TreeFilterableContext;
1414
use Kettasoft\Filterable\Engines\Contracts\HasAllowedFieldChecker;
1515
use Kettasoft\Filterable\Support\TreeBasedSignelConditionResolver;
1616
use Kettasoft\Filterable\Engines\Contracts\HasInteractsWithOperators;
@@ -22,9 +22,9 @@ class Tree implements Engine, HasInteractsWithOperators, HasAllowedFieldChecker
2222
protected operatorMapper $operatorMapper;
2323
/**
2424
* Create Engine instance.
25-
* @param \Kettasoft\Filterable\Contracts\FilterableContext $context
25+
* @param \Kettasoft\Filterable\Engines\Contracts\TreeFilterableContext $context
2626
*/
27-
public function __construct(protected FilterableContext $context)
27+
public function __construct(protected TreeFilterableContext $context)
2828
{
2929
$this->operatorMapper = new OperatorMapper($this);
3030
}

0 commit comments

Comments
 (0)