@@ -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 }
0 commit comments