@@ -27,20 +27,11 @@ class RefKeywordParser extends KeywordParser
27
27
{
28
28
use VariablesTrait;
29
29
30
- protected ?string $ recursiveRef = null ;
31
- protected ?string $ recursiveAnchor = null ;
32
- protected bool $ dynamic = false ;
33
-
34
- public function __construct (
35
- string $ keyword ,
36
- ?string $ recursiveRef = null ,
37
- ?string $ recursiveAnchor = null ,
38
- bool $ dynamic = false
39
- ) {
30
+ protected ?array $ variations = null ;
31
+
32
+ public function __construct (string $ keyword , ?array $ variations = null ) {
40
33
parent ::__construct ($ keyword );
41
- $ this ->recursiveRef = $ recursiveRef ;
42
- $ this ->recursiveAnchor = $ recursiveAnchor ;
43
- $ this ->dynamic = $ dynamic ;
34
+ $ this ->variations = $ variations ;
44
35
}
45
36
46
37
/**
@@ -56,24 +47,36 @@ public function type(): string
56
47
*/
57
48
public function parse (SchemaInfo $ info , SchemaParser $ parser , object $ shared ): ?Keyword
58
49
{
50
+ $ ref = null ;
59
51
$ recursive = false ;
60
52
$ schema = $ info ->data ();
53
+ $ variation = null ;
61
54
62
55
if ($ this ->keywordExists ($ schema )) {
63
56
$ ref = $ this ->keywordValue ($ schema );
64
57
if (!is_string ($ ref ) || $ ref === '' ) {
65
58
throw $ this ->keywordException ('{keyword} must be a non-empty string ' , $ info );
66
59
}
67
- } elseif ($ this ->recursiveRef && $ this ->keywordExists ($ schema , $ this ->recursiveRef )) {
68
- $ ref = $ this ->keywordValue ($ schema , $ this ->recursiveRef );
69
- if ($ this ->dynamic ) {
70
- if (!preg_match ('/^#[a-z][a-z0-9 \\-.:_]*/i ' , $ ref )) {
71
- $ this ->keywordException ("{keyword} value is malformed " , $ info , $ this ->recursiveRef );
60
+ } elseif ($ this ->variations ) {
61
+ foreach ($ this ->variations as $ v ) {
62
+ if (!$ this ->keywordExists ($ schema , $ v ['ref ' ])) {
63
+ continue ;
64
+ }
65
+ $ ref = $ this ->keywordValue ($ schema , $ v ['ref ' ]);
66
+ if ($ v ['fragment ' ]) {
67
+ if (!preg_match ('/^#[a-z][a-z0-9 \\-.:_]*/i ' , $ ref )) {
68
+ $ this ->keywordException ("{keyword} value is malformed " , $ info , $ v ['ref ' ]);
69
+ }
70
+ } elseif ($ ref !== '# ' ) {
71
+ $ this ->keywordException ("{keyword} supports only '#' as value " , $ info , $ v ['ref ' ]);
72
72
}
73
- } elseif ($ ref !== '# ' ) {
74
- $ this ->keywordException ("{keyword} supports only '#' as value " , $ info , $ this ->recursiveRef );
73
+ $ variation = $ v ;
74
+ $ recursive = true ;
75
+ break ;
76
+ }
77
+ if (!$ recursive ) {
78
+ return null ;
75
79
}
76
- $ recursive = true ;
77
80
} else {
78
81
return null ;
79
82
}
@@ -110,12 +113,12 @@ public function parse(SchemaInfo $info, SchemaParser $parser, object $shared): ?
110
113
111
114
if ($ recursive ) {
112
115
$ ref = $ info ->idBaseRoot ()->resolveRef ($ ref );
113
- if ($ this -> dynamic ) {
114
- return new RecursiveRefKeyword ($ ref , $ mapper , $ globals , $ slots ,
115
- $ this -> recursiveRef , $ this -> recursiveAnchor , $ ref ->fragment ());
116
+ if ($ variation [ ' fragment ' ] ) {
117
+ return new RecursiveRefKeyword ($ ref-> resolveRef ( ' # ' ) , $ mapper , $ globals , $ slots ,
118
+ $ variation [ ' ref ' ] , $ variation [ ' anchor ' ] , $ ref ->fragment ());
116
119
}
117
120
return new RecursiveRefKeyword ($ ref , $ mapper , $ globals , $ slots ,
118
- $ this -> recursiveRef , $ this -> recursiveAnchor , true );
121
+ $ variation [ ' ref ' ] , $ variation [ ' anchor ' ] , true );
119
122
}
120
123
121
124
if ($ ref === '# ' ) {
0 commit comments