1919class Factory
2020{
2121 /**
22- * @var UriRetriever
22+ * @var UriRetriever $uriRetriever
2323 */
2424 protected $ uriRetriever ;
2525
26+ /**
27+ * @var array $constraintMap
28+ */
29+ protected $ constraintMap = array (
30+ 'array ' => 'JsonSchema\Constraints\CollectionConstraint ' ,
31+ 'collection ' => 'JsonSchema\Constraints\CollectionConstraint ' ,
32+ 'object ' => 'JsonSchema\Constraints\ObjectConstraint ' ,
33+ 'type ' => 'JsonSchema\Constraints\TypeConstraint ' ,
34+ 'undefined ' => 'JsonSchema\Constraints\UndefinedConstraint ' ,
35+ 'string ' => 'JsonSchema\Constraints\StringConstraint ' ,
36+ 'number ' => 'JsonSchema\Constraints\NumberConstraint ' ,
37+ 'enum ' => 'JsonSchema\Constraints\EnumConstraint ' ,
38+ 'format ' => 'JsonSchema\Constraints\FormatConstraint ' ,
39+ 'schema ' => 'JsonSchema\Constraints\SchemaConstraint ' ,
40+ 'validator ' => 'JsonSchema\Validator ' ,
41+ );
42+
2643 /**
2744 * @param UriRetriever $uriRetriever
2845 */
@@ -43,6 +60,25 @@ public function getUriRetriever()
4360 return $ this ->uriRetriever ;
4461 }
4562
63+ /**
64+ * @param string $name
65+ * @param string $class
66+ * @return Factory
67+ */
68+ public function setConstraintClass ($ name , $ class )
69+ {
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 ;
80+ }
81+
4682 /**
4783 * Create a constraint instance for the given constraint name.
4884 *
@@ -52,30 +88,9 @@ public function getUriRetriever()
5288 */
5389 public function createInstanceFor ($ constraintName )
5490 {
55- switch ($ constraintName ) {
56- case 'array ' :
57- case 'collection ' :
58- return new CollectionConstraint (Constraint::CHECK_MODE_NORMAL , $ this ->uriRetriever , $ this );
59- case 'object ' :
60- return new ObjectConstraint (Constraint::CHECK_MODE_NORMAL , $ this ->uriRetriever , $ this );
61- case 'type ' :
62- return new TypeConstraint (Constraint::CHECK_MODE_NORMAL , $ this ->uriRetriever , $ this );
63- case 'undefined ' :
64- return new UndefinedConstraint (Constraint::CHECK_MODE_NORMAL , $ this ->uriRetriever , $ this );
65- case 'string ' :
66- return new StringConstraint (Constraint::CHECK_MODE_NORMAL , $ this ->uriRetriever , $ this );
67- case 'number ' :
68- return new NumberConstraint (Constraint::CHECK_MODE_NORMAL , $ this ->uriRetriever , $ this );
69- case 'enum ' :
70- return new EnumConstraint (Constraint::CHECK_MODE_NORMAL , $ this ->uriRetriever , $ this );
71- case 'format ' :
72- return new FormatConstraint (Constraint::CHECK_MODE_NORMAL , $ this ->uriRetriever , $ this );
73- case 'schema ' :
74- return new SchemaConstraint (Constraint::CHECK_MODE_NORMAL , $ this ->uriRetriever , $ this );
75- case 'validator ' :
76- return new Validator (Constraint::CHECK_MODE_NORMAL , $ this ->uriRetriever , $ this );
91+ if (array_key_exists ($ constraintName , $ this ->constraintMap )) {
92+ return new $ this ->constraintMap [$ constraintName ](Constraint::CHECK_MODE_NORMAL , $ this ->uriRetriever , $ this );
7793 }
78-
7994 throw new InvalidArgumentException ('Unknown constraint ' . $ constraintName );
8095 }
8196}
0 commit comments