11
11
12
12
use JsonSchema \Exception \InvalidArgumentException ;
13
13
use JsonSchema \Uri \UriRetriever ;
14
- use JsonSchema \Validator ;
15
14
16
15
/**
17
16
* Factory for centralize constraint initialization.
@@ -23,6 +22,16 @@ class Factory
23
22
*/
24
23
protected $ uriRetriever ;
25
24
25
+ /**
26
+ * @var int
27
+ */
28
+ private $ checkMode ;
29
+
30
+ /**
31
+ * @var TypeCheck\TypeCheckInterface[]
32
+ */
33
+ private $ typeCheck = array ();
34
+
26
35
/**
27
36
* @var array $constraintMap
28
37
*/
@@ -43,13 +52,14 @@ class Factory
43
52
/**
44
53
* @param UriRetriever $uriRetriever
45
54
*/
46
- public function __construct (UriRetriever $ uriRetriever = null )
55
+ public function __construct (UriRetriever $ uriRetriever = null , $ checkMode = Constraint:: CHECK_MODE_NORMAL )
47
56
{
48
57
if (!$ uriRetriever ) {
49
58
$ uriRetriever = new UriRetriever ();
50
59
}
51
60
52
61
$ this ->uriRetriever = $ uriRetriever ;
62
+ $ this ->checkMode = $ checkMode ;
53
63
}
54
64
55
65
/**
@@ -60,23 +70,36 @@ public function getUriRetriever()
60
70
return $ this ->uriRetriever ;
61
71
}
62
72
73
+ public function getTypeCheck ()
74
+ {
75
+ if (!isset ($ this ->typeCheck [$ this ->checkMode ])) {
76
+ if ($ this ->checkMode === Constraint::CHECK_MODE_TYPE_CAST ) {
77
+ $ this ->typeCheck [Constraint::CHECK_MODE_TYPE_CAST ] = new TypeCheck \LooseTypeCheck ();
78
+ } else {
79
+ $ this ->typeCheck [$ this ->checkMode ] = new TypeCheck \StrictTypeCheck ();
80
+ }
81
+ }
82
+
83
+ return $ this ->typeCheck [$ this ->checkMode ];
84
+ }
85
+
63
86
/**
64
87
* @param string $name
65
88
* @param string $class
66
89
* @return Factory
67
90
*/
68
91
public function setConstraintClass ($ name , $ class )
69
92
{
70
- // Ensure class exists
71
- if (!class_exists ($ class )) {
72
- throw new InvalidArgumentException ('Unknown constraint ' . $ name );
73
- }
74
- // Ensure class is appropriate
75
- if (!in_array ('JsonSchema\Constraints\ConstraintInterface ' , class_implements ($ class ))) {
76
- throw new InvalidArgumentException ('Invalid class ' . $ name );
77
- }
78
- $ this ->constraintMap [$ name ] = $ class ;
79
- return $ this ;
93
+ // Ensure class exists
94
+ if (!class_exists ($ class )) {
95
+ throw new InvalidArgumentException ('Unknown constraint ' . $ name );
96
+ }
97
+ // Ensure class is appropriate
98
+ if (!in_array ('JsonSchema\Constraints\ConstraintInterface ' , class_implements ($ class ))) {
99
+ throw new InvalidArgumentException ('Invalid class ' . $ name );
100
+ }
101
+ $ this ->constraintMap [$ name ] = $ class ;
102
+ return $ this ;
80
103
}
81
104
82
105
/**
@@ -89,7 +112,7 @@ public function setConstraintClass($name, $class)
89
112
public function createInstanceFor ($ constraintName )
90
113
{
91
114
if (array_key_exists ($ constraintName , $ this ->constraintMap )) {
92
- return new $ this ->constraintMap [$ constraintName ](Constraint:: CHECK_MODE_NORMAL , $ this ->uriRetriever , $ this );
115
+ return new $ this ->constraintMap [$ constraintName ]($ this -> checkMode , $ this ->uriRetriever , $ this );
93
116
}
94
117
throw new InvalidArgumentException ('Unknown constraint ' . $ constraintName );
95
118
}
0 commit comments