Skip to content

Commit 748f4ef

Browse files
committed
update add more tests
1 parent 753ec38 commit 748f4ef

File tree

14 files changed

+272
-168
lines changed

14 files changed

+272
-168
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/Filter/FilteringTrait.php

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Inhere\Validate\Filter;
1010

11+
use Inhere\Validate\Helper;
12+
1113
/**
1214
* Trait FilteringTrait
1315
* @package Inhere\Validate\Filter
@@ -17,25 +19,6 @@ trait FilteringTrait
1719
/** @var array user custom filters */
1820
private $_filters = [];
1921

20-
/** @var array filter aliases map */
21-
private static $filterAliases = [
22-
'substr' => 'subStr',
23-
'substring' => 'subStr',
24-
'str2list' => 'explode',
25-
'str2array' => 'explode',
26-
'string2list' => 'explode',
27-
'string2array' => 'explode',
28-
'toUpper' => 'uppercase',
29-
'str2upper' => 'uppercase',
30-
'strToUpper' => 'uppercase',
31-
'toLower' => 'lowercase',
32-
'str2lower' => 'lowercase',
33-
'strToLower' => 'lowercase',
34-
'clearNl' => 'clearNewline',
35-
'str2time' => 'strToTime',
36-
'strtotime' => 'strToTime',
37-
];
38-
3922
/**
4023
* value sanitize 直接对给的值进行过滤
4124
* @param mixed $value
@@ -91,7 +74,7 @@ protected function valueFiltering($value, $filters)
9174
protected function callStringCallback(string $filter, ...$args)
9275
{
9376
// if is alias name
94-
$filterName = isset(self::$filterAliases[$filter]) ? self::$filterAliases[$filter] : $filter;
77+
$filterName = self::$filterAliases[$filter] ?? $filter;
9578

9679
// if $filter is a custom by addFiler()
9780
if ($callback = $this->getFilter($filter)) {
@@ -130,11 +113,7 @@ protected function callStringCallback(string $filter, ...$args)
130113
*/
131114
public function getFilter(string $name)
132115
{
133-
if (isset($this->_filters[$name])) {
134-
return $this->_filters[$name];
135-
}
136-
137-
return null;
116+
return $this->_filters[$name] ?? null;
138117
}
139118

140119
/**
@@ -161,19 +140,16 @@ public function setFilter(string $name, callable $filter)
161140

162141
/**
163142
* @param string $name
164-
* @return $this
165143
*/
166-
public function delFilter(string $name): self
144+
public function delFilter(string $name)
167145
{
168146
if (isset($this->_filters[$name])) {
169147
unset($this->_filters[$name]);
170148
}
171-
172-
return $this;
173149
}
174150

175151
/**
176-
* clear Filters
152+
* clear filters
177153
*/
178154
public function clearFilters()
179155
{
@@ -188,6 +164,14 @@ public function getFilters(): array
188164
return $this->_filters;
189165
}
190166

167+
/**
168+
* @param array $filters
169+
*/
170+
public function addFilters(array $filters)
171+
{
172+
$this->_filters = \array_merge($this->_filters, $filters);
173+
}
174+
191175
/**
192176
* @param array $filters
193177
*/

src/Filter/Filters.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,34 @@
77
namespace Inhere\Validate\Filter;
88

99
use Inhere\Validate\Helper;
10+
use Inhere\Validate\Traits\NameAliasTrait;
1011

