Skip to content

Commit c3a059e

Browse files
committed
code format for php7
1 parent a714386 commit c3a059e

10 files changed

+82
-63
lines changed

src/AbstractValidation.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ abstract class AbstractValidation implements ValidationInterface
3737
* @param array $translates
3838
* @param string $scene
3939
* @param bool $startValidate 立即开始验证
40+
* @throws \RuntimeException
4041
*/
4142
public function __construct(array $data = [], array $rules = [], array $translates = [], $scene = '', $startValidate = false)
4243
{
@@ -59,6 +60,7 @@ public function __construct(array $data = [], array $rules = [], array $translat
5960
* @param string $scene
6061
* @param bool $startValidate 立即开始验证
6162
* @return static
63+
* @throws \RuntimeException
6264
*/
6365
public static function make(array $data, array $rules = [], array $translates = [], $scene = '', $startValidate = false)
6466
{

src/Factory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ final class Factory
1919

2020
public static function make(
2121
array $data = [], array $rules = [], array $translates = [],
22-
$type = self::FIELDS, $startValidate = false
22+
$type = self::RULES, $scene = '', $startValidate = false
2323
)
2424
{
25+
if ($type === self::FIELDS) {
26+
return FieldValidation::make($data, $rules, $translates, $scene, $startValidate);
27+
}
2528

29+
return Validation::make($data, $rules, $translates, $scene, $startValidate);
2630
}
2731
}

src/FieldValidation.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function rules()
3232
];
3333
}
3434

