@@ -21,19 +21,33 @@ class ConfigValidator implements ConfigValidatorInterface
21
21
22
22
protected $ rules = [];
23
23
24
- protected $ contextObject ;
25
-
26
24
protected $ extraFieldsAllowed = false ;
27
25
28
26
/** @var ValidationRuleInterface[] */
29
27
protected $ validationRules = [];
30
28
31
- public function __construct ($ contextObject = null )
29
+ /** @var ConfigValidator */
30
+ protected static $ instance ;
31
+
32
+ private function __construct ()
32
33
{
33
- $ this ->contextObject = $ contextObject ;
34
34
$ this ->initializeRules ();
35
35
}
36
36
37
+ /**
38
+ * @return ConfigValidator
39
+ */
40
+ public static function getInstance ()
41
+ {
42
+ if (empty (self ::$ instance )) {
43
+ self ::$ instance = new self ();
44
+ }
45
+
46
+ self ::$ instance ->clearErrors ();
47
+
48
+ return self ::$ instance ;
49
+ }
50
+
37
51
public function validate ($ data , $ rules = [], $ extraFieldsAllowed = null )
38
52
{
39
53
if ($ extraFieldsAllowed !== null ) $ this ->setExtraFieldsAllowed ($ extraFieldsAllowed );
@@ -47,7 +61,7 @@ public function validate($data, $rules = [], $extraFieldsAllowed = null)
47
61
unset($ fieldRules ['required ' ]);
48
62
49
63
if (!array_key_exists ($ fieldName , $ data )) {
50
- $ this ->addError (new ValidationException ('Field \'' . $ fieldName . '\' of ' . $ this -> getContextName () . ' is required ' ));
64
+ $ this ->addError (new ValidationException (sprintf ( 'Field "%s" is required ' , $ fieldName ) ));
51
65
52
66
continue ;
53
67
}
@@ -59,25 +73,21 @@ public function validate($data, $rules = [], $extraFieldsAllowed = null)
59
73
/** Validation of all other rules*/
60
74
foreach ($ fieldRules as $ ruleName => $ ruleInfo ) {
61
75
if (!array_key_exists ($ ruleName , $ this ->validationRules )) {
62
- $ this ->addError (new ValidationException ('Field \'' . $ fieldName . '\' has invalid rule \'' . $ ruleInfo . '\'' ));
76
+ $ this ->addError (new ValidationException (sprintf ( 'Field "%s" has invalid rule "%s" ' , $ fieldName , $ ruleInfo) ));
63
77
64
78
continue ;
65
79
}
66
80
67
81
if (!$ this ->validationRules [$ ruleName ]->validate ($ data [$ fieldName ], $ ruleInfo )) {
68
- $ this ->addError (
69
- new ValidationException ('Field \'' . $ fieldName . '\' of ' . $ this ->getContextName ()
70
- . ' expected to be ' . $ ruleName . ': \'' . (string )$ ruleInfo . '\', but got: ' . gettype ($ data [$ fieldName ])));
82
+ $ this ->addError (new ValidationException (sprintf ('Field "%s" expected to be "%s" but got "%s" ' , $ fieldName , $ ruleName , gettype ($ data [$ fieldName ]))));
71
83
}
72
84
}
73
85
}
74
86
75
87
if (!$ this ->isExtraFieldsAllowed ()) {
76
88
foreach (array_keys ($ data ) as $ fieldName ) {
77
89
if (!in_array ($ fieldName , $ processedFields )) {
78
- $ this ->addError (
79
- new ValidationException ('Field \'' . $ fieldName . '\' is not expected in ' . $ this ->getContextName ()));
80
-
90
+ $ this ->addError (new ValidationException (sprintf ('Field "%s" is not expected ' , $ fieldName )));
81
91
}
82
92
}
83
93
}
@@ -90,19 +100,9 @@ protected function initializeRules()
90
100
$ this ->validationRules ['type ' ] = new TypeValidationRule ($ this );
91
101
}
92
102
93
- /**
94
- * @return string
95
- */
96
- protected function getContextName ()
103
+ public function addRule ($ name , ValidationRuleInterface $ rule )
97
104
{
98
- if (is_object ($ this ->contextObject )) {
99
- $ class = get_class ($ this ->contextObject );
100
- $ class = substr ($ class , strrpos ($ class , '\\' ) + 1 );
101
-
102
- return $ class ;
103
- } else {
104
- return $ this ->contextObject ? $ this ->contextObject : '(context) ' ;
105
- }
105
+ $ this ->validationRules [$ name ] = $ rule ;
106
106
}
107
107
108
108
public function isValid ()
0 commit comments