19
19
class Factory
20
20
{
21
21
/**
22
- * @var UriRetriever
22
+ * @var UriRetriever $uriRetriever
23
23
*/
24
24
protected $ uriRetriever ;
25
25
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
+
26
43
/**
27
44
* @param UriRetriever $uriRetriever
28
45
*/
@@ -43,6 +60,25 @@ public function getUriRetriever()
43
60
return $ this ->uriRetriever ;
44
61
}
45
62
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
+
46
82
/**
47
83
* Create a constraint instance for the given constraint name.
48
84
*
@@ -52,30 +88,9 @@ public function getUriRetriever()
52
88
*/
53
89
public function createInstanceFor ($ constraintName )
54
90
{
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 );
77
93
}
78
-
79
94
throw new InvalidArgumentException ('Unknown constraint ' . $ constraintName );
80
95
}
81
96
}
0 commit comments