@@ -15,63 +15,130 @@ class Validator
15
15
16
16
private $ validated = [];
17
17
18
+ private $ _customMessages = [];
19
+
20
+ private $ _attributeLabels = [];
21
+
22
+ private $ _data = [];
23
+
18
24
public function make ($ data , $ ruleFields , $ customMessages = null , $ attributeLabels = null )
19
25
{
26
+ $ this ->_data = $ data ;
27
+ $ this ->_customMessages = $ customMessages ;
28
+ $ this ->_attributeLabels = $ attributeLabels ;
29
+
20
30
$ this ->inputContainer = new InputDataContainer ($ data );
21
31
22
32
$ this ->errorBag = new ErrorBag ();
23
33
24
34
foreach ($ ruleFields as $ field => $ rules ) {
25
- $ attributeLabel = $ field ;
35
+ $ this ->processAndValidateField ($ field , $ rules );
36
+ }
26
37
27
- if (isset ($ attributeLabels [$ field ])) {
28
- $ attributeLabel = $ attributeLabels [$ field ];
29
- }
38
+ return $ this ;
39
+ }
40
+
41
+ public function processAndValidateField ($ field , $ rules )
42
+ {
43
+ $ attributeLabel = $ field ;
30
44
31
- $ this ->inputContainer -> setAttributeKey ($ field );
45
+ $ fieldKeys = $ this ->processWildcardFieldKey ($ field );
32
46
33
- $ this ->inputContainer ->setAttributeLabel ($ attributeLabel );
47
+ foreach ($ fieldKeys as $ fieldKey ) {
48
+ $ this ->validateField ($ fieldKey , $ rules , $ attributeLabel );
49
+ }
50
+ }
34
51
35
- $ value = $ this ->inputContainer ->getAttributeValue ();
52
+ public function processWildcardFieldKey ($ field )
53
+ {
54
+ if (strpos ($ field , '* ' ) === false ) {
55
+ return [$ field ];
56
+ }
36
57
37
- $ this ->setValidatedData ($ field , $ data , $ value );
58
+ $ nestedKeyQueue = explode ('. ' , $ field );
59
+ $ visitedFieldKeys = [];
60
+ $ dataByKey = (array ) $ this ->_data ;
38
61
39
- if (\in_array ('nullable ' , $ rules ) && $ this ->isEmpty ($ value )) {
40
- continue ;
62
+ while ($ head = array_shift ($ nestedKeyQueue )) {
63
+ if (trim ($ head ) === '* ' ) {
64
+ $ keys = array_keys ((array ) $ dataByKey );
65
+ $ dataByKey = count ($ keys ) && \array_key_exists ($ keys [0 ], $ dataByKey ) ? $ dataByKey [$ keys [0 ]] : [];
66
+ } else {
67
+ $ keys = [$ head ];
68
+ $ dataByKey = \array_key_exists ($ head , $ dataByKey ) ? $ dataByKey [$ head ] : [];
41
69
}
42
70
43
- foreach ($ rules as $ ruleName ) {
44
- if (\is_string ($ ruleName ) && strpos ($ ruleName , 'sanitize ' ) !== false ) {
45
- $ this ->applyFilter ($ ruleName , $ field , $ value );
46
-
47
- continue ;
71
+ if (empty ($ visitedFieldKeys )) {
72
+ foreach ($ keys as $ keyToVisit ) {
73
+ $ visitedFieldKeys [$ keyToVisit ] = 1 ;
48
74
}
49
-
50
- if ( is_subclass_of ( $ ruleName , Rule::class) ) {
51
- $ ruleClass = \is_object ( $ ruleName ) ? $ ruleName : new $ ruleName ();
52
- } else {
53
- list ( $ ruleName , $ paramValues ) = $ this -> parseRule ( $ ruleName ) ;
54
- $ ruleClass = $ this -> resolveRule ( $ ruleName );
75
+ } else {
76
+ foreach ( $ visitedFieldKeys as $ key => $ v ) {
77
+ foreach ( $ keys as $ keyToVisit ) {
78
+ unset( $ visitedFieldKeys [ $ key ]);
79
+ $ visitedFieldKeys [ "{ $ key } . { $ keyToVisit }" ] = 1 ;
80
+ }
55
81
}
82
+ }
83
+ }
56
84
57
- $ ruleClass -> setInputDataContainer ( $ this -> inputContainer );
58
- $ ruleClass -> setRuleName ( $ ruleName );
85
+ return array_keys ( $ visitedFieldKeys );
86
+ }
59
87
60
- if (!empty ($ paramValues )) {
61
- $ ruleClass ->setParameterValues ($ ruleClass ->getParamKeys (), $ paramValues );
62
- }
88
+ public function validateField ($ fieldKey , $ rules , $ fieldLabel )
89
+ {
90
+ if (isset ($ this ->_attributeLabels [$ fieldLabel ])) {
91
+ $ attributeLabel = $ this ->_attributeLabels [$ fieldLabel ];
92
+ } else {
93
+ $ attributeLabel = $ fieldKey ;
94
+ }
63
95
64
- $ isValidated = $ ruleClass -> validate ( $ this ->inputContainer ->getAttributeValue () );
96
+ $ this ->inputContainer ->setAttributeKey ( $ fieldKey );
65
97
66
- if (!$ isValidated ) {
67
- $ this ->errorBag ->addError ($ ruleClass , $ customMessages );
98
+ $ this ->inputContainer ->setAttributeLabel ($ attributeLabel );
68
99
69
- break ;
70
- }
71
- }
100
+ $ value = $ this ->inputContainer ->getAttributeValue ();
101
+
102
+ $ this ->setValidatedData ($ fieldKey , $ this ->_data , $ value );
103
+
104
+ if (\in_array ('nullable ' , $ rules ) && $ this ->isEmpty ($ value )) {
105
+ return ;
72
106
}
73
107
74
- return $ this ;
108
+ $ this ->validateByRules ($ fieldKey , $ value , $ rules );
109
+ }
110
+
111
+ public function validateByRules ($ fieldKey , $ value , $ rules )
112
+ {
113
+ foreach ($ rules as $ ruleName ) {
114
+ if (\is_string ($ ruleName ) && strpos ($ ruleName , 'sanitize ' ) !== false ) {
115
+ $ this ->applyFilter ($ ruleName , $ fieldKey , $ value );
116
+
117
+ continue ;
118
+ }
119
+
120
+ if (is_subclass_of ($ ruleName , Rule::class)) {
121
+ $ ruleClass = \is_object ($ ruleName ) ? $ ruleName : new $ ruleName ();
122
+ } else {
123
+ list ($ ruleName , $ paramValues ) = $ this ->parseRule ($ ruleName );
124
+ $ ruleClass = $ this ->resolveRule ($ ruleName );
125
+ }
126
+
127
+ $ ruleClass ->setInputDataContainer ($ this ->inputContainer );
128
+ $ ruleClass ->setRuleName ($ ruleName );
129
+
130
+ if (!empty ($ paramValues )) {
131
+ $ ruleClass ->setParameterValues ($ ruleClass ->getParamKeys (), $ paramValues );
132
+ }
133
+
134
+ $ isValidated = $ ruleClass ->validate ($ this ->inputContainer ->getAttributeValue ());
135
+
136
+ if (!$ isValidated ) {
137
+ $ this ->errorBag ->addError ($ ruleClass , $ this ->_customMessages );
138
+
139
+ break ;
140
+ }
141
+ }
75
142
}
76
143
77
144
public function fails ()
@@ -96,8 +163,8 @@ private function resolveRule($ruleName)
96
163
}
97
164
98
165
$ ruleClass = __NAMESPACE__
99
- . '\\Rules \\'
100
- . str_replace (' ' , '' , ucwords (str_replace ('_ ' , ' ' , $ ruleName )))
166
+ . '\\Rules \\'
167
+ . str_replace (' ' , '' , ucwords (str_replace ('_ ' , ' ' , $ ruleName )))
101
168
. 'Rule ' ;
102
169
103
170
if (!class_exists ($ ruleClass )) {
0 commit comments