Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 7befa0b

Browse files
committed
Add method addFilter
1 parent ed0803b commit 7befa0b

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

ggrachdev.debugbar/classes/general/BitrixDebugger/Debugger/FilterDebugger.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,29 @@ class FilterDebugger extends ConfigurationDebugger {
2121
*/
2222
protected $isFreezedFilter = false;
2323

24+
public function __call($name, $arguments) {
25+
26+
if($this->getFiltrator()->hasCustomFilter($name))
27+
{
28+
$this->getFiltrator()->addFilter($name, $arguments);
29+
}
30+
else
31+
{
32+
throw new \BadMethodCallException('Not found '.$name.' method');
33+
}
34+
35+
return $this;
36+
}
37+
2438
public function getFiltrator(): FiltratorContract {
2539
return $this->filtrator;
2640
}
2741

42+
public function addFilter(string $nameMethod, callable $callback): self {
43+
$this->getFiltrator()->addCustomFilter($nameMethod, $callback);
44+
return $this;
45+
}
46+
2847
public function setFiltrator(FiltratorContract $filtrator): self {
2948
$this->filtrator = $filtrator;
3049
return $this;

ggrachdev.debugbar/classes/general/Filtrator/Filtrator.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ class Filtrator implements FiltratorContract {
1313
'limit', 'first', 'last', 'keys'
1414
];
1515

16+
private $customFilters = [];
17+
1618
/**
1719
*
1820
* @var array
1921
*/
2022
protected $sequenceFilters;
2123

2224
public function addFilter(string $filterType, array $filterParams = []): void {
23-
if ($this->hasFilter($filterType)) {
25+
if ($this->hasFilter($filterType) || $this->hasCustomFilter($filterType)) {
2426
$this->sequenceFilters[] = [
2527
'type' => $filterType,
2628
'params' => $filterParams
@@ -73,7 +75,13 @@ public function clearFilters(): void {
7375
public function filtrate($data) {
7476
if (!empty($this->sequenceFilters) && !empty($data)) {
7577
foreach ($this->sequenceFilters as $arFilter) {
76-
$data = $this->filtrateItem($arFilter['type'], $arFilter['params'], $data);
78+
if($this->hasCustomFilter($arFilter['type']))
79+
{
80+
$data = $this->customFiltrateItem($arFilter['type'], $arFilter['params'], $data);
81+
}
82+
else {
83+
$data = $this->filtrateItem($arFilter['type'], $arFilter['params'], $data);
84+
}
7785
}
7886
}
7987

@@ -84,4 +92,20 @@ public function hasFilter(string $filterType): bool {
8492
return \in_array($filterType, self::FILTERS_NAME);
8593
}
8694

95+
public function addCustomFilter(string $filterName, callable $callback) {
96+
if (!$this->hasCustomFilter($filterName)) {
97+
$this->customFilters[$filterName] = $callback;
98+
}
99+
100+
return $this;
101+
}
102+
103+
public function customFiltrateItem(string $filterType, array $filterParams, $data) {
104+
return $this->customFilters[$filterType]($data, $filterParams);
105+
}
106+
107+
public function hasCustomFilter(string $filterName) {
108+
return \array_key_exists($filterName, $this->customFilters);
109+
}
110+
87111
}

ggrachdev.debugbar/classes/general/Filtrator/FiltratorContract.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
interface FiltratorContract {
1010
public function filtrate($data);
1111

12+
public function addCustomFilter(string $filterName, callable $callback);
13+
14+
public function hasCustomFilter(string $filterName);
15+
16+
public function customFiltrateItem(string $filterType, array $filterParams, $data);
17+
1218
public function filtrateItem(string $filterType, array $filterParams, $data);
1319

1420
public function addFilter(string $filterType, array $filterParams): void;

0 commit comments

Comments
 (0)