Skip to content

Commit 8f87b74

Browse files
committed
add some new method. bug fixed for required check. add more unit test
1 parent 423f180 commit 8f87b74

8 files changed

+241
-59
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -677,14 +677,15 @@ public function addValidator(string $name, \Closure $callback, string $msg = '')
677677

678678
```
679679
// 验证失败
680-
public function hasError()
681-
public function isFail() // hasError() 的别名方法
682-
public function fail() // hasError() 的别名方法
680+
public function isFail()
681+
public function fail() // isFail() 的别名方法
682+
public function failed() // isFail() 的别名方法
683+
public function hasError() // isFail() 的别名方法
683684
684685
// 成功通过验证
685686
public function ok()
686-
public function passed()
687-
public function isPassed() // passed() 的别名方法
687+
public function isOk()
688+
public function isPassed()
688689
```
689690

690691
获取验证是否通过(是否有验证失败)。
@@ -713,20 +714,20 @@ public function getErrors(): array
713714
public function firstError($onlyMsg = true)
714715
```
715716

716-
- `$onlyMsg` 是否只返回消息字符串。当为 false,返回的则是数组 eg: `[ attr => 'error message']`
717+
- `$onlyMsg` 是否只返回消息字符串。当为 false,返回的则是数组 eg: `['name' => 'field', 'msg' => 'error message']`
717718

718719
### 得到最后一个错误信息
719720

720721
```php
721722
public function lastError($onlyMsg = true)
722723
```
723724

724-
- `$onlyMsg` 是否只返回消息字符串。当为 false,返回的则是数组 eg: `[ attr => 'error message']`
725+
- `$onlyMsg` 是否只返回消息字符串。当为 false,返回的则是数组 eg: `['name' => 'field', 'msg' => 'error message']`
725726

726727
### 获取所有验证通过的数据
727728

728729
```php
729-
public function getSafeData(): array
730+
public function getSafeData(): array|\stdClass
730731
```
731732

732733
获取所有 **验证通过** 的安全数据.
@@ -739,8 +740,9 @@ public function getSafeData(): array
739740
### 根据字段名获取安全值
740741

741742
```php
742-
public function getSafe(string $key, $default = null)
743+
public function val(string $key, $default = null) // getSafe() 的别名方法
743744
public function getValid(string $key, $default = null) // getSafe() 的别名方法
745+
public function getSafe(string $key, $default = null)
744746
```
745747

746748
**验证通过** 的数据中取出对应 key 的值
@@ -756,7 +758,7 @@ public function all(): array
756758
### 根据字段名获取原始数据的值
757759

758760
```php
759-
public function get(string $key, $default = null)
761+
public function getRaw(string $key, $default = null)
760762
```
761763

762764
从验证时传入的数据中取出对应 key 的值

src/Utils/ErrorMessageTrait.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ public function ok(): bool
183183
/**
184184
* @return bool
185185
*/
186+
public function isOk(): bool
187+
{
188+
return !$this->isFail();
189+
}
190+
191+
/**
192+
* @deprecated will delete
193+
* @return bool
194+
*/
186195
public function passed(): bool
187196
{
188197
return !$this->isFail();

src/Utils/UserAndContextValidatorsTrait.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function required($field, $value = null)
139139
{
140140
if (null !== $value) {
141141
$val = $value;
142-
} elseif (null !== ($val = $this->getValue($field))) {
142+
} elseif (null === ($val = $this->getByPath($field))) {
143143
// check uploaded files
144144
if (!isset($this->uploadedFiles[$field])) {
145145
return false;
@@ -198,7 +198,7 @@ public function requiredUnless($field, $fieldVal, $anotherField, $values)
198198
}
199199

200200
/**
201-
* 如果指定的字段中的 任意一个 有值且不为空,则此字段为必填
201+
* 如果指定的其他字段中的 任意一个 有值且不为空,则此字段为 必填
202202
* @from laravel
203203
* @param string $field
204204
* @param mixed $fieldVal
@@ -411,7 +411,7 @@ public function mimesValidator($field, $types = null)
411411
*/
412412
public function compareValidator($val, $compareField)
413413
{
414-
return $compareField && ($val === $this->get($compareField));
414+
return $compareField && ($val === $this->getByPath($compareField));
415415
}
416416

417417
public function sameValidator($val, $compareField)
@@ -432,7 +432,7 @@ public function equalValidator($val, $compareField)
432432
*/
433433
public function notEqualValidator($val, $compareField)
434434
{
435-
return $compareField && ($val !== $this->get($compareField));
435+
return $compareField && ($val !== $this->getByPath($compareField));
436436
}
437437

438438
/**
@@ -443,7 +443,7 @@ public function notEqualValidator($val, $compareField)
443443
*/
444444
public function differentValidator($val, $compareField)
445445
{
446-
return $compareField && ($val !== $this->get($compareField));
446+
return $compareField && ($val !== $this->getByPath($compareField));
447447
}
448448

449449
/**
@@ -454,7 +454,7 @@ public function differentValidator($val, $compareField)
454454
*/
455455
public function inFieldValidator($val, $anotherField)
456456
{
457-
if ($anotherField && $dict = $this->get($anotherField)) {
457+
if ($anotherField && $dict = $this->getByPath($anotherField)) {
458458
return ValidatorList::in($val, $dict);
459459
}
460460

src/ValidationInterface.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ public function isFail(): bool;
5757
/**
5858
* @return bool
5959
*/
60-
public function passed(): bool;
60+
public function ok(): bool;
61+
62+
/**
63+
* @return bool
64+
*/
65+
public function isPassed(): bool;
6166

6267
/**
6368
* @return array
@@ -86,7 +91,7 @@ public function lastError($onlyMsg = true);
8691
public function getMessages(): array;
8792

8893
/**
89-
* @return array
94+
* @return array|\stdClass
9095
*/
91-
public function getSafeData(): array;
96+
public function getSafeData();
9297
}

src/ValidationTrait.php

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,6 @@ trait ValidationTrait
2222
{
2323
use DataFiltersTrait, ErrorMessageTrait, UserAndContextValidatorsTrait;
2424

25-
/**
26-
* current scenario name
27-
* 当前验证的场景 -- 如果需要让规则列表在多个类似情形下使用
28-
* (
29-
* e.g: 在MVC框架中,
30-
* - 通常可以根据控制器的 action name(add, edit, register) 来区分。
31-
* - 或者根据模型的场景(create, update, delete) 来区分。
32-
* )
33-
* @var string
34-
*/
35-
protected $scene = '';
36-
37-
/** @var array used rules at current scene */
38-
protected $_usedRules = [];
39-
4025
/**
4126
* the rules is by setRules()
4227
* @var array
@@ -58,6 +43,21 @@ trait ValidationTrait
5843
/** @var \Closure after validate handler */
5944
private $_afterHandler;
6045

46+
/**
47+
* current scenario name
48+
* 当前验证的场景 -- 如果需要让规则列表在多个类似情形下使用
49+
* (
50+
* e.g: 在MVC框架中,
51+
* - 通常可以根据控制器的 action name(add, edit, register) 来区分。
52+
* - 或者根据模型的场景(create, update, delete) 来区分。
53+
* )
54+
* @var string
55+
*/
56+
protected $scene = '';
57+
58+
/** @var array used rules at current scene */
59+
protected $_usedRules = [];
60+
6161
/**
6262
* @return array
6363
*/
@@ -92,6 +92,18 @@ public function messages()
9292
];
9393
}
9494

95+
/**
96+
* 当前场景需要收集的字段
97+
*/
98+
// public function scenarios()
99+
// {
100+
// return [
101+
// // scene name => needed fields ...
102+
// // 'scene' => ['filed1', 'field2'],
103+
// // 'create' => ['filed1', 'field2'],
104+
// ];
105+
// }
106+
95107
/**
96108
* before validate handler
97109
* @param \Closure $cb
@@ -193,12 +205,12 @@ public function validate(array $onlyChecked = null, $stopOnError = null)
193205
continue;
194206
}
195207

196-
$value = $this->getValue($field, $defValue);
208+
$value = $this->getByPath($field, $defValue);
197209
// $hasWildcard = (bool)strpos($field, '*');
198210

199211
// mark field is safe. not need validate. like. 'created_at'
200212
if ($validator === 'safe') {
201-
$this->_safeData[$field] = $value;
213+
$this->setSafe($field, $value);
202214
continue;
203215
}
204216

@@ -360,11 +372,12 @@ protected function collectSafeValue($field, $value)
360372
{
361373
// 进行的是子级属性检查 eg: 'goods.apple'
362374
if ($pos = strpos($field, '.')) {
363-
$firstLevelKey = substr($field, 0, $pos);
364-
$this->_safeData[$firstLevelKey] = $this->data[$firstLevelKey];
365-
} else {
366-
$this->_safeData[$field] = $value;
375+
$field = (string)substr($field, 0, $pos);
376+
$value = $this->data[$field];
367377
}
378+
379+
// set
380+
$this->_safeData[$field] = $value;
368381
}
369382

370383
/**
@@ -608,31 +621,42 @@ public function getRaw(string $key, $default = null)
608621
* @param mixed $default The default value
609622
* @return mixed The key's value, or the default value
610623
*/
611-
public function getValue(string $key, $default = null)
624+
public function getByPath(string $key, $default = null)
612625
{
613626
return Helper::getValueOfArray($this->data, $key, $default);
614627
}
615628

616629
/**
617-
* get safe field value
630+
* alias of the 'getSafe()'
618631
* @param string $key
619632
* @param mixed $default
620633
* @return mixed
621634
*/
622-
public function getSafe(string $key, $default = null)
635+
public function val(string $key, $default = null)
623636
{
624-
return $this->getValid($key, $default);
637+
return $this->getSafe($key, $default);
625638
}
626639

627640
/**
628-
* get safe field value
641+
* alias of the 'getSafe()'
629642
* @param string $key
630643
* @param mixed $default
631644
* @return mixed
632645
*/
633646
public function getValid(string $key, $default = null)
634647
{
635-
return array_key_exists($key, $this->_safeData) ? $this->_safeData[$key] : $default;
648+
return $this->getSafe($key, $default);
649+
}
650+
651+
/**
652+
* get safe field value
653+
* @param string $key
654+
* @param mixed $default
655+
* @return mixed
656+
*/
657+
public function getSafe(string $key, $default = null)
658+
{
659+
return $this->_safeData[$key] ?? $default;
636660
}
637661

638662
/**
@@ -645,11 +669,21 @@ public function setSafe(string $key, $value)
645669
}
646670

647671
/**
648-
* @return array
672+
* @param bool $asObject
673+
* @return array|\stdClass
674+
*/
675+
public function safeData($asObject = false)
676+
{
677+
return $this->getSafeData($asObject);
678+
}
679+
680+
/**
681+
* @param bool $asObject
682+
* @return array|\stdClass
649683
*/
650-
public function getSafeData(): array
684+
public function getSafeData($asObject = false)
651685
{
652-
return $this->_safeData;
686+
return $asObject ? (object)$this->_safeData : $this->_safeData;
653687
}
654688

655689
/**
@@ -666,9 +700,10 @@ public function setSafeData(array $safeData, $clearOld = false)
666700
}
667701

668702
/**
703+
* Through the validation of the data keys
669704
* @return array
670705
*/
671-
public function getSafeFields(): array
706+
public function getSafeKeys(): array
672707
{
673708
return array_keys($this->_safeData);
674709
}

tests/FieldValidationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testValidate()
4242
])
4343
->validate([], false);
4444

45-
$this->assertFalse($v->passed());
45+
$this->assertFalse($v->isOk());
4646
$this->assertTrue($v->failed());
4747

4848
$errors = $v->getErrors();

0 commit comments

Comments
 (0)