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

Commit 6de2c54

Browse files
committed
Исправлены ошибки, добавлен фильтр keys
1 parent ab8df51 commit 6de2c54

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class FilterDebugger extends ConfigurationDebugger {
1313

1414
protected FiltratorContract $filtrator;
1515

16+
/**
17+
* @var bool Не нужно сбрасывать фильтр после каждой операции логирования?
18+
*/
19+
protected bool $isFreezedFilter = false;
20+
1621
public function getFiltrator(): FiltratorContract {
1722
return $this->filtrator;
1823
}
@@ -23,11 +28,27 @@ public function setFiltrator(FiltratorContract $filtrator): self {
2328
}
2429

2530
public function resetFilter(): self {
26-
$this->getFiltrator()->clearFilters();
31+
if (!$this->isFreezedFilter()) {
32+
$this->getFiltrator()->clearFilters();
33+
}
2734
return $this;
2835
}
2936

30-
public function filtrateItem(array $itemData): array {
37+
public function isFreezedFilter(): self {
38+
return $this->isFreezedFilter === true;
39+
}
40+
41+
public function unfreezeFilter(): self {
42+
$this->isFreezedFilter = false;
43+
return $this;
44+
}
45+
46+
public function freezeFilter(): self {
47+
$this->isFreezedFilter = true;
48+
return $this;
49+
}
50+
51+
public function filtrateItem($itemData) {
3152
return $this->getFiltrator()->filtrate($itemData);
3253
}
3354

@@ -41,6 +62,13 @@ public function last(): self {
4162
return $this;
4263
}
4364

65+
public function keys(array $availableKeys): self {
66+
$this->getFiltrator()->addFilter('keys', [
67+
'keys' => $availableKeys
68+
]);
69+
return $this;
70+
}
71+
4472
public function limit(int $limit = 10): self {
4573
$this->getFiltrator()->addFilter('limit', [
4674
'count' => $limit

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class Filtrator implements FiltratorContract {
1313

1414
public const FILTERS_NAME = [
15-
'limit', 'first', 'last'
15+
'limit', 'first', 'last', 'keys'
1616
];
1717

1818
protected array $filters;
@@ -26,21 +26,36 @@ public function addFilter(string $filterType, array $filterParams = []): void {
2626
}
2727
}
2828

29-
protected function filtrateItem(string $filterType, array $filterParams, array $data): array {
29+
protected function filtrateItem(string $filterType, array $filterParams, $data) {
3030
switch ($filterType) {
3131
case 'limit':
32-
if (empty($filterParams['count'])) {
33-
$filterParams['count'] = 10;
32+
if (\is_array($data) && !empty($data)) {
33+
if (!empty($filterParams['count'])) {
34+
$filterParams['count'] = 10;
35+
}
36+
$data = array_slice($data, 0, $filterParams['count'], true);
3437
}
35-
$data = array_slice($data, 0, $filterParams['count'], true);
3638
break;
3739
case 'first':
38-
if (!empty($data[0])) {
40+
if (\is_array($data) && !empty($data[0])) {
3941
$data = $data[0];
4042
}
4143
break;
44+
case 'keys':
45+
if (\is_array($data) && !empty($data) && !empty($filterParams['keys'])) {
46+
$newData = [];
47+
48+
foreach ($data as $k => $v) {
49+
if (\in_array($k, $filterParams['keys'])) {
50+
$newData[$k] = $v;
51+
}
52+
}
53+
54+
$data = $newData;
55+
}
56+
break;
4257
case 'last':
43-
if (!empty($data)) {
58+
if (\is_array($data) && !empty($data)) {
4459
$data = $data[sizeof($data) - 1];
4560
}
4661
break;
@@ -53,7 +68,7 @@ public function clearFilters(): void {
5368
$this->filters = [];
5469
}
5570

56-
public function filtrate(array $data): array {
71+
public function filtrate($data) {
5772
if (!empty($this->filters)) {
5873
foreach ($this->filters as $arFilter) {
5974
$data = $this->filtrateItem($arFilter['type'], $arFilter['params'], $data);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author ggrachdev
88
*/
99
interface FiltratorContract {
10-
public function filtrate(array $data): array;
10+
public function filtrate($data);
1111

1212
public function filtrateItem(string $filterType,array $data): array;
1313

module/ggrachdev.debugbar/classes/general/Filtrator/structure.puml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
skinparam classAttributeIconSize 0
33

44
interface FiltratorContract {
5-
+ array filtrate(array $data)
6-
# array filtrateItem(string $filterType, array $filterParams, array $data)
5+
+ mixed filtrate(mixed $data)
6+
# array filtrateItem(string $filterType, array $filterParams, mixed $data): mixed
77

88
+ void addFilter(string $filterType, array $filterParams = [])
99
+ bool hasFilter(string $filterType)
@@ -14,8 +14,8 @@
1414
# const array FILTERS_NAME
1515
# array $filters
1616

17-
+ array filtrate(array $data)
18-
# array filtrateItem(string $filterType, array $filterParams, array $data)
17+
+ mixed filtrate(mixed $data)
18+
# array filtrateItem(string $filterType, array $filterParams, mixed $data): mixed
1919

2020
+ void addFilter(string $filterType, array $filterParams = [])
2121
+ bool hasFilter(string $filterType)

0 commit comments

Comments
 (0)