Skip to content

Commit c027d64

Browse files
committed
some update
1 parent f292c6c commit c027d64

File tree

5 files changed

+65
-55
lines changed

5 files changed

+65
-55
lines changed

src/StrHelper.php

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public static function length($str)
6868

6969
if (function_exists('mb_strlen')){
7070
return mb_strlen($str,'utf-8');
71-
} else {
72-
preg_match_all("/./u", $str, $arr);
73-
74-
return count($arr[0]);
7571
}
72+
73+
preg_match_all('/./u', $str, $arr);
74+
75+
return count($arr[0]);
7676
}
7777

7878
/**
@@ -90,12 +90,14 @@ public static function abs_length($str)
9090

9191
if (function_exists('mb_strwidth')){
9292
return mb_strwidth($str,'utf-8');
93-
} else if (function_exists('mb_strlen')){
93+
}
94+
95+
if (function_exists('mb_strlen')){
9496
return mb_strlen($str,'utf-8');
95-
} else {
96-
preg_match_all("/./u", $str, $ar);
97-
return count($ar[0]);
9897
}
98+
99+
preg_match_all('/./u', $str, $ar);
100+
return count($ar[0]);
99101
}
100102

101103
/**
@@ -117,24 +119,24 @@ public static function utf8_substr($str,$start=0,$end=null)
117119
$end = func_get_arg(2);
118120

119121
return mb_substr($str,$start,$end,'utf-8');
120-
} else {
121-
mb_internal_encoding("UTF-8");
122-
123-
return mb_substr($str,$start);
124122
}
125123

126-
} else {
127-
$null = "";
128-
preg_match_all("/./u", $str, $ar);
124+
mb_internal_encoding('UTF-8');
129125

130-
if (func_num_args() >= 3) {
131-
$end = func_get_arg(2);
126+
return mb_substr($str,$start);
132127

133-
return implode($null, array_slice($ar[0],$start,$end));
134-
} else {
135-
return implode($null, array_slice($ar[0],$start));
136-
}
137128
}
129+
130+
$null = '';
131+
preg_match_all('/./u', $str, $ar);
132+
133+
if (func_num_args() >= 3) {
134+
$end = func_get_arg(2);
135+
136+
return implode($null, array_slice($ar[0],$start,$end));
137+
}
138+
139+
return implode($null, array_slice($ar[0],$start));
138140
}
139141

140142

@@ -148,9 +150,9 @@ public static function utf8_substr($str,$start=0,$end=null)
148150
* @param bool $suffix 是否加尾缀
149151
* @return string
150152
*/
151-
public static function zh_substr($str, $start=0, $length, $charset="utf-8", $suffix=true)
153+
public static function zh_substr($str, $start=0, $length, $charset = 'utf-8', $suffix=true)
152154
{
153-
if (function_exists("mb_substr"))
155+
if (function_exists('mb_substr'))
154156
{
155157
if (mb_strlen($str, $charset) <= $length) {
156158
return $str;
@@ -168,10 +170,10 @@ public static function zh_substr($str, $start=0, $length, $charset="utf-8", $suf
168170
return $str;
169171
}
170172

171-
$slice = implode("",array_slice($match[0], $start, $length));
173+
$slice = implode('',array_slice($match[0], $start, $length));
172174
}
173175

174-
return (bool)$suffix ? $slice."" : $slice;
176+
return (bool)$suffix ? $slice. '' : $slice;
175177
}
176178

177179
/**
@@ -192,16 +194,12 @@ public static function strtolower($str)
192194
}
193195

194196
/**
195-
* @param $str
197+
* @param string $str
196198
* @param string $encoding
197-
* @return bool|int
199+
* @return int
198200
*/
199201
public static function strlen($str, $encoding = 'UTF-8')
200202
{
201-
if (is_array($str)) {
202-
return false;
203-
}
204-
205203
$str = html_entity_decode($str, ENT_COMPAT, 'UTF-8');
206204
if (function_exists('mb_strlen')) {
207205
return mb_strlen($str, $encoding);

src/StrainerList.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public static function inputMulti($type, array $filters=[])
3030
*/
3131
public static function trim($var)
3232
{
33-
return is_array($var) ? array_walk_recursive(function(&$val){
34-
$value = trim((string)$val);
33+
return is_array($var) ? array_walk_recursive(function($val)
34+
{
35+
return trim((string)$val);
3536
}, $var) : trim((string)$var);
3637
}
3738

@@ -245,4 +246,4 @@ public static function callback($var, $callback)
245246
return filter_var($var, FILTER_CALLBACK,['options' => $callback]);
246247
}
247248

248-
}
249+
}

src/Validation.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ public static function make(array $data, array $rules=[], array $attrTrans = [],
5454
return new static($data, $rules, $attrTrans, $scene, $startValidate);
5555
}
5656

57+
/**
58+
* @param $key
59+
* @param null $value
60+
* @return mixed
61+
*/
5762
public function get($key, $value=null)
5863
{
59-
return self::traitGet($key, $value);
64+
return $this->traitGet($key, $value);
6065
}
6166

62-
}
67+
}

