Skip to content

Commit 7f0f6a2

Browse files
committed
Add FilterScope to every Filter
1 parent 5e54a67 commit 7f0f6a2

12 files changed

Lines changed: 72 additions & 33 deletions

Component/ComponentViewModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function getValue(): mixed
5555
}
5656

5757
$value = $this->getComponent()->getRepository()->getValue();
58-
$value = $this->filter->filter($this->getComponent()->getFilters(), $value);
58+
$value = $this->filter->filter($this->getComponent(), $value);
5959

6060
if (false === $this->component->isValidated()) {
6161
$this->validator->validate($this->component, $value);

Filter/Capitalize.php

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

33
namespace Loki\Components\Filter;
44

5-
use Loki\Components\Component\ComponentInterface;
6-
75
class Capitalize implements FilterInterface
86
{
9-
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed
7+
public function filter(mixed $value, FilterScope $scope): mixed
108
{
119
return ucfirst((string)$value);
1210
}

Filter/Filter.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,38 @@
77
class Filter
88
{
99
public function __construct(
10-
private readonly FilterRegistry $filterRegistry
10+
private readonly FilterRegistry $filterRegistry,
11+
private readonly FilterScopeFactory $filterScopeFactory,
1112
) {
1213
}
1314

1415
public function filter(
1516
ComponentInterface $component,
1617
mixed $data = null,
17-
?string $scope = null
18+
mixed $propertyName = null
1819
): mixed {
19-
$requestedFilters = $component->getFilters();
20+
$filters = $this->filterRegistry->getApplicableFilters(
21+
$component->getFilters()
22+
);
23+
24+
$filterScope = $this->filterScopeFactory->create();
25+
$filterScope->setComponent($component);
26+
$filterScope->setProperty($propertyName);
2027

2128
if (empty($data) || is_bool($data) || is_int($data)) {
2229
return $data;
2330
}
2431

2532
if (is_array($data)) {
26-
foreach ($data as $name => $value) {
27-
$data[$name] = $this->filter($component, $value, $name);
33+
foreach ($data as $propertyName => $value) {
34+
$data[$propertyName] = $this->filter($component, $value, $propertyName);
2835
}
2936

3037
return $data;
3138
}
3239

33-
$filters = $this->filterRegistry->getApplicableFilters(
34-
$requestedFilters
35-
);
3640
foreach ($filters as $filter) {
37-
$data = $filter->filter($data, $component, $scope);
41+
$data = $filter->filter($data, $filterScope);
3842
}
3943

4044
return $data;

Filter/FilterInterface.php

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

44
namespace Loki\Components\Filter;
55

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

Filter/FilterScope.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Filter;
4+
5+
use Loki\Components\Component\ComponentInterface;
6+
7+
class FilterScope
8+
{
9+
private ComponentInterface|null $component = null;
10+
private mixed $property = null;
11+
12+
public function getComponent(): ?ComponentInterface
13+
{
14+
return $this->component;
15+
}
16+
17+
public function setComponent(?ComponentInterface $component): void
18+
{
19+
$this->component = $component;
20+
}
21+
22+
public function getProperty(): mixed
23+
{
24+
return $this->property;
25+
}
26+
27+
public function setProperty(mixed $property): void
28+
{
29+
$this->property = $property;
30+
}
31+
}

Filter/FilterScopeFactory.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Filter;
4+
5+
use Magento\Framework\ObjectManagerInterface;
6+
7+
class FilterScopeFactory
8+
{
9+
public function __construct(
10+
private ObjectManagerInterface $objectManager,
11+
){
12+
}
13+
14+
public function create(): FilterScope
15+
{
16+
return $this->objectManager->create(FilterScope::class);
17+
}
18+
}

Filter/Lowercase.php

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

33
namespace Loki\Components\Filter;
44

5-
use Loki\Components\Component\ComponentInterface;
6-
75
class Lowercase implements FilterInterface
86
{
9-
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed
7+
public function filter(mixed $value, FilterScope $scope): mixed
108
{
119
return strtolower((string)$value);
1210
}

Filter/Number.php

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

33
namespace Loki\Components\Filter;
44

5-
use Loki\Components\Component\ComponentInterface;
6-
75
class Number implements FilterInterface
86
{
9-
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed
7+
public function filter(mixed $value, FilterScope $scope): mixed
108
{
119
return (int)$value;
1210
}

Filter/PositiveNumber.php

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

33
namespace Loki\Components\Filter;
44

5-
use Loki\Components\Component\ComponentInterface;
6-
75
class PositiveNumber implements FilterInterface
86
{
9-
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed
7+
public function filter(mixed $value, FilterScope $scope): mixed
108
{
119
if ($value > 0) {
1210
return $value;

Filter/Security.php

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

33
namespace Loki\Components\Filter;
44

5-
use Loki\Components\Component\ComponentInterface;
6-
75
class Security implements FilterInterface
86
{
9-
public function filter(mixed $value, ?ComponentInterface $component = null, ?string $scope = null): mixed
7+
public function filter(mixed $value, FilterScope $scope): mixed
108
{
119
if (is_object($value)) {
1210
return $value;
1311
}
1412

1513
$value = (string)$value;
16-
//$value = strip_tags($value);
14+
$value = strip_tags($value);
1715
$value = htmlspecialchars_decode($value);
1816
return $value;
1917
}

0 commit comments

Comments
 (0)