@@ -17,9 +17,10 @@ final class Shorthand
1717{
1818 /**
1919 * @param array<string, mixed> $shorthand
20+ * @param string|null $namespace
2021 * @return array<string, mixed>
2122 */
22- public static function convertToJsonSchema (array $ shorthand ): array
23+ public static function convertToJsonSchema (array $ shorthand, ? string $ namespace = null ): array
2324 {
2425 $ schema = [
2526 'type ' => 'object ' ,
@@ -30,6 +31,10 @@ public static function convertToJsonSchema(array $shorthand): array
3031 'additionalProperties ' => false ,
3132 ];
3233
34+ if ($ namespace !== null ) {
35+ $ schema ['namespace ' ] = $ namespace ;
36+ }
37+
3338 foreach ($ shorthand as $ property => $ shorthandDefinition ) {
3439 if (! \is_string ($ property ) || empty ($ property )) {
3540 throw InvalidShorthand::emptyString ($ shorthand );
@@ -100,10 +105,6 @@ private static function convertShorthandStringToJsonSchema(string $shorthandStr)
100105
101106 $ parts = \explode ('| ' , $ shorthandStr );
102107
103- if ($ parts [0 ] === 'enum ' ) {
104- return ['enum ' => \array_slice ($ parts , 1 )];
105- }
106-
107108 if (\mb_substr ($ parts [0 ], -2 ) === '[] ' ) {
108109 $ itemsParts = [\mb_substr ($ parts [0 ], 0 , -2 )];
109110 \array_push ($ itemsParts , ...\array_slice ($ parts , 1 ));
@@ -114,39 +115,63 @@ private static function convertShorthandStringToJsonSchema(string $shorthandStr)
114115 ];
115116 }
116117
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 :
122124 $ 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+ }
123132
124133 if (isset ($ parts [1 ]) && $ parts [1 ] === 'null ' ) {
125- $ type = [$ type , 'null ' ];
134+ $ typeValue = [$ type , 'null ' ];
126135
127136 \array_splice ($ parts , 1 , 1 );
128137 }
129138
130- $ schema = ['type ' => $ type ];
139+ $ schema = self ::populateSchema ($ parts );
140+ $ schema [$ typeKey ] = $ typeValue ;
131141
132- if (\count ($ parts ) > 1 ) {
133- $ parts = \array_slice ($ parts , 1 );
142+ return $ schema ;
143+ default :
144+ $ type = $ parts [0 ];
134145
135- foreach ($ parts as $ part ) {
136- [$ validationKey , $ validationValue ] = self ::parseShorthandValidation ($ part );
146+ $ schema = self ::populateSchema ($ parts );
137147
138- $ schema [$ validationKey ] = $ validationValue ;
139- }
140- }
148+ $ schema ['$ref ' ] = '#/definitions/ ' .$ type ;
141149
142150 return $ schema ;
143- default :
144- return [
145- '$ref ' => '#/definitions/ ' .$ parts [0 ],
146- ];
147151 }
148152 }
149153
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+
150175 /**
151176 * @param string $shorthandValidation
152177 * @return array<mixed>
0 commit comments