Skip to content

Commit 20e245c

Browse files
author
Abdalrhman Emad Saad
committed
feat: add field normalizer option to convert incoming field names to lowercase
1 parent c42a070 commit 20e245c

File tree

5 files changed

+109
-3
lines changed

5 files changed

+109
-3
lines changed

config/filterable.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@
8585
|
8686
*/
8787
'ignore_empty_values' => false,
88+
89+
/*
90+
|--------------------------------------------------------------------------
91+
| Normalize Field Names
92+
|--------------------------------------------------------------------------
93+
|
94+
| Whether to automatically convert field names to lowercase
95+
| for consistency when parsing filters.
96+
|
97+
*/
98+
'normalize_keys' => false,
8899
],
89100

90101
/*
@@ -153,6 +164,17 @@
153164
|
154165
*/
155166
'ignore_empty_values' => false,
167+
168+
/*
169+
|--------------------------------------------------------------------------
170+
| Normalize Field Names
171+
|--------------------------------------------------------------------------
172+
|
173+
| Whether to automatically convert field names to lowercase
174+
| for consistency when parsing filters.
175+
|
176+
*/
177+
'normalize_keys' => false,
156178
],
157179

158180
/*
@@ -221,6 +243,17 @@
221243
|
222244
*/
223245
'default_operator' => 'eq', // =
246+
247+
/*
248+
|--------------------------------------------------------------------------
249+
| Normalize Field Names
250+
|--------------------------------------------------------------------------
251+
|
252+
| Whether to automatically convert field names to lowercase
253+
| for consistency when parsing filters.
254+
|
255+
*/
256+
'normalize_keys' => false,
224257
],
225258

226259
/*
@@ -307,7 +340,18 @@
307340
| If true, the package will throw an exception if a field
308341
| is not allowed in the allowed fields.
309342
*/
310-
'strict' => true
343+
'strict' => true,
344+
345+
/*
346+
|--------------------------------------------------------------------------
347+
| Normalize Field Names
348+
|--------------------------------------------------------------------------
349+
|
350+
| Whether to automatically convert field names to lowercase
351+
| for consistency when parsing filters.
352+
|
353+
*/
354+
'normalize_keys' => false,
311355
]
312356
],
313357

src/Engines/Expression.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Kettasoft\Filterable\Engines;
44

5+
use Kettasoft\Filterable\Traits\FieldNormalizer;
56
use Kettasoft\Filterable\Engines\Contracts\Engine;
67
use Illuminate\Contracts\Database\Eloquent\Builder;
78
use Kettasoft\Filterable\Contracts\FilterableContext;
@@ -12,6 +13,8 @@
1213

1314
class Expression implements Engine
1415
{
16+
use FieldNormalizer;
17+
1518
/**
1619
* Create Engine instance.
1720
* @param \Kettasoft\Filterable\Contracts\FilterableContext $context
@@ -28,6 +31,9 @@ public function apply(Builder $builder)
2831
$filters = $this->context->getData();
2932

3033
foreach ($filters as $field => $condition) {
34+
35+
$field = $this->normalizeField($field);
36+
3137
if (! is_array($condition)) {
3238
$condition = ConditionNormalizer::normalize($condition, 'eq');
3339
}
@@ -205,4 +211,13 @@ private function defaultOperator(): string
205211
{
206212
return config('filterable.engines.expression.default_operator', 'eq');
207213
}
214+
215+
/**
216+
* Check if normalize field option is enable in engine.
217+
* @return bool
218+
*/
219+
protected function hasNormalizeFieldCondition(): bool
220+
{
221+
return config('filterable.engines.expression.normalize_keys', false);
222+
}
208223
}

src/Engines/Ruleset.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Kettasoft\Filterable\Engines;
44

