1111
1212use JsonSchema \Exception \InvalidArgumentException ;
1313use JsonSchema \Uri \UriRetriever ;
14- use JsonSchema \Validator ;
1514
1615/**
1716 * Factory for centralize constraint initialization.
@@ -23,6 +22,16 @@ class Factory
2322 */
2423 protected $ uriRetriever ;
2524
25+ /**
26+ * @var int
27+ */
28+ private $ checkMode ;
29+
30+ /**
31+ * @var TypeCheck\TypeCheckInterface[]
32+ */
33+ private $ typeCheck = array ();
34+
2635 /**
2736 * @var array $constraintMap
2837 */
@@ -43,13 +52,14 @@ class Factory
4352 /**
4453 * @param UriRetriever $uriRetriever
4554 */
46- public function __construct (UriRetriever $ uriRetriever = null )
55+ public function __construct (UriRetriever $ uriRetriever = null , $ checkMode = Constraint:: CHECK_MODE_NORMAL )
4756 {
4857 if (!$ uriRetriever ) {
4958 $ uriRetriever = new UriRetriever ();
5059 }
5160
5261 $ this ->uriRetriever = $ uriRetriever ;
62+ $ this ->checkMode = $ checkMode ;
5363 }
5464
5565 /**
@@ -60,23 +70,36 @@ public function getUriRetriever()
6070 return $ this ->uriRetriever ;
6171 }
6272
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+
6386 /**
6487 * @param string $name
6588 * @param string $class
6689 * @return Factory
6790 */
6891 public function setConstraintClass ($ name , $ class )
6992 {
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 ;
80103 }
81104
82105 /**
@@ -89,7 +112,7 @@ public function setConstraintClass($name, $class)
89112 public function createInstanceFor ($ constraintName )
90113 {
91114 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 );
93116 }
94117 throw new InvalidArgumentException ('Unknown constraint ' . $ constraintName );
95118 }
0 commit comments