Skip to content

Commit 5e54a67

Browse files
committed
Add component and scope args to FilterInterface::filter()
1 parent ac2de23 commit 5e54a67

11 files changed

Lines changed: 42 additions & 16 deletions

File tree

Filter/Capitalize.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Loki\Components\Filter;
44

5+
use Loki\Components\Component\ComponentInterface;
6+
57
class Capitalize implements FilterInterface
68
{
7-
public function filter(mixed $value): mixed
9+
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed
810
{
911
return ucfirst((string)$value);
1012
}

Filter/Filter.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,39 @@
22

33
namespace Loki\Components\Filter;
44

5+
use Loki\Components\Component\ComponentInterface;
6+
57
class Filter
68
{
79
public function __construct(
810
private readonly FilterRegistry $filterRegistry
911
) {
1012
}
1113

12-
public function filter(array $requestedFilters = [], mixed $data = null): mixed
13-
{
14+
public function filter(
15+
ComponentInterface $component,
16+
mixed $data = null,
17+
?string $scope = null
18+
): mixed {
19+
$requestedFilters = $component->getFilters();
20+
1421
if (empty($data) || is_bool($data) || is_int($data)) {
1522
return $data;
1623
}
1724

1825
if (is_array($data)) {
1926
foreach ($data as $name => $value) {
20-
$data[$name] = $this->filter($requestedFilters, $value);
27+
$data[$name] = $this->filter($component, $value, $name);
2128
}
2229

2330
return $data;
2431
}
2532

26-
$filters = $this->filterRegistry->getApplicableFilters($requestedFilters);
33+
$filters = $this->filterRegistry->getApplicableFilters(
34+
$requestedFilters
35+
);
2736
foreach ($filters as $filter) {
28-
$data = $filter->filter($data);
37+
$data = $filter->filter($data, $component, $scope);
2938
}
3039

3140
return $data;

Filter/FilterInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
namespace Loki\Components\Filter;
55

6+
use Loki\Components\Component\ComponentInterface;
7+
68
interface FilterInterface
79
{
8-
public function filter(mixed $value): mixed;
10+
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed;
911
}

Filter/Lowercase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Loki\Components\Filter;
44

5+
use Loki\Components\Component\ComponentInterface;
6+
57
class Lowercase implements FilterInterface
68
{
7-
public function filter(mixed $value): mixed
9+
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed
810
{
911
return strtolower((string)$value);
1012
}

Filter/Number.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Loki\Components\Filter;
44

5+
use Loki\Components\Component\ComponentInterface;
6+
57
class Number implements FilterInterface
68
{
7-
public function filter(mixed $value): mixed
9+
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed
810
{
911
return (int)$value;
1012
}

Filter/PositiveNumber.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Loki\Components\Filter;
44

5+
use Loki\Components\Component\ComponentInterface;
6+
57
class PositiveNumber implements FilterInterface
68
{
7-
public function filter(mixed $value): mixed
9+
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed
810
{
911
if ($value > 0) {
1012
return $value;

Filter/Security.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
namespace Loki\Components\Filter;
44

5+
use Loki\Components\Component\ComponentInterface;
6+
57
class Security implements FilterInterface
68
{
7-
public function filter(mixed $value): mixed
9+
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed
810
{
911
if (is_object($value)) {
1012
return $value;
1113
}
1214

1315
$value = (string)$value;
14-
$value = strip_tags($value);
16+
//$value = strip_tags($value);
1517
$value = htmlspecialchars_decode($value);
1618
return $value;
1719
}

Filter/Trim.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Loki\Components\Filter;
44

5+
use Loki\Components\Component\ComponentInterface;
6+
57
class Trim implements FilterInterface
68
{
7-
public function filter(mixed $value): mixed
9+
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed
810
{
911
return trim((string)$value);
1012
}

Filter/Uppercase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Loki\Components\Filter;
44

5+
use Loki\Components\Component\ComponentInterface;
6+
57
class Uppercase implements FilterInterface
68
{
7-
public function filter(mixed $value): mixed
9+
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed
810
{
911
return strtoupper((string)$value);
1012
}

Util/Controller/RepositoryDispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function dispatch(ComponentInterface $component, mixed $componentData): v
2626
return;
2727
}
2828

29-
$componentData = $this->filter->filter($component->getFilters(), $componentData);
29+
$componentData = $this->filter->filter($component, $componentData);
3030
if (false === $this->validator->validate($component, $componentData)) {
3131
return;
3232
}

0 commit comments

Comments
 (0)