99 */
1010class Filtrator implements FiltratorContract {
1111
12- public const FILTERS_NAME = [
13- 'limit ' , 'first ' , 'last ' , 'keys '
14- ];
15-
1612 private $ customFilters = [];
1713
1814 /**
@@ -22,91 +18,43 @@ class Filtrator implements FiltratorContract {
2218 */
2319 protected $ sequenceFilters ;
2420
25- public function addFilter (string $ filterType , array $ filterParams = []): void {
26- if ($ this ->hasFilter ($ filterType ) || $ this ->hasCustomFilter ($ filterType )) {
27- $ this ->sequenceFilters [] = [
28- 'type ' => $ filterType ,
29- 'params ' => $ filterParams
30- ];
31- }
32- }
33-
34- public function filtrateItem (string $ filterType , array $ filterParams , $ data ) {
35- switch ($ filterType ) {
36- case 'limit ' :
37- if (\is_array ($ data ) && !empty ($ data )) {
38- if (empty ($ filterParams ['count ' ]) || $ filterParams ['count ' ] < 1 ) {
39- $ filterParams ['count ' ] = 10 ;
40- }
41- $ data = array_slice ($ data , 0 , $ filterParams ['count ' ], true );
42- }
43- break ;
44- case 'first ' :
45- if (\is_array ($ data ) && !empty ($ data )) {
46- $ data = array_shift ($ data );
47- }
48- break ;
49- case 'keys ' :
50- if (\is_array ($ data ) && !empty ($ data ) && !empty ($ filterParams ['keys ' ])) {
51- $ newData = [];
52-
53- foreach ($ data as $ k => $ v ) {
54- if (\in_array ($ k , $ filterParams ['keys ' ])) {
55- $ newData [$ k ] = $ v ;
56- }
57- }
58-
59- $ data = $ newData ;
60- }
61- break ;
62- case 'last ' :
63- if (\is_array ($ data ) && !empty ($ data )) {
64- $ data = array_pop ($ data );
65- }
66- break ;
67- }
68-
69- return $ data ;
70- }
71-
7221 public function clearFilters (): void {
7322 $ this ->sequenceFilters = [];
7423 }
7524
7625 public function filtrate ($ data ) {
7726 if (!empty ($ this ->sequenceFilters ) && !empty ($ data )) {
7827 foreach ($ this ->sequenceFilters as $ arFilter ) {
79- if ($ this ->hasCustomFilter ($ arFilter ['type ' ]))
80- {
81- $ data = $ this ->customFiltrateItem ($ arFilter ['type ' ], $ arFilter ['params ' ], $ data );
82- }
83- else {
84- $ data = $ this ->filtrateItem ($ arFilter ['type ' ], $ arFilter ['params ' ], $ data );
85- }
28+ $ data = $ this ->filtrateItem ($ arFilter ['type ' ], $ arFilter ['params ' ], $ data );
8629 }
8730 }
8831
8932 return $ data ;
9033 }
9134
35+ public function addFilter (string $ filterType , array $ filterParams = []): void {
36+ if ($ this ->hasFilter ($ filterType )) {
37+ $ this ->sequenceFilters [] = [
38+ 'type ' => $ filterType ,
39+ 'params ' => $ filterParams
40+ ];
41+ }
42+ }
43+
9244 public function hasFilter (string $ filterType ): bool {
93- return \in_array ($ filterType , self :: FILTERS_NAME );
45+ return \array_key_exists ($ filterType , $ this -> customFilters );
9446 }
9547
96- public function addCustomFilter (string $ filterName , callable $ callback ) {
97- if (!$ this ->hasCustomFilter ($ filterName )) {
48+ public function addFilterRule (string $ filterName , callable $ callback ): FiltratorContract {
49+ if (!$ this ->hasFilter ($ filterName )) {
9850 $ this ->customFilters [$ filterName ] = $ callback ;
9951 }
10052
10153 return $ this ;
10254 }
10355
104- public function customFiltrateItem (string $ filterType , array $ filterParams , $ data ) {
56+ public function filtrateItem (string $ filterType , array $ filterParams , $ data ) {
10557 return $ this ->customFilters [$ filterType ]($ data , $ filterParams );
10658 }
10759
108- public function hasCustomFilter (string $ filterName ) {
109- return \array_key_exists ($ filterName , $ this ->customFilters );
110- }
111-
11260}
0 commit comments