@@ -17,9 +17,10 @@ final class Shorthand
17
17
{
18
18
/**
19
19
* @param array<string, mixed> $shorthand
20
+ * @param string|null $namespace
20
21
* @return array<string, mixed>
21
22
*/
22
- public static function convertToJsonSchema (array $ shorthand ): array
23
+ public static function convertToJsonSchema (array $ shorthand, ? string $ namespace = null ): array
23
24
{
24
25
$ schema = [
25
26
'type ' => 'object ' ,
@@ -30,6 +31,10 @@ public static function convertToJsonSchema(array $shorthand): array
30
31
'additionalProperties ' => false ,
31
32
];
32
33
34
+ if ($ namespace !== null ) {
35
+ $ schema ['namespace ' ] = $ namespace ;
36
+ }
37
+
33
38
foreach ($ shorthand as $ property => $ shorthandDefinition ) {
34
39
if (! \is_string ($ property ) || empty ($ property )) {
35
40
throw InvalidShorthand::emptyString ($ shorthand );
@@ -100,10 +105,6 @@ private static function convertShorthandStringToJsonSchema(string $shorthandStr)
100
105
101
106
$ parts = \explode ('| ' , $ shorthandStr );
102
107
103
- if ($ parts [0 ] === 'enum ' ) {
104
- return ['enum ' => \array_slice ($ parts , 1 )];
105
- }
106
-
107
108
if (\mb_substr ($ parts [0 ], -2 ) === '[] ' ) {
108
109
$ itemsParts = [\mb_substr ($ parts [0 ], 0 , -2 )];
109
110
\array_push ($ itemsParts , ...\array_slice ($ parts , 1 ));
@@ -114,39 +115,63 @@ private static function convertShorthandStringToJsonSchema(string $shorthandStr)
114
115
];
115
116
}
116
117
117
- switch ($ parts [0 ]) {
118
- case 'string ' :
119
- case 'integer ' :
120
- case 'number ' :
121
- case 'boolean ' :
118
+ switch (true ) {
119
+ case \mb_strpos ($ parts [0 ], 'string ' ) === 0 :
120
+ case \mb_strpos ($ parts [0 ], 'integer ' ) === 0 :
121
+ case \mb_strpos ($ parts [0 ], 'number ' ) === 0 :
122
+ case \mb_strpos ($ parts [0 ], 'boolean ' ) === 0 :
123
+ case \mb_strpos ($ parts [0 ], 'enum: ' ) === 0 :
122
124
$ type = $ parts [0 ];
125
+ $ typeKey = 'type ' ;
126
+ $ typeValue = $ type ;
127
+
128
+ if (\mb_strpos ($ parts [0 ], 'enum: ' ) === 0 ) {
129
+ $ typeValue = \explode (', ' , \mb_substr ($ parts [0 ], 5 ));
130
+ $ typeKey = 'enum ' ;
131
+ }
123
132
124
133
if (isset ($ parts [1 ]) && $ parts [1 ] === 'null ' ) {
125
- $ type = [$ type , 'null ' ];
134
+ $ typeValue = [$ type , 'null ' ];
126
135
127
136
\array_splice ($ parts , 1 , 1 );
128
137
}
129
138
130
- $ schema = ['type ' => $ type ];
139
+ $ schema = self ::populateSchema ($ parts );
140
+ $ schema [$ typeKey ] = $ typeValue ;
131
141
132
- if (\count ($ parts ) > 1 ) {
133
- $ parts = \array_slice ($ parts , 1 );
142
+ return $ schema ;
143
+ default :
144
+ $ type = $ parts [0 ];
134
145
135
- foreach ($ parts as $ part ) {
136
- [$ validationKey , $ validationValue ] = self ::parseShorthandValidation ($ part );
146
+ $ schema = self ::populateSchema ($ parts );
137
147
138
- $ schema [$ validationKey ] = $ validationValue ;
139
- }
140
- }
148
+ $ schema ['$ref ' ] = '#/definitions/ ' .$ type ;
141
149
142
150
return $ schema ;
143
- default :
144
- return [
145
- '$ref ' => '#/definitions/ ' .$ parts [0 ],
146
- ];
147
151
}
148
152
}
149
153
154
+ /**
155
+ * @param array<int, mixed> $parts
156
+ * @return array<string, mixed>
157
+ */
158
+ private static function populateSchema (array $ parts ): array
159
+ {
160
+ $ schema = [];
161
+
162
+ if (\count ($ parts ) > 1 ) {
163
+ $ parts = \array_slice ($ parts , 1 );
164
+
165
+ foreach ($ parts as $ part ) {
166
+ [$ validationKey , $ validationValue ] = self ::parseShorthandValidation ($ part );
167
+
168
+ $ schema [$ validationKey ] = $ validationValue ;
169
+ }
170
+ }
171
+
172
+ return $ schema ;
173
+ }
174
+
150
175
/**
151
176
* @param string $shorthandValidation
152
177
* @return array<mixed>
0 commit comments