1112
/**
1213
* Class Filters
1314
* @package Inhere\Validate\Filter
1415
*/
1516
final class Filters
1617
{
17-
// TODO manage aliases
18+
use NameAliasTrait;
19+
20+
/** @var array filter aliases map */
21+
private static $aliases = [
22+
'substr' => 'subStr',
23+
'substring' => 'subStr',
24+
'str2list' => 'explode',
25+
'str2array' => 'explode',
26+
'string2list' => 'explode',
27+
'string2array' => 'explode',
28+
'toUpper' => 'uppercase',
29+
'str2upper' => 'uppercase',
30+
'strToUpper' => 'uppercase',
31+
'toLower' => 'lowercase',
32+
'str2lower' => 'lowercase',
33+
'strToLower' => 'lowercase',
34+
'clearNl' => 'clearNewline',
35+
'str2time' => 'strToTime',
36+
'strtotime' => 'strToTime',
37+
];
1838

1939
/**
2040
* don't allow create instance.
@@ -88,9 +108,9 @@ public static function abs($val): int
88108
/**
89109
* 过滤器删除浮点数中所有非法的字符。
90110
* @note 该过滤器默认允许所有数字以及 + -
91-
* @param mixed $val 要过滤的变量
111+
* @param mixed $val 要过滤的变量
92112
* @param null|int $decimal
93-
* @param int $flags 标志
113+
* @param int $flags 标志
94114
* FILTER_FLAG_ALLOW_FRACTION - 允许小数分隔符 (比如 .)
95115
* FILTER_FLAG_ALLOW_THOUSAND - 允许千位分隔符(比如 ,)
96116
* FILTER_FLAG_ALLOW_SCIENTIFIC - 允许科学记数法(比如 e 和 E)
@@ -108,7 +128,7 @@ public static function float($val, $decimal = null, $flags = \FILTER_FLAG_ALLOW_
108128
$new = \strpos($ret, '.') ? (float)$ret : $ret;
109129

110130
if (\is_int($decimal)) {
111-
return round($new, $decimal);
131+
return \round($new, $decimal);
112132
}
113133

114134
return $new;
@@ -263,7 +283,7 @@ public static function ucwords($str): string
263283
return \mb_convert_case($str, \MB_CASE_TITLE);
264284
}
265285

266-
return \ucwords(Filters::lowercase($str));
286+
return \ucwords(self::lowercase($str));
267287
}
268288

269289
/**
@@ -285,7 +305,7 @@ public static function snake($val, $sep = '_'): string
285305
* @param string $sep
286306
* @return string
287307
*/
288-
public static function snakeCase($val, $sep = '_'): string
308+
public static function snakeCase($val, string $sep = '_'): string
289309
{
290310
if (!$val || !\is_string($val)) {
291311
return '';
@@ -442,7 +462,7 @@ public static function stripTags($val, $allowedTags = null): string
442462
* FILTER_FLAG_ENCODE_HIGH - 编码 ASCII 值在 32 以上的字符
443463
* @return string
444464
*/
445-
public static function encoded($val, $flags = 0): string
465+
public static function encoded(string $val, int $flags = 0): string
446466
{
447467
$settings = [];
448468

@@ -458,7 +478,7 @@ public static function encoded($val, $flags = 0): string
458478
* @param string $val
459479
* @return string
460480
*/
461-
public static function quotes($val): string
481+
public static function quotes(string $val): string
462482
{
463483
return (string)\filter_var($val, \FILTER_SANITIZE_MAGIC_QUOTES);
464484
}

src/Filter/UserFilters.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ final class UserFilters
2525
*/
2626
public static function get(string $name)
2727
{
28-
if (isset(self::$filters[$name])) {
29-
return self::$filters[$name];
30-
}
31-
32-
return null;
28+
return self::$filters[$name] ?? null;
3329
}
3430

3531
/**
@@ -49,7 +45,9 @@ public static function add(string $name, callable $filter)
4945
*/
5046
public static function set(string $name, callable $filter)
5147
{
52-
self::$filters[$name] = $filter;
48+
if ($name) {
49+
self::$filters[$name] = $filter;
50+
}
5351
}
5452

5553
/**
@@ -69,20 +67,14 @@ public static function getFilters(): array
6967
return self::$filters;
7068
}
7169

72-
/**
73-
* @param array $filters
74-
*/
75-
public static function addFilters(array $filters)
76-
{
77-
self::$filters = \array_merge(self::$filters, $filters);
78-
}
79-
8070
/**
8171
* @param array $filters
8272
*/
8373
public static function setFilters(array $filters)
8474
{
85-
self::$filters = $filters;
75+
foreach ($filters as $name => $filter) {
76+
self::set($name, $filter);
77+
}
8678
}
8779

8880
/**

src/Traits/ErrorMessageTrait.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ trait ErrorMessageTrait
5555
*/
5656
private $_prettifyName = true;
5757

58+
protected function prepareValidation()
59+
{
60+
// error message
61+
$this->_messages = \array_merge($this->messages(), $this->_messages);
62+
// field translate
63+
$this->_translates = \array_merge($this->translates(), $this->_translates);
64+
}
65+
5866
/*******************************************************************************
5967
* Errors Information
6068
******************************************************************************/
@@ -145,7 +153,6 @@ public function inError(string $field): bool
145153
return true;
146154
}
147155
}
148-
149156
return false;
150157
}
151158

@@ -182,12 +189,11 @@ public function getErrors(string $field = null): array
182189
}
183190