35+
/**
36+
* @return \Generator
37+
* @throws \InvalidArgumentException
38+
*/
3539
protected function collectRules()
3640
{
3741
$scene = $this->scene;
@@ -50,9 +54,9 @@ protected function collectRules()
5054

5155
// an rule for special sence.
5256
if (!empty($rule['on'])) {
53-
$sceneList = is_string($rule['on']) ? array_map('trim', explode(',', $rule['on'])) : (array)$rule['on'];
57+
$sceneList = \is_string($rule['on']) ? array_map('trim', explode(',', $rule['on'])) : (array)$rule['on'];
5458

55-
if ($scene && !in_array($scene, $sceneList, true)) {
59+
if ($scene && !\in_array($scene, $sceneList, true)) {
5660
continue;
5761
}
5862

@@ -62,22 +66,27 @@ protected function collectRules()
6266

6367
$field = trim(array_shift($rule));
6468

65-
if (is_object($rule[0])) {
66-
yield [$field] => $rule;
69+
if (\is_object($rule[0])) {
70+
yield $field => $rule;
6771
} else {
6872
// 'required|string:5,10;' OR 'required|in:5,10'
69-
$rules = is_array($rule[0]) ? $rule[0] : array_map('trim', explode('|', $rule[0]));
73+
$rules = \is_array($rule[0]) ? $rule[0] : array_map('trim', explode('|', $rule[0]));
7074

7175
foreach ($rules as $aRule) {
7276
$rule = $this->parseRule($aRule, $rule);
7377

74-
yield [$field] => $rule;
78+
yield $field => $rule;
7579
}
7680
}
7781

7882
}
7983
}
8084

85+
/**
86+
* @param string $rule
87+
* @param array $row
88+
* @return array
89+
*/
8190
protected function parseRule($rule, $row)
8291
{
8392
$rule = trim($rule, ': ');

src/Utils/ErrorInformationTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function hasError(): bool
6161
*/
6262
public function isFail(): bool
6363
{
64-
return count($this->_errors) > 0;
64+
return \count($this->_errors) > 0;
6565
}
6666

6767
/**

src/Utils/ErrorMessageTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function setMessages(array $messages)
138138
*/
139139
public function getMessage($validator, $field, array $args = [], $message = null)
140140
{
141-
$validator = is_string($validator) ? $validator : 'callback';
141+
$validator = \is_string($validator) ? $validator : 'callback';
142142

143143
// get message from default dict.
144144
if (!$message) {
@@ -158,13 +158,13 @@ public function getMessage($validator, $field, array $args = [], $message = null
158158
];
159159

160160
foreach ($args as $key => $value) {
161-
$key = is_int($key) ? "value{$key}" : $key;
162-
$params['{' . $key . '}'] = is_array($value) ? implode(',', $value) : $value;
161+
$key = \is_int($key) ? "value{$key}" : $key;
162+
$params['{' . $key . '}'] = \is_array($value) ? implode(',', $value) : $value;
163163
}
164164

165165
// @see self::$messages['size']
166-
if (is_array($message)) {
167-
$msgKey = count($params) - 1;
166+
if (\is_array($message)) {
167+
$msgKey = \count($params) - 1;
168168
$message = $message[$msgKey] ?? $message[0];
169169
}
170170

src/Utils/Helper.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class Helper
1717
*/
1818
public static function strtolower($str)
1919
{
20-
if (is_array($str)) {
20+
if (\is_array($str)) {
2121
return false;
2222
}
2323

24-
if (function_exists('mb_strtolower')) {
24+
if (\function_exists('mb_strtolower')) {
2525
return mb_strtolower($str, 'utf-8');
2626
}
2727

@@ -37,11 +37,11 @@ public static function strlen($str, $encoding = 'UTF-8')
3737
{
3838
$str = html_entity_decode($str, ENT_COMPAT, 'UTF-8');
3939

40-
if (function_exists('mb_strlen')) {
40+
if (\function_exists('mb_strlen')) {
4141
return mb_strlen($str, $encoding);
4242
}
4343

44-
return strlen($str);
44+
return \strlen($str);
4545
}
4646

4747
/**
@@ -50,11 +50,11 @@ public static function strlen($str, $encoding = 'UTF-8')
5050
*/
5151
public static function strtoupper($str)
5252
{
53-
if (is_array($str)) {
53+
if (\is_array($str)) {
5454
return false;
5555
}
5656

57-
if (function_exists('mb_strtoupper')) {
57+
if (\function_exists('mb_strtoupper')) {
5858
return mb_strtoupper($str, 'utf-8');
5959
}
6060

@@ -70,11 +70,11 @@ public static function strtoupper($str)
7070
*/
7171
public static function substr($str, $start, $length = false, $encoding = 'utf-8')
7272
{
73-
if (is_array($str)) {
73+
if (\is_array($str)) {
7474
return false;
7575
}
7676

77-
if (function_exists('mb_substr')) {
77+
if (\function_exists('mb_substr')) {
7878
return mb_substr($str, (int)$start, ($length === false ? self::strlen($str) : (int)$length), $encoding);
7979
}
8080

@@ -90,7 +90,7 @@ public static function substr($str, $start, $length = false, $encoding = 'utf-8'
9090
*/
9191
public static function strpos($str, $find, $offset = 0, $encoding = 'UTF-8')
9292
{
93-
if (function_exists('mb_strpos')) {
93+
if (\function_exists('mb_strpos')) {
9494
return mb_strpos($str, $find, $offset, $encoding);
9595
}
9696

@@ -106,7 +106,7 @@ public static function strpos($str, $find, $offset = 0, $encoding = 'UTF-8')
106106
*/
107107
public static function strrpos($str, $find, $offset = 0, $encoding = 'utf-8')
108108
{
109-
if (function_exists('mb_strrpos')) {
109+
if (\function_exists('mb_strrpos')) {
110110
return mb_strrpos($str, $find, $offset, $encoding);
111111
}
112112

@@ -128,7 +128,7 @@ public static function ucfirst($str)
128128
*/
129129
public static function ucwords($str)
130130
{
131-
if (function_exists('mb_convert_case')) {
131+
if (\function_exists('mb_convert_case')) {
132132
return mb_convert_case($str, MB_CASE_TITLE);
133133
}
134134

@@ -186,7 +186,7 @@ public static function getValueOfArray(array $array, $key, $default = null)
186186
}
187187

188188
foreach (explode('.', $key) as $segment) {
189-
if (is_array($array) && array_key_exists($segment, $array)) {
189+
if (\is_array($array) && array_key_exists($segment, $array)) {
190190
$array = $array[$segment];
191191
} else {
192192
return $default;
@@ -200,25 +200,26 @@ public static function getValueOfArray(array $array, $key, $default = null)
200200
* @param $cb
201201
* @param array $args
202202
* @return mixed
203+
* @throws \InvalidArgumentException
203204
*/
204205
public static function call($cb, ...$args)
205206
{
206-
if (is_string($cb)) {
207+
if (\is_string($cb)) {
207208
// function
208209
if (strpos($cb, '::') === false) {
209210
return $cb(...$args);
210211
}
211212

212213
// ClassName/Service::method
213214
$cb = explode('::', $cb, 2);
214-
} elseif (is_object($cb) && method_exists($cb, '__invoke')) {
215+
} elseif (\is_object($cb) && method_exists($cb, '__invoke')) {
215216
return $cb(...$args);
216217
}
217218

218-
if (is_array($cb)) {
219+
if (\is_array($cb)) {
219220
list($obj, $mhd) = $cb;
220221

221-
return is_object($obj) ? $obj->$mhd(...$args) : $obj::$mhd(...$args);
222+
return \is_object($obj) ? $obj->$mhd(...$args) : $obj::$mhd(...$args);
222223
}
223224

224225
throw new \InvalidArgumentException('The parameter is not a callable');

src/Utils/RequiredValidatorsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function requiredIf($field, $anotherField, $values)
4949

5050
$val = $this->data[$anotherField];
5151

52-
if (in_array($val, (array)$values, true)) {
52+
if (\in_array($val, (array)$values, true)) {
5353
return $this->required($field);
5454
}
5555

src/ValidationTrait.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public function afterValidate(\Closure $cb)
150150
* @param array $onlyChecked 可以设置此次需要验证的字段
151151
* @param bool|null $stopOnError 是否出现错误即停止验证
152152
* @return static
153+
* @throws \InvalidArgumentException
153154
* @throws \RuntimeException
154155
*/
155156
public function validate(array $onlyChecked = [], $stopOnError = null)
@@ -173,6 +174,7 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
173174

174175
// 循环规则
175176
foreach ($this->collectRules() as $attrs => $rule) {
177+
$attrs = \is_string($attrs) ? array_map('trim', explode(',', $attrs)) : (array)$attrs;
176178
// 要使用的验证器(a string or a Closure)
177179
$validator = array_shift($rule);
178180

@@ -207,7 +209,7 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
207209
$value = $this->getValue($attr, $defValue);
208210

209211
// 不在需要检查的列表内
210-
if ($onlyChecked && !in_array($attr, $onlyChecked, true)) {
212+
if ($onlyChecked && !\in_array($attr, $onlyChecked, true)) {
211213
continue;
212214
}
213215

@@ -218,7 +220,7 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
218220
}
219221

220222
// required* 系列字段检查器
221-
if (is_string($validator) && 0 === strpos($validator, 'required')) {
223+
if (\is_string($validator) && 0 === strpos($validator, 'required')) {
222224
if (!$this->fieldValidate($attr, $value, $validator, $args)) {
223225
$this->addError($attr, $this->getMessage($validator, $attr, $args, $message));
224226

@@ -274,6 +276,7 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
274276
* @param string $validator required* 验证器
275277
* @param array $args 验证需要的参数
276278
* @return bool
279+
* @throws \InvalidArgumentException
277280
*/
278281
protected function fieldValidate($attr, $value, $validator, $args)
279282
{
@@ -307,6 +310,7 @@ protected function fieldValidate($attr, $value, $validator, $args)
307310
* @param \Closure|string $validator 验证器
308311
* @param array $args 验证需要的参数
309312
* @return bool
313+
* @throws \InvalidArgumentException
310314
*/
311315
protected function valueValidate($data, $attr, $value, $validator, $args)
312316
{
@@ -318,10 +322,10 @@ protected function valueValidate($data, $attr, $value, $validator, $args)
318322
$args = array_values($args);
319323

320324
// if $validator is a closure
321-
if (is_object($validator) && method_exists($validator, '__invoke')) {
325+
if (\is_object($validator) && method_exists($validator, '__invoke')) {
322326
$args[] = $data;
323327
$passed = $validator($value, ...$args);
324-
} elseif (is_string($validator)) {
328+
} elseif (\is_string($validator)) {
325329
// if $validator is a custom add callback in the property {@see $_validators}.
326330
if (isset(self::$_validators[$validator])) {
327331
$callback = self::$_validators[$validator];
@@ -336,7 +340,7 @@ protected function valueValidate($data, $attr, $value, $validator, $args)
336340
$passed = ValidatorList::$validator($value, ...$args);
337341

338342
// it is function name
339-
} elseif (function_exists($validator)) {
343+
} elseif (\function_exists($validator)) {
340344
$passed = $validator($value, ...$args);
341345
} else {
342346
throw new \InvalidArgumentException("The validator [$validator] don't exists!");
@@ -374,6 +378,7 @@ protected function resetValidation($clearErrors = false)
374378
/**
375379
* 收集当前场景可用的规则列表
376380
* Collect the current scenario of the available rules list
381+
* @throws \InvalidArgumentException
377382
*/
378383
protected function collectRules()
379384
{
@@ -396,9 +401,9 @@ protected function collectRules()
396401

397402
// only use to special scene.
398403
} else {
399-
$sceneList = is_string($rule['on']) ? array_map('trim', explode(',', $rule['on'])) : (array)$rule['on'];
404+
$sceneList = \is_string($rule['on']) ? array_map('trim', explode(',', $rule['on'])) : (array)$rule['on'];
400405

401-
if ($scene && !in_array($scene, $sceneList, true)) {
406+
if ($scene && !\in_array($scene, $sceneList, true)) {
402407
continue;
403408
}
404409

@@ -407,7 +412,6 @@ protected function collectRules()
407412
}
408413

409414
$attrs = array_shift($rule);
410-
$attrs = is_string($attrs) ? array_map('trim', explode(',', $attrs)) : (array)$attrs;
411415

412416
yield $attrs => $rule;
413417
}
@@ -437,7 +441,7 @@ protected function collectSafeValue($attr, $value)
437441

438442
/**
439443
* @param mixed $val
440-
* @param mixed $compareVal
444+
* @param string $compareField
441445
* @return bool
442446
*/
443447
public function compare($val, $compareField)

0 commit comments

Comments
 (0)