src/ValidationTrait.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,14 @@ public function validate(array $onlyChecked = [], $hasErrorStop=false)
200200
foreach ($attrs as $attr) {
201201
// 不在需要检查的列表内 || $skipOnEmpty is true && ValidatorList::isEmpty($data,$attr)
202202
if (
203-
($onlyChecked && !in_array($attr, $onlyChecked)) ||
203+
($onlyChecked && !in_array($attr, $onlyChecked, true)) ||
204204
( $validator !== 'required' && $skipOnEmpty && call_user_func($isEmpty, $data, $attr))
205205
) {
206206
continue;
207+
}
207208

208209
// mark attribute is safe. not need validate. like. 'created_at'
209-
} elseif ( $validator === 'safe' ) {
210+
if ( $validator === 'safe' ) {
210211
$this->_safeData[$attr] = $data[$attr];
211212
continue;
212213
}
@@ -292,7 +293,7 @@ protected function doValidate($data, $attr, $validator, $args)
292293
throw new \InvalidArgumentException("validator [$validator] don't exists!");
293294
}
294295
} else {
295-
throw new \InvalidArgumentException("validator format is error, must is String or Closure !");
296+
throw new \InvalidArgumentException('Validator format is error, must is String or Closure !');
296297
}
297298

298299
$result = call_user_func_array($callback, $args);
@@ -358,7 +359,7 @@ protected function collectRules()
358359

359360
// check validator
360361
if ( !is_string($rule[1]) && !($rule[1] instanceof \Closure) ) {
361-
throw new \InvalidArgumentException("validator setting error!");
362+
throw new \InvalidArgumentException('Validator rule setting error!');
362363
}
363364