5+
use Kettasoft\Filterable\Traits\FieldNormalizer;
56
use Kettasoft\Filterable\Engines\Contracts\Engine;
67
use Illuminate\Contracts\Database\Eloquent\Builder;
78
use Kettasoft\Filterable\Contracts\FilterableContext;
@@ -10,6 +11,8 @@
1011

1112
class Ruleset implements Engine
1213
{
14+
use FieldNormalizer;
15+
1316
/**
1417
* Create Engine instance.
1518
* @param \Kettasoft\Filterable\Contracts\FilterableContext $context
@@ -27,6 +30,8 @@ public function apply(Builder $builder): Builder
2730

2831
foreach ($data as $field => $rawValue) {
2932

33+
$field = $this->normalizeField($field);
34+
3035
if (! in_array($field, $this->getAllowedFields())) {
3136
if ($this->isStrict()) {
3237
throw new NotAllowedFieldException($field);
@@ -47,6 +52,15 @@ public function apply(Builder $builder): Builder
4752
return $builder;
4853
}
4954

55+
/**
56+
* Check if normalize field option is enable in engine.
57+
* @return bool
58+
*/
59+
protected function hasNormalizeFieldCondition(): bool
60+
{
61+
return config('filterable.engines.ruleset.normalize_keys', false);
62+
}
63+
5064
/**
5165
* Get allowed fields to filtering.
5266
* @return array

src/Engines/Tree.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use Kettasoft\Filterable\Support\TreeNode;
66
use Kettasoft\Filterable\Support\OperatorMapper;
7+
use Kettasoft\Filterable\Traits\FieldNormalizer;
78
use Kettasoft\Filterable\Engines\Contracts\Engine;
8-
use Kettasoft\Filterable\Support\RelationPathParser;
99
use Illuminate\Contracts\Database\Eloquent\Builder;
10+
use Kettasoft\Filterable\Support\RelationPathParser;
1011
use Kettasoft\Filterable\Contracts\FilterableContext;
1112
use Kettasoft\Filterable\Support\AllowedFieldChecker;
1213
use Kettasoft\Filterable\Support\TreeBasedRelationsResolver;
@@ -16,6 +17,8 @@
1617

1718
class Tree implements Engine, HasInteractsWithOperators, HasAllowedFieldChecker
1819
{
20+
use FieldNormalizer;
21+
1922
protected operatorMapper $operatorMapper;
2023
/**
2124
* Create Engine instance.
@@ -57,7 +60,7 @@ private function applyNode(Builder $builder, TreeNode $node)
5760

5861
[$relation, $field] = RelationPathParser::resolve($node->field);
5962

60-
$field = $this->context->getFieldsMap()[$field] ?? $field;
63+
$field = $this->normalizeField($this->context->getFieldsMap()[$field] ?? $field);
6164
$operator = $this->operatorMapper->map($node->operator);
6265
$value = $this->context->getSanitizerInstance()->handle($field, $node->value);
6366

@@ -77,6 +80,15 @@ private function applyNode(Builder $builder, TreeNode $node)
7780
return $builder;
7881
}
7982

83+
/**
84+
* Check if normalize field option is enable in engine.
85+
* @return bool
86+
*/
87+
protected function hasNormalizeFieldCondition(): bool
88+
{
89+
return config('filterable.engines.tree.normalize_keys', false);
90+
}
91+
8092
/**
8193
* Get all allowed fields.
8294
* @return array

src/Traits/FieldNormalizer.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Kettasoft\Filterable\Traits;
4+
5+
trait FieldNormalizer
6+
{
7+
/**
8+
* Check if normalize field option is enable in engine.
9+
* @return bool
10+
*/
11+
abstract protected function hasNormalizeFieldCondition(): bool;
12+
13+
/**
14+
* Normalize incoming field name to lowercase.
15+
* @param mixed $field
16+
*/
17+
public function normalizeField($field)
18+
{
19+
return $this->hasNormalizeFieldCondition() ? strtolower($field) : $field;
20+
}
21+
}

0 commit comments

Comments
 (0)