77use GuzzleHttp \Psr7 \UriNormalizer ;
88use GuzzleHttp \Psr7 \UriResolver ;
99use Psr \Http \Message \UriInterface ;
10+ use Ropi \JsonSchemaEvaluator \Draft \Exception \UnsupportedVocabularyException ;
1011use Ropi \JsonSchemaEvaluator \EvaluationContext \RuntimeEvaluationContext ;
1112use Ropi \JsonSchemaEvaluator \EvaluationContext \StaticEvaluationContext ;
1213use Ropi \JsonSchemaEvaluator \Draft \Exception \InvalidSchemaException ;
1516use Ropi \JsonSchemaEvaluator \Keyword \RuntimeKeywordInterface ;
1617use Ropi \JsonSchemaEvaluator \Keyword \StaticKeywordInterface ;
1718use Ropi \JsonSchemaEvaluator \Keyword \UnknownKeyword ;
18- use Ropi \JsonSchemaEvaluator \Type \BigNumber ;
19- use Ropi \JsonSchemaEvaluator \Type \BigNumberInterface ;
19+ use Ropi \JsonSchemaEvaluator \Type \Number ;
20+ use Ropi \JsonSchemaEvaluator \Type \NumberInterface ;
2021
2122abstract class AbstractDraft implements DraftInterface
2223{
@@ -27,18 +28,104 @@ abstract class AbstractDraft implements DraftInterface
2728 private int $ lastPriority = 0 ;
2829
2930 /**
30- * @var string []
31+ * @var bool []
3132 */
32- protected const VOCABULARIES = [];
33+ protected array $ vocabularies = [];
34+
35+ public function __construct (
36+ private string $ uri = '' ,
37+ private bool $ assertFormat = false ,
38+ private bool $ assertContentMediaTypeEncoding = false ,
39+ private bool $ evaluateMutations = false ,
40+ private bool $ acceptNumericStrings = false ,
41+ private bool $ shortCircuit = false
42+ ) {}
43+
44+ public function getUri (): string
45+ {
46+ return $ this ->uri ;
47+ }
48+
49+ public function assertFormat (): bool
50+ {
51+ return $ this ->assertFormat ;
52+ }
53+
54+ public function assertContentMediaTypeEncoding (): bool
55+ {
56+ return $ this ->assertContentMediaTypeEncoding ;
57+ }
58+
59+ public function evaluateMutations (): bool
60+ {
61+ return $ this ->evaluateMutations ;
62+ }
63+
64+ public function acceptNumericStrings (): bool
65+ {
66+ return $ this ->acceptNumericStrings ;
67+ }
68+
69+ public function shortCircuit (): bool
70+ {
71+ return $ this ->shortCircuit ;
72+ }
3373
3474 public function supportsVocabulary (string $ vocabulary ): bool
3575 {
36- return isset (static ::VOCABULARIES [$ vocabulary ]) && static ::VOCABULARIES [$ vocabulary ];
76+ return isset ($ this ->vocabularies [$ vocabulary ]);
77+ }
78+
79+ public function getSupportedVocabularies (): array
80+ {
81+ return array_keys ($ this ->vocabularies );
3782 }
3883
3984 public function getVocabularies (): array
4085 {
41- return static ::VOCABULARIES ;
86+ return $ this ->vocabularies ;
87+ }
88+
89+ public function vocabularyEnabled (string $ vocabulary ): bool
90+ {
91+ if (!$ this ->supportsVocabulary ($ vocabulary )) {
92+ throw new UnsupportedVocabularyException (
93+ 'Can not enable vocabulary " '
94+ . $ vocabulary
95+ . '", because vocabulary is not supported ' ,
96+ 1647637917
97+ );
98+ }
99+
100+ return $ this ->vocabularies [$ vocabulary ];
101+ }
102+
103+ public function enableVocabulary (string $ vocabulary ): void
104+ {
105+ if (!$ this ->supportsVocabulary ($ vocabulary )) {
106+ throw new UnsupportedVocabularyException (
107+ 'Can not enable vocabulary " '
108+ . $ vocabulary
109+ . '", because vocabulary is not supported ' ,
110+ 1647637758
111+ );
112+ }
113+
114+ $ this ->vocabularies [$ vocabulary ] = true ;
115+ }
116+
117+ public function disableVocabulary (string $ vocabulary ): void
118+ {
119+ if (!$ this ->supportsVocabulary ($ vocabulary )) {
120+ throw new UnsupportedVocabularyException (
121+ 'Can not disable vocabulary " '
122+ . $ vocabulary
123+ . '", because vocabulary is not supported ' ,
124+ 1647637759
125+ );
126+ }
127+
128+ $ this ->vocabularies [$ vocabulary ] = false ;
42129 }
43130
44131 public function registerKeyword (KeywordInterface $ keyword ): void
@@ -117,7 +204,7 @@ public function evaluate(RuntimeEvaluationContext $context, bool $mutationsOnly
117204 }
118205
119206 $ lastResultNumber = $ context ->getLastResultNumber ();
120- $ shortCircuit = $ context ->config ->shortCircuit ;
207+ $ shortCircuit = $ context ->draft ->shortCircuit () ;
121208 $ valid = true ;
122209
123210 foreach ($ context ->staticEvaluationContext ->getPrioritizedSchemaKeywords ($ schema ) as $ keyword ) {
@@ -209,14 +296,14 @@ public function dereferenceJsonPointer(object $schema, string $fragment): mixed
209296 return $ currentSchemaPart ;
210297 }
211298
212- public function createBigNumber (mixed $ value, bool $ acceptNumericStrings = false ): ?BigNumberInterface
299+ public function createNumber (mixed $ value ): ?NumberInterface
213300 {
214- if ($ value instanceof BigNumberInterface ) {
301+ if ($ value instanceof NumberInterface ) {
215302 return clone $ value ;
216303 }
217304
218305 if (!is_int ($ value ) && !is_float ($ value )) {
219- if ($ acceptNumericStrings ) {
306+ if ($ this -> acceptNumericStrings () ) {
220307 if (!is_string ($ value ) || !is_numeric ($ value )) {
221308 return null ;
222309 }
@@ -226,7 +313,7 @@ public function createBigNumber(mixed $value, bool $acceptNumericStrings = false
226313 }
227314
228315 try {
229- return new BigNumber (sprintf ('%f ' , $ value ));
316+ return new Number (sprintf ('%f ' , $ value ));
230317 } catch (\InvalidArgumentException ) {
231318 // Instance is not a number
232319 }
@@ -236,13 +323,13 @@ public function createBigNumber(mixed $value, bool $acceptNumericStrings = false
236323
237324 public function valueIsNumeric (mixed $ value ): bool
238325 {
239- return is_int ($ value ) || is_float ($ value ) || $ value instanceof BigNumberInterface ;
326+ return is_int ($ value ) || is_float ($ value ) || $ value instanceof NumberInterface ;
240327 }
241328
242329 public function valuesAreEqual (mixed $ value1 , mixed $ value2 ): bool
243330 {
244331 if ($ this ->valueIsNumeric ($ value1 ) && $ this ->valueIsNumeric ($ value2 )) {
245- return $ this ->createBigNumber ($ value1 )->equals ($ this ->createBigNumber ($ value2 ));
332+ return $ this ->createNumber ($ value1 )->equals ($ this ->createNumber ($ value2 ));
246333 }
247334
248335 if (gettype ($ value1 ) !== gettype ($ value2 )) {
0 commit comments