364365
if ( empty($rule['on']) ) {
@@ -367,7 +368,7 @@ protected function collectRules()
367368
$ruleScene = $rule['on'];
368369
$ruleScene = is_string($ruleScene) ? array_map('trim',explode(',', $ruleScene)) : (array)$ruleScene;
369370

370-
if ( in_array($scene,$ruleScene) ) {
371+
if (in_array($scene, $ruleScene, true)) {
371372
unset($rule['on']);
372373
$availableRules[] = $rule;
373374
}
@@ -414,7 +415,6 @@ public function fail()
414415
/**
415416
* @param $attr
416417
* @param $msg
417-
* @return mixed
418418
*/
419419
public function addError($attr, $msg)
420420
{
@@ -433,7 +433,7 @@ public function getErrors()
433433
* @param bool $onlyMsg
434434
* @return array|string
435435
*/
436-
public function firstError($onlyMsg=true)
436+
public function firstError($onlyMsg = true)
437437
{
438438
$e = $this->_errors;
439439
$first = array_shift($e);
@@ -448,7 +448,7 @@ public function firstError($onlyMsg=true)
448448
* @param bool $onlyMsg
449449
* @return array|string
450450
*/
451-
public function lastError($onlyMsg=true)
451+
public function lastError($onlyMsg = true)
452452
{
453453
$e = $this->_errors;
454454
$last = array_pop($e);
@@ -469,18 +469,22 @@ public function lastError($onlyMsg=true)
469469
'url' => '{attr} not is url address!',
470470
'email' => '{attr} not is email address!',
471471
'ip' => '{attr} not is ip address!',
472-
'required' => '{attr} is not block!',
472+
'required' => 'parameter {attr} is required!',
473473
'length' => '{attr} length must at rang {min} ~ {max}',
474474
'size' => '{attr} must be an integer and at rang {min} ~ {max}',
475475
'min' => '{attr} minimum boundary is {value}',
476476
'max' => '{attr} maximum boundary is {value}',
477477
'in' => '{attr} must in ({value})',
478478
'string' => '{attr} must be a string',
479+
'compare' => '{attr} must be equals to {compare}',
479480
'isArray' => '{attr} must be an array',
480481
'callback' => 'The custom callback validation fails of the [{attr}]!',
481482
'_' => '{attr} validation is not through!',
482483
];
483484

485+
/**
486+
* @return array
487+
*/
484488
public function getMessages()
485489
{
486490
return array_merge(self::$_defaultMessages, $this->messages());
@@ -496,7 +500,7 @@ public function getMessages()
496500
* @param string $msg 提示消息
497501
* @return string
498502
*/
499-
public function getMessage($name, array $params, $rule = [], $msg=null)
503+
public function getMessage($name, array $params, array $rule = [], $msg=null)
500504
{
501505
if ( !$msg ) {
502506
$msgList = $this->getMessages();

src/ValidatorList.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ public static function number($int, array $options=[], $flags=0)
8888
}
8989

9090
/**
91-
* @param mixed $var
91+
* @param mixed $var
92+
* @param int $minLength
93+
* @param null|int $maxLength
9294
* @return mixed
9395
*/
9496
public static function string($var, $minLength=0, $maxLength=null)
@@ -188,7 +190,7 @@ public static function length($var, $minLength=0, $maxLength=null)
188190
* @param int $flags 标志 FILTER_NULL_ON_FAILURE
189191
* @return mixed
190192
*/
191-
public static function boolean($var, $default=null, $flags=0)
193+
public static function boolean($var, $default = null, $flags=0)
192194
{
193195
$settings = [];
194196

@@ -348,7 +350,7 @@ public static function isArray($var)
348350
*/
349351
public static function in($var, array $range)
350352
{
351-
return in_array($var, $range) ? $var : false;
353+
return in_array($var, $range, true) ? $var : false;
352354
}
353355

354356
/**
@@ -361,15 +363,15 @@ public static function compare($var, $compareVar)
361363
return $var === $compareVar;
362364
}
363365

364-
365366
/////////////////////////////// extension validator ///////////////////////////////
366367

368+
/**
369+
* @param $value
370+
* @return bool
371+
*/
367372
public static function phone($value)
368373
{
369-
return 1 === preg_match(
370-
'/^(?:(?:1(?:3[4-9]|5[012789]|8[78])\d{8}|1(?:3[0-2]|5[56]|8[56])\d{8}|18[0-9]\d{8}|1[35]3\d{8})|14[57]\d{8}|170[059]\d{7}|17\d{9})$/',
371-
$value
372-
);
374+
return 1 === preg_match('/^1[2-9]\d{9}$/', $value);
373375
}
374376

375377
public static function telNumber($value)
@@ -385,7 +387,7 @@ public static function telNumber($value)
385387
*/
386388
public static function postCode($postcode)
387389
{
388-
return empty($postcode) || preg_match('/^[0-9]{6}$/', $postcode);
390+
return empty($postcode) || preg_match('/^\d{6}$/', $postcode);
389391
}
390392

391393
/**

0 commit comments

Comments
 (0)