1
1
<?php
2
- declare (strict_types=1 );
3
2
4
3
/**
5
4
* @see https://github.com/open-code-modeling/json-schema-to-php for the canonical source repository
6
5
* @copyright https://github.com/open-code-modeling/json-schema-to-php/blob/master/COPYRIGHT.md
7
6
* @license https://github.com/open-code-modeling/json-schema-to-php/blob/master/LICENSE.md MIT License
8
7
*/
9
8
9
+ declare (strict_types=1 );
10
+
10
11
namespace OpenCodeModeling \JsonSchemaToPhp \Shorthand ;
11
12
12
13
use LogicException ;
13
- use function array_push ;
14
- use function array_slice ;
15
- use function array_splice ;
16
- use function count ;
17
- use function explode ;
18
- use function floatval ;
19
- use function implode ;
20
- use function intval ;
21
- use function is_array ;
22
- use function is_string ;
23
- use function json_encode ;
24
- use function mb_strlen ;
25
- use function mb_substr ;
26
- use function sprintf ;
27
- use function str_replace ;
28
- use function strlen ;
29
14
30
15
final class Shorthand
31
16
{
17
+ /**
18
+ * @param array<string, mixed> $shorthand
19
+ * @return array<string, mixed>
20
+ */
32
21
public static function convertToJsonSchema (array $ shorthand ): array
33
22
{
34
23
$ schema = [
@@ -41,70 +30,70 @@ public static function convertToJsonSchema(array $shorthand): array
41
30
];
42
31
43
32
foreach ($ shorthand as $ property => $ shorthandDefinition ) {
44
- if (! is_string ($ property ) || empty ($ property )) {
45
- throw new LogicException (sprintf (
33
+ if (! \ is_string ($ property ) || empty ($ property )) {
34
+ throw new LogicException (\ sprintf (
46
35
'Shorthand %s contains an empty or non string property. Cannot deal with that! ' ,
47
- json_encode ($ shorthand )
36
+ \ json_encode ($ shorthand )
48
37
));
49
38
}
50
39
51
40
$ schemaProperty = $ property ;
52
41
53
- if ( mb_substr ($ property , -1 ) === '? ' ) {
54
- $ schemaProperty = mb_substr ($ property , 0 , strlen ($ property ) - 1 );
55
- } else if ($ schemaProperty === '$ref ' ) {
56
- if ( count ($ shorthand ) > 1 ) {
57
- throw new LogicException (sprintf (
42
+ if ( \ mb_substr ($ property , -1 ) === '? ' ) {
43
+ $ schemaProperty = \ mb_substr ($ property , 0 , \ strlen ($ property ) - 1 );
44
+ } elseif ($ schemaProperty === '$ref ' ) {
45
+ if ( \ count ($ shorthand ) > 1 ) {
46
+ throw new LogicException (\ sprintf (
58
47
'Shorthand %s contains a top level ref property "$ref", but it is not the only property!
59
48
\nA top level reference cannot have other properties then "$ref". ' ,
60
- json_encode ($ shorthand )
49
+ \ json_encode ($ shorthand )
61
50
));
62
51
}
63
52
64
- if (! is_string ($ shorthandDefinition )) {
65
- throw new LogicException (sprintf (
53
+ if (! \ is_string ($ shorthandDefinition )) {
54
+ throw new LogicException (\ sprintf (
66
55
'Detected a top level shorthand reference using a "$ref" property, but the value of the property is not a string. ' ,
67
56
));
68
57
}
69
58
70
- $ shorthandDefinition = str_replace ('#/definitions/ ' , '' , $ shorthandDefinition );
59
+ $ shorthandDefinition = \ str_replace ('#/definitions/ ' , '' , $ shorthandDefinition );
71
60
72
61
return [
73
- '$ref ' => "#/definitions/ $ shorthandDefinition "
62
+ '$ref ' => "#/definitions/ $ shorthandDefinition " ,
74
63
];
75
- } else if ($ schemaProperty === '$items ' ) {
76
- if ( count ($ shorthand ) > 1 ) {
77
- throw new LogicException (sprintf (
64
+ } elseif ($ schemaProperty === '$items ' ) {
65
+ if ( \ count ($ shorthand ) > 1 ) {
66
+ throw new LogicException (\ sprintf (
78
67
'Shorthand %s contains a top level array property "$items", but it is not the only property!
79
68
\nA top level array cannot have other properties then "$items". ' ,
80
- json_encode ($ shorthand )
69
+ \ json_encode ($ shorthand )
81
70
));
82
71
}
83
72
84
- if (! is_string ($ shorthandDefinition )) {
85
- throw new LogicException (sprintf (
73
+ if (! \ is_string ($ shorthandDefinition )) {
74
+ throw new LogicException (\ sprintf (
86
75
'Detected a top level shorthand array using an "$items" property, but the value of the property is not a string. ' ,
87
76
));
88
77
}
89
78
90
- if ( mb_substr ($ shorthandDefinition , -2 ) !== '[] ' ) {
79
+ if ( \ mb_substr ($ shorthandDefinition , -2 ) !== '[] ' ) {
91
80
$ shorthandDefinition .= '[] ' ;
92
81
}
93
82
94
83
return self ::convertShorthandStringToJsonSchema ($ shorthandDefinition );
95
- } else if ($ schemaProperty === '$title ' ) {
84
+ } elseif ($ schemaProperty === '$title ' ) {
96
85
$ schema ['title ' ] = $ shorthandDefinition ;
97
86
continue ;
98
87
} else {
99
88
$ schema ['required ' ][] = $ schemaProperty ;
100
89
}
101
90
102
- if ( is_array ($ shorthandDefinition )) {
91
+ if ( \ is_array ($ shorthandDefinition )) {
103
92
$ schema ['properties ' ][$ schemaProperty ] = self ::convertToJsonSchema ($ shorthandDefinition );
104
- } else if ( is_string ($ shorthandDefinition )) {
93
+ } elseif ( \ is_string ($ shorthandDefinition )) {
105
94
$ schema ['properties ' ][$ schemaProperty ] = self ::convertShorthandStringToJsonSchema ($ shorthandDefinition );
106
95
} else {
107
- throw new LogicException (sprintf (
96
+ throw new LogicException (\ sprintf (
108
97
'I tried to parse JSONSchema for property: "%s", but it is neither a string nor an object. ' ,
109
98
$ schemaProperty
110
99
));
@@ -114,25 +103,29 @@ public static function convertToJsonSchema(array $shorthand): array
114
103
return $ schema ;
115
104
}
116
105
106
+ /**
107
+ * @param string $shorthandStr
108
+ * @return array<string, mixed>
109
+ */
117
110
private static function convertShorthandStringToJsonSchema (string $ shorthandStr ): array
118
111
{
119
- if ($ shorthandStr === '' ) {
112
+ if ($ shorthandStr === '' ) {
120
113
return ['type ' => 'string ' ];
121
114
}
122
115
123
- $ parts = explode ('| ' , $ shorthandStr );
116
+ $ parts = \ explode ('| ' , $ shorthandStr );
124
117
125
- if ($ parts [0 ] === 'enum ' ) {
126
- return ['enum ' => array_slice ($ parts , 1 )];
118
+ if ($ parts [0 ] === 'enum ' ) {
119
+ return ['enum ' => \ array_slice ($ parts , 1 )];
127
120
}
128
121
129
- if ( mb_substr ($ parts [0 ], -2 ) === '[] ' ) {
130
- $ itemsParts = [mb_substr ($ parts [0 ], 0 , mb_strlen ($ parts [0 ]) - 2 )];
131
- array_push ($ itemsParts , ...array_slice ($ parts , 1 ));
122
+ if ( \ mb_substr ($ parts [0 ], -2 ) === '[] ' ) {
123
+ $ itemsParts = [\ mb_substr ($ parts [0 ], 0 , \ mb_strlen ($ parts [0 ]) - 2 )];
124
+ \ array_push ($ itemsParts , ...\ array_slice ($ parts , 1 ));
132
125
133
126
return [
134
127
'type ' => 'array ' ,
135
- 'items ' => self ::convertShorthandStringToJsonSchema (implode ('| ' , $ itemsParts )),
128
+ 'items ' => self ::convertShorthandStringToJsonSchema (\ implode ('| ' , $ itemsParts )),
136
129
];
137
130
}
138
131
@@ -143,16 +136,16 @@ private static function convertShorthandStringToJsonSchema(string $shorthandStr)
143
136
case 'boolean ' :
144
137
$ type = $ parts [0 ];
145
138
146
- if (isset ($ parts [1 ]) && $ parts [1 ] === 'null ' ) {
139
+ if (isset ($ parts [1 ]) && $ parts [1 ] === 'null ' ) {
147
140
$ type = [$ type , 'null ' ];
148
141
149
- array_splice ($ parts , 1 , 1 );
142
+ \ array_splice ($ parts , 1 , 1 );
150
143
}
151
144
152
145
$ schema = ['type ' => $ type ];
153
146
154
- if ( count ($ parts ) > 1 ) {
155
- $ parts = array_slice ($ parts , 1 );
147
+ if ( \ count ($ parts ) > 1 ) {
148
+ $ parts = \ array_slice ($ parts , 1 );
156
149
157
150
foreach ($ parts as $ part ) {
158
151
[$ validationKey , $ validationValue ] = self ::parseShorthandValidation ($ part );
@@ -169,33 +162,37 @@ private static function convertShorthandStringToJsonSchema(string $shorthandStr)
169
162
}
170
163
}
171
164
165
+ /**
166
+ * @param string $shorthandValidation
167
+ * @return array<mixed>
168
+ */
172
169
private static function parseShorthandValidation (string $ shorthandValidation ): array
173
170
{
174
- $ parts = explode (': ' , $ shorthandValidation );
171
+ $ parts = \ explode (': ' , $ shorthandValidation );
175
172
176
- if ( count ($ parts ) !== 2 ) {
177
- throw new LogicException (sprintf (
173
+ if ( \ count ($ parts ) !== 2 ) {
174
+ throw new LogicException (\ sprintf (
178
175
'Cannot parse shorthand validation: "%s". Expected format "validationKey:value". Please check again! ' ,
179
176
$ shorthandValidation
180
177
));
181
178
}
182
179
183
180
[$ validationKey , $ value ] = $ parts ;
184
181
185
- if ($ value === 'true ' ) {
182
+ if ($ value === 'true ' ) {
186
183
return [$ validationKey , true ];
187
184
}
188
185
189
- if ($ value === 'false ' ) {
186
+ if ($ value === 'false ' ) {
190
187
return [$ validationKey , false ];
191
188
}
192
189
193
- if ((string )intval ( $ value) === $ value ) {
194
- return [$ validationKey , (int )$ value ];
190
+ if ((string ) ( int ) $ value === $ value ) {
191
+ return [$ validationKey , (int ) $ value ];
195
192
}
196
193
197
- if ((string )floatval ( $ value) === $ value ) {
198
- return [$ validationKey , (float )$ value ];
194
+ if ((string ) ( float ) $ value === $ value ) {
195
+ return [$ validationKey , (float ) $ value ];
199
196
}
200
197
201
198
return [$ validationKey , $ value ];
0 commit comments