Skip to content

Commit 8cbc6dc

Browse files
committed
update, code format
1 parent 390d4c6 commit 8cbc6dc

File tree

2 files changed

+61
-74
lines changed

2 files changed

+61
-74
lines changed

src/StrHelper.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,6 @@ public static function regexVerify($value,$rule)
4141
return preg_match($rule,$value)===1;
4242
}
4343

44-
/**
45-
* 计算字符长度
46-
* @param [type] $str
47-
* @return int|string [type]
48-
*/
49-
public static function length($str)
50-
{
51-
if (empty($str)){
52-
return '0';
53-
}
54-
55-
if ((string)$str === '0'){
56-
return '1';
57-
}
58-
59-
if (function_exists('mb_strlen')){
60-
return mb_strlen($str,'utf-8');
61-
}
62-
63-
preg_match_all('/./u', $str, $arr);
64-
65-
return count($arr[0]);
66-
}
67-
6844
/**
6945
* @param $str
7046
* @return bool|string

src/ValidationTrait.php

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,25 @@ trait ValidationTrait
5353
* [ field2 => errorMessage3 ]
5454
* ]
5555
*/
56-
private $_errors = [];
56+
private $_errors = [];
5757

5858
/**
5959
* the rules is by setRules()
6060
* @var array
6161
*/
62-
private $_rules = [];
62+
private $_rules = [];
6363

6464
/**
6565
* available rules at current scene
6666
* @var array
6767
*/
68-
private $_availableRules = [];
68+
private $_availableRules = [];
6969

7070
/**
7171
* custom append's validator by addValidator()
7272
* @var array
7373
*/
74-
private $_validators = [];
74+
private $_validators = [];
7575

