2525trait FilteringTrait
2626{
2727 /** @var array user custom filters */
28- private $ _filters = [];
28+ private array $ _filters = [];
2929
3030 /**
3131 * value sanitize 直接对给的值进行过滤
3232 *
33- * @param mixed $value
34- * @param string|array $filters
35- * string:
33+ * filters:
34+ * string:
3635 * 'string|trim|upper'
37- * array:
38- * [
36+ * array:
37+ * [
3938 * 'string',
4039 * 'trim',
4140 * ['Class', 'method'],
4241 * // 追加额外参数. 传入时,第一个参数总是要过滤的字段值,其余的依次追加
4342 * 'myFilter' => ['arg1', 'arg2'],
4443 * function($val) {
45- * return str_replace(' ', '', $val);
44+ * return str_replace(' ', '', $val);
4645 * },
47- * ]
46+ * ]
47+ *
48+ * @param mixed $value
49+ * @param array|string|callable $filters
4850 *
4951 * @return mixed
50- * @throws InvalidArgumentException
5152 */
52- protected function valueFiltering ($ value , $ filters )
53+ protected function valueFiltering (mixed $ value , array | string | callable $ filters ): mixed
5354 {
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 )) {
5858 $ filters = [$ filters ];
5959 }
6060
@@ -64,14 +64,14 @@ protected function valueFiltering($value, $filters)
6464 $ args = (array )$ filter ;
6565 $ value = $ this ->callStringCallback ($ key , $ value , ...$ args );
6666
67- // closure
67+ // closure
6868 } elseif (is_object ($ filter ) && method_exists ($ filter , '__invoke ' )) {
6969 $ value = $ filter ($ value );
70- // string, trim, ....
70+ // string, trim, ....
7171 } elseif (is_string ($ filter )) {
7272 $ value = $ this ->callStringCallback ($ filter , $ value );
7373
74- // e.g ['Class', 'method'],
74+ // e.g ['Class', 'method'],
7575 } else {
7676 $ value = Helper::call ($ filter , $ value );
7777 }
@@ -87,29 +87,29 @@ protected function valueFiltering($value, $filters)
8787 * @return mixed
8888 * @throws InvalidArgumentException
8989 */
90- protected function callStringCallback (string $ filter , ...$ args )
90+ protected function callStringCallback (string $ filter , ...$ args ): mixed
9191 {
9292 // if is alias name
9393 $ filterName = Filters::realName ($ filter );
9494
9595 // if $filter is a custom by addFiler()
9696 if ($ callback = $ this ->getFilter ($ filter )) {
9797 $ value = $ callback (...$ args );
98- // if $filter is a custom method of the subclass.
98+ // if $filter is a custom method of the subclass.
9999 } elseif (method_exists ($ this , $ filter . 'Filter ' )) {
100100 $ filter .= 'Filter ' ;
101101 $ value = $ this ->$ filter (...$ args );
102102
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}.
104104 } elseif ($ callback = UserFilters::get ($ filter )) {
105105 $ value = $ callback (...$ args );
106106
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}.
108108 // $filter is a method of the class 'FilterList'
109109 } elseif (method_exists (Filters::class, $ filterName )) {
110110 $ value = Filters::$ filterName (...$ args );
111111
112- // it is function name
112+ // it is function name
113113 } elseif (function_exists ($ filter )) {
114114 $ value = $ filter (...$ args );
115115 } else {
@@ -134,28 +134,27 @@ public function getFilter(string $name): ?callable
134134 }
135135
136136 /**
137- * @param string $name
137+ * @param string $name
138138 * @param callable $filter
139139 *
140- * @return $this
140+ * @return static
141141 */
142- public function addFilter (string $ name , callable $ filter ): self
142+ public function addFilter (string $ name , callable $ filter ): static
143143 {
144144 return $ this ->setFilter ($ name , $ filter );
145145 }
146146
147147 /**
148- * @param string $name
148+ * @param string $name
149149 * @param callable $filter
150150 *
151- * @return $this
151+ * @return static
152152 */
153- public function setFilter (string $ name , callable $ filter ): self
153+ public function setFilter (string $ name , callable $ filter ): static
154154 {
155155 if ($ name = trim ($ name )) {
156156 $ this ->_filters [$ name ] = $ filter ;
157157 }
158-
159158 return $ this ;
160159 }
161160
0 commit comments