184191
/**
185-
* @return $this
192+
* clear errors
186193
*/
187-
public function clearErrors(): self
194+
public function clearErrors()
188195
{
189196
$this->_errors = [];
190-
return $this;
191197
}
192198

193199
/**
@@ -196,11 +202,10 @@ public function clearErrors(): self
196202
* @param bool $onlyMsg
197203
* @return array|string
198204
*/
199-
public function firstError($onlyMsg = true)
205+
public function firstError(bool $onlyMsg = true)
200206
{
201207
$errors = $this->_errors;
202208
$first = \array_shift($errors);
203-
204209
return $onlyMsg ? $first['msg'] : $first;
205210
}
206211

@@ -209,11 +214,10 @@ public function firstError($onlyMsg = true)
209214
* @param bool $onlyMsg
210215
* @return array|string
211216
*/
212-
public function lastError($onlyMsg = true)
217+
public function lastError(bool $onlyMsg = true)
213218
{
214-
$e = $this->_errors;
215-
$last = \array_pop($e);
216-
219+
$ers = $this->_errors;
220+
$last = \array_pop($ers);
217221
return $onlyMsg ? $last['msg'] : $last;
218222
}
219223

@@ -238,13 +242,6 @@ public function isStopOnError(): bool
238242
return $this->_stopOnError;
239243
}
240244

241-
protected function prepareValidation()
242-
{
243-
$this->_translates = \array_merge($this->translates(), $this->_translates);
244-
// error message
245-
$this->_messages = \array_merge($this->messages(), $this->_messages);
246-
}
247-
248245
/*******************************************************************************
249246
* Error Messages
250247
******************************************************************************/
@@ -277,7 +274,6 @@ public function setMessages(array $messages): self
277274
foreach ($messages as $key => $value) {
278275
$this->setMessage($key, $value);
279276
}
280-
281277
return $this;
282278
}
283279

@@ -339,7 +335,7 @@ protected function findMessage(string $field, string $rawName)
339335
// allow define a message for a validator.
340336
// eg: 'username.required' => 'some message ...'
341337
$fullKey = $field . '.' . $rawName;
342-
$realName = Validators::getRealName($rawName);
338+
$realName = Validators::realName($rawName);
343339

344340
if (isset($this->_messages[$fullKey])) {
345341
$message = $this->_messages[$fullKey];
@@ -362,8 +358,7 @@ protected function findMessage(string $field, string $rawName)
362358
*/
363359
public function setTranslates(array $fieldTrans): self
364360
{
365-
$this->_translates = $fieldTrans;
366-
return $this;
361+
return $this->addTranslates($fieldTrans);
367362
}
368363

369364
/**
@@ -373,7 +368,9 @@ public function setTranslates(array $fieldTrans): self
373368
*/
374369
public function addTranslates(array $fieldTrans): self
375370
{
376-
$this->_translates = \array_merge($this->_translates, $fieldTrans);
371+
foreach ($fieldTrans as $field => $tran) {
372+
$this->_translates[$field] = $tran;
373+
}
377374
return $this;
378375
}
379376

@@ -405,6 +402,14 @@ public function getTranslate(string $field): string
405402
return $field;
406403
}
407404

405+
/**
406+
* @return array
407+
*/
408+
public function clearTranslates(): array
409+
{
410+
return $this->_translates = [];
411+
}
412+
408413
/**
409414
* @return bool
410415
*/

0 commit comments

Comments
 (0)