25
25
trait FilteringTrait
26
26
{
27
27
/** @var array user custom filters */
28
- private $ _filters = [];
28
+ private array $ _filters = [];
29
29
30
30
/**
31
31
* value sanitize 直接对给的值进行过滤
32
32
*
33
- * @param mixed $value
34
- * @param string|array $filters
35
- * string:
33
+ * filters:
34
+ * string:
36
35
* 'string|trim|upper'
37
- * array:
38
- * [
36
+ * array:
37
+ * [
39
38
* 'string',
40
39
* 'trim',
41
40
* ['Class', 'method'],
42
41
* // 追加额外参数. 传入时,第一个参数总是要过滤的字段值,其余的依次追加
43
42
* 'myFilter' => ['arg1', 'arg2'],
44
43
* function($val) {
45
- * return str_replace(' ', '', $val);
44
+ * return str_replace(' ', '', $val);
46
45
* },
47
- * ]
46
+ * ]
47
+ *
48
+ * @param mixed $value
49
+ * @param array|string|callable $filters
48
50
*
49
51
* @return mixed
50
- * @throws InvalidArgumentException
51
52
*/
52
- protected function valueFiltering ($ value , $ filters )
53
+ protected function valueFiltering (mixed $ value , array | string | callable $ filters ): mixed
53
54
{
54
- $ filters = is_string ($ filters ) ? Filters::explode ($ filters , '| ' ) : $ filters ;
55
-
56
- // fix: must ensure is array
57
- if (!is_array ($ filters )) {
55
+ if (is_string ($ filters )) {
56
+ $ filters = Filters::explode ($ filters , '| ' );
57
+ } elseif (!is_array ($ filters )) {
58
58
$ filters = [$ filters ];
59
59
}
60
60
@@ -64,14 +64,14 @@ protected function valueFiltering($value, $filters)
64
64
$ args = (array )$ filter ;
65
65
$ value = $ this ->callStringCallback ($ key , $ value , ...$ args );
66
66
67
- // closure
67
+ // closure
68
68
} elseif (is_object ($ filter ) && method_exists ($ filter , '__invoke ' )) {
69
69
$ value = $ filter ($ value );
70
- // string, trim, ....
70
+ // string, trim, ....
71
71
} elseif (is_string ($ filter )) {
72
72
$ value = $ this ->callStringCallback ($ filter , $ value );
73
73
74
- // e.g ['Class', 'method'],
74
+ // e.g ['Class', 'method'],
75
75
} else {
76
76
$ value = Helper::call ($ filter , $ value );
77
77
}
@@ -87,29 +87,29 @@ protected function valueFiltering($value, $filters)
87
87
* @return mixed
88
88
* @throws InvalidArgumentException
89
89
*/
90
- protected function callStringCallback (string $ filter , ...$ args )
90
+ protected function callStringCallback (string $ filter , ...$ args ): mixed
91
91
{
92
92
// if is alias name
93
93
$ filterName = Filters::realName ($ filter );
94
94
95
95
// if $filter is a custom by addFiler()
96
96
if ($ callback = $ this ->getFilter ($ filter )) {
97
97
$ value = $ callback (...$ args );
98
- // if $filter is a custom method of the subclass.
98
+ // if $filter is a custom method of the subclass.
99
99
} elseif (method_exists ($ this , $ filter . 'Filter ' )) {
100
100
$ filter .= 'Filter ' ;
101
101
$ value = $ this ->$ filter (...$ args );
102
102
103
- // if $filter is a custom add callback in the property {@see $_filters}.
103
+ // if $filter is a custom add callback in the property {@see $_filters}.
104
104
} elseif ($ callback = UserFilters::get ($ filter )) {
105
105
$ value = $ callback (...$ args );
106
106
107
- // if $filter is a custom add callback in the property {@see $_filters}.
107
+ // if $filter is a custom add callback in the property {@see $_filters}.
108
108
// $filter is a method of the class 'FilterList'
109
109
} elseif (method_exists (Filters::class, $ filterName )) {
110
110
$ value = Filters::$ filterName (...$ args );
111
111
112
- // it is function name
112
+ // it is function name
113
113
} elseif (function_exists ($ filter )) {
114
114
$ value = $ filter (...$ args );
115
115
} else {
@@ -134,28 +134,27 @@ public function getFilter(string $name): ?callable
134
134
}
135
135
136
136
/**
137
- * @param string $name
137
+ * @param string $name
138
138
* @param callable $filter
139
139
*
140
- * @return $this
140
+ * @return static
141
141
*/
142
- public function addFilter (string $ name , callable $ filter ): self
142
+ public function addFilter (string $ name , callable $ filter ): static
143
143
{
144
144
return $ this ->setFilter ($ name , $ filter );
145
145
}
146
146
147
147
/**
148
- * @param string $name
148
+ * @param string $name
149
149
* @param callable $filter
150
150
*
151
- * @return $this
151
+ * @return static
152
152
*/
153
- public function setFilter (string $ name , callable $ filter ): self
153
+ public function setFilter (string $ name , callable $ filter ): static
154
154
{
155
155
if ($ name = trim ($ name )) {
156
156
$ this ->_filters [$ name ] = $ filter ;
157
157
}
158
-
159
158
return $ this ;
160
159
}
161
160
0 commit comments