7676
/**
7777
* attribute field translate list
@@ -126,8 +126,13 @@ public function messages()
126126

127127
//////////////////////////////////// Validate ////////////////////////////////////
128128

129-
public function beforeValidate(){}
130-
public function afterValidate(){}
129+
public function beforeValidate()
130+
{
131+
}
132+
133+
public function afterValidate()
134+
{
135+
}
131136

132137
/**
133138
* [ValidatorList::required] 验证是必定被调用的
@@ -166,8 +171,8 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
166171
$skipOnEmpty = $rule['skipOnEmpty'] ?? true;
167172

168173
// 如何判断属性为空 默认使用 empty($data[$attr]). 也可自定义
169-
$isEmpty = [ ValidatorList::class, 'isEmpty'];
170-
if ( isset($rule['isEmpty']) && $rule['isEmpty'] instanceof \Closure ) {
174+
$isEmpty = [ValidatorList::class, 'isEmpty'];
175+
if (isset($rule['isEmpty']) && $rule['isEmpty'] instanceof \Closure) {
171176
$isEmpty = $rule['isEmpty'];
172177
}
173178

@@ -176,7 +181,7 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
176181

177182
// 验证的前置条件 -- 不满足条件,跳过此条规则
178183
$when = $rule['when'] ?? null;
179-
if ( $when && $when instanceof \Closure && $when($data, $this) !== true ) {
184+
if ($when && $when instanceof \Closure && $when($data, $this) !== true) {
180185
continue;
181186
}
182187

@@ -192,18 +197,18 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
192197
// $skipOnEmpty is true && ValidatorList::isEmpty($data,$attr)
193198
if (
194199
($onlyChecked && !in_array($attr, $onlyChecked, true)) ||
195-
( $validator !== 'required' && $skipOnEmpty && call_user_func($isEmpty, $data, $attr))
200+
($validator !== 'required' && $skipOnEmpty && call_user_func($isEmpty, $data, $attr))
196201
) {
197-
continue;
202+
continue;
198203
}
199204

200205
// mark attribute is safe. not need validate. like. 'created_at'
201-
if ( $validator === 'safe' ) {
206+
if ($validator === 'safe') {
202207
$this->_safeData[$attr] = $data[$attr];
203208
continue;
204209
}
205210

206-
[$result,$validator] = $this->doValidate($data, $attr, $validator, $copy);
211+
[$result, $validator] = $this->doValidate($data, $attr, $validator, $copy);
207212

208213
if ($result === false) {
209214
$this->_errors[] = [
@@ -217,13 +222,13 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
217222
$message = null;
218223

219224
// There is an error an immediate end to verify
220-
if ( $this->hasError() && $this->_stopOnError ) {
225+
if ($this->hasError() && $this->_stopOnError) {
221226
break;
222227
}
223228
}
224229

225230
// fix: has error, clear safe data.
226-
if ( $this->hasError() ) {
231+
if ($this->hasError()) {
227232
$this->_safeData = [];
228233
}
229234

@@ -239,18 +244,18 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
239244
* do Validate
240245
* @param array $data 待验证的数据列表
241246
* @param string $attr 属性名称
242-
* @param mixed $validator 验证器
247+
* @param \Closure|string $validator 验证器
243248
* @param array $args 验证需要的参数
244249
* @return array
245250
*/
246251
protected function doValidate($data, $attr, $validator, $args)
247252
{
248253
// if attr don't exists.
249-
if ( !$this->has($attr) ) {
254+
if (!$this->has($attr)) {
250255
return [false, $validator instanceof \Closure ? 'callback' : $validator];
251256
}
252257

253-
if ( $validator === 'required' ) {
258+
if ($validator === 'required') {
254259
$result = ValidatorList::required($data, $attr);
255260

256261
return [$result, $validator];
@@ -260,26 +265,26 @@ protected function doValidate($data, $attr, $validator, $args)
260265
array_unshift($args, $data[$attr]);
261266

262267
// if $validator is a closure
263-
if ( is_callable($validator) && $validator instanceof \Closure) {
264-
$callback = $validator;
268+
if (is_callable($validator) && $validator instanceof \Closure) {
269+
$callback = $validator;
265270
$validator = 'callback';
266271
$args[] = $data;
267272

268-
} elseif ( is_string($validator) ) {
273+
} elseif (is_string($validator)) {
269274

270275
// if $validator is a custom add callback in the property {@see $_validators}.
271-
if ( isset($this->_validators[$validator]) ) {
276+
if (isset($this->_validators[$validator])) {
272277
$callback = $this->_validators[$validator];
273278

274-
// if $validator is a custom method of the subclass.
275-
} elseif ( is_string($validator) && method_exists($this, $validator) ) {
279+
// if $validator is a custom method of the subclass.
280+
} elseif (is_string($validator) && method_exists($this, $validator)) {
276281

277282
$callback = [$this, $validator];
278283

279-
// $validator is a method of the class 'ValidatorList'
280-
} elseif ( is_string($validator) && is_callable([ValidatorList::class, $validator]) ) {
284+
// $validator is a method of the class 'ValidatorList'
285+
} elseif (is_string($validator) && is_callable([ValidatorList::class, $validator])) {
281286

282-
$callback = [ ValidatorList::class, $validator];
287+
$callback = [ValidatorList::class, $validator];
283288
} else {
284289
throw new \InvalidArgumentException("The validator [$validator] don't exists!");
285290
}
@@ -300,7 +305,7 @@ protected function resetRuntimeData($clearErrors = false)
300305
{
301306
$this->_safeData = $this->_availableRules = [];
302307

303-
if ( $clearErrors ) {
308+
if ($clearErrors) {
304309
$this->clearErrors();
305310
}
306311

@@ -350,18 +355,18 @@ protected function collectRules()
350355
}
351356

352357
// check validator
353-
if ( !is_string($rule[1]) && !($rule[1] instanceof \Closure) ) {
358+
if (!is_string($rule[1]) && !($rule[1] instanceof \Closure)) {
354359
throw new \InvalidArgumentException('The rule validator rule must be is a validator name or a Closure! position: rule[1].');
355360
}
356361

357362
// global rule.
358363
if (empty($rule['on'])) {
359364
$this->_availableRules[] = $rule;
360365

361-
// only use to special scene.
366+
// only use to special scene.
362367
} else {
363368
$ruleScene = $rule['on'];
364-
$ruleScene = is_string($ruleScene) ? array_map('trim',explode(',', $ruleScene)) : (array)$ruleScene;
369+
$ruleScene = is_string($ruleScene) ? array_map('trim', explode(',', $ruleScene)) : (array)$ruleScene;
365370

366371
if (in_array($scene, $ruleScene, true)) {
367372
unset($rule['on']);
@@ -389,17 +394,24 @@ public function clearErrors()
389394

390395
/**
391396
* 是否有错误
392-
* @date 2015-09-27
393397
* @return boolean
394398
*/
395399
public function hasError(): bool
396400
{
397401
return count($this->_errors) > 0;
398402
}
403+
404+
/**
405+
* @return bool
406+
*/
399407
public function isFail(): bool
400408
{
401409
return $this->hasError();
402410
}
411+
412+
/**
413+
* @return bool
414+
*/
403415
public function fail(): bool
404416
{
405417
return $this->hasError();
@@ -425,13 +437,12 @@ public function getErrors(): array
425437
/**
426438
* 得到第一个错误信息
427439
* @author inhere
428-
* @date 2015-09-27
429440
* @param bool $onlyMsg
430441
* @return array|string
431442
*/
432443
public function firstError($onlyMsg = true)
433444
{
434-
$e = $this->_errors;
445+
$e = $this->_errors;
435446
$first = array_shift($e);
436447

437448
return $onlyMsg ? array_values($first)[0] : $first;
@@ -440,13 +451,12 @@ public function firstError($onlyMsg = true)
440451
/**
441452
* 得到最后一个错误信息
442453
* @author inhere
443-
* @date 2015-09-27
444454
* @param bool $onlyMsg
445455
* @return array|string
446456
*/
447457
public function lastError($onlyMsg = true)
448458
{
449-
$e = $this->_errors;
459+
$e = $this->_errors;
450460
$last = array_pop($e);
451461

452462
return $onlyMsg ? array_values($last)[0] : $last;
@@ -457,25 +467,25 @@ public function lastError($onlyMsg = true)
457467
* @var array
458468
*/
459469
private static $_defaultMessages = [
460-
'int' => '{attr} must be an integer!',
470+
'int' => '{attr} must be an integer!',
461471
'number' => '{attr} must be an integer greater than 0!',
462-
'bool' => '{attr} must be is boolean!',
463-
'float' => '{attr} must be is float!',
472+
'bool' => '{attr} must be is boolean!',
473+
'float' => '{attr} must be is float!',
464474
'regexp' => '{attr} does not meet the conditions',
465-
'url' => '{attr} not is url address!',
466-
'email' => '{attr} not is email address!',
467-
'ip' => '{attr} not is ip address!',
475+
'url' => '{attr} not is url address!',
476+
'email' => '{attr} not is email address!',
477+
'ip' => '{attr} not is ip address!',
468478
'required' => 'parameter {attr} is required!',
469479
'length' => '{attr} length must at rang {min} ~ {max}',
470-
'size' => '{attr} must be an integer and at rang {min} ~ {max}',
471-
'min' => '{attr} minimum boundary is {value}',
472-
'max' => '{attr} maximum boundary is {value}',
473-
'in' => '{attr} must in ({value})',
480+
'size' => '{attr} must be an integer and at rang {min} ~ {max}',
481+
'min' => '{attr} minimum boundary is {value}',
482+
'max' => '{attr} maximum boundary is {value}',
483+
'in' => '{attr} must in ({value})',
474484
'string' => '{attr} must be a string',
475485
'compare' => '{attr} must be equals to {attr0}',
476486
'isArray' => '{attr} must be an array',
477487
'callback' => '{attr} don\'t pass the test and verify!',
478-
'_' => '{attr} validation is not through!',
488+
'_' => '{attr} validation is not through!',
479489
];
480490

481491
/**
@@ -504,7 +514,7 @@ public function getMessages(): array
504514
* @param string $msg 自定义提示消息
505515
* @return string
506516
*/
507-
public function getMessage($name, array $params, array $rule = [], $msg=null)
517+
public function getMessage($name, array $params, array $rule = [], $msg = null)
508518
{
509519
if (!$msg) {
510520
$msgList = $this->getMessages();
@@ -675,8 +685,8 @@ public function has(string $key): bool
675685

676686
/**
677687
* Get data item for key
678-
* @param string $key The data key
679-
* @param mixed $default The default value to return if data key does not exist
688+
* @param string $key The data key
689+
* @param mixed $default The default value to return if data key does not exist
680690
* @return mixed The key's value, or the default value
681691
*/
682692
public function get($key, $default = null)
@@ -694,6 +704,7 @@ public function getSafe(string $key, $default = null)
694704
{
695705
return $this->getValid($key, $default);
696706
}
707+
697708
public function getValid(string $key, $default = null)
698709
{
699710
return array_key_exists($key, $this->_safeData) ? $this->_safeData[$key] : $default;

0 commit comments

Comments
 (0)