1010
1111namespace OpenCodeModeling \JsonSchemaToPhp \Shorthand ;
1212
13- use LogicException ;
13+ use OpenCodeModeling \JsonSchemaToPhp \Exception \InvalidShorthand ;
14+ use OpenCodeModeling \JsonSchemaToPhp \Exception \LogicException ;
1415
1516final class Shorthand
1617{
@@ -31,29 +32,20 @@ public static function convertToJsonSchema(array $shorthand): array
3132
3233 foreach ($ shorthand as $ property => $ shorthandDefinition ) {
3334 if (! \is_string ($ property ) || empty ($ property )) {
34- throw new LogicException (\sprintf (
35- 'Shorthand %s contains an empty or non string property. Cannot deal with that! ' ,
36- \json_encode ($ shorthand )
37- ));
35+ throw InvalidShorthand::emptyString ($ shorthand );
3836 }
3937
4038 $ schemaProperty = $ property ;
4139
4240 if (\mb_substr ($ property , -1 ) === '? ' ) {
43- $ schemaProperty = \mb_substr ($ property , 0 , \strlen ( $ property ) - 1 );
41+ $ schemaProperty = \mb_substr ($ property , 0 , - 1 );
4442 } elseif ($ schemaProperty === '$ref ' ) {
4543 if (\count ($ shorthand ) > 1 ) {
46- throw new LogicException (\sprintf (
47- 'Shorthand %s contains a top level ref property "$ref", but it is not the only property!
48- \nA top level reference cannot have other properties then "$ref". ' ,
49- \json_encode ($ shorthand )
50- ));
44+ throw InvalidShorthand::refWithOtherProperties ($ shorthand );
5145 }
5246
5347 if (! \is_string ($ shorthandDefinition )) {
54- throw new LogicException (\sprintf (
55- 'Detected a top level shorthand reference using a "$ref" property, but the value of the property is not a string. ' ,
56- ));
48+ throw InvalidShorthand::refNotString ($ shorthand );
5749 }
5850
5951 $ shorthandDefinition = \str_replace ('#/definitions/ ' , '' , $ shorthandDefinition );
@@ -63,17 +55,11 @@ public static function convertToJsonSchema(array $shorthand): array
6355 ];
6456 } elseif ($ schemaProperty === '$items ' ) {
6557 if (\count ($ shorthand ) > 1 ) {
66- throw new LogicException (\sprintf (
67- 'Shorthand %s contains a top level array property "$items", but it is not the only property!
68- \nA top level array cannot have other properties then "$items". ' ,
69- \json_encode ($ shorthand )
70- ));
58+ throw InvalidShorthand::itemsWithOtherProperties ($ shorthand );
7159 }
7260
7361 if (! \is_string ($ shorthandDefinition )) {
74- throw new LogicException (\sprintf (
75- 'Detected a top level shorthand array using an "$items" property, but the value of the property is not a string. ' ,
76- ));
62+ throw InvalidShorthand::itemsNotString ($ shorthand );
7763 }
7864
7965 if (\mb_substr ($ shorthandDefinition , -2 ) !== '[] ' ) {
@@ -93,10 +79,7 @@ public static function convertToJsonSchema(array $shorthand): array
9379 } elseif (\is_string ($ shorthandDefinition )) {
9480 $ schema ['properties ' ][$ schemaProperty ] = self ::convertShorthandStringToJsonSchema ($ shorthandDefinition );
9581 } else {
96- throw new LogicException (\sprintf (
97- 'I tried to parse JSONSchema for property: "%s", but it is neither a string nor an object. ' ,
98- $ schemaProperty
99- ));
82+ throw InvalidShorthand::cannotParseProperty ($ schemaProperty , $ shorthand );
10083 }
10184 }
10285
@@ -120,7 +103,7 @@ private static function convertShorthandStringToJsonSchema(string $shorthandStr)
120103 }
121104
122105 if (\mb_substr ($ parts [0 ], -2 ) === '[] ' ) {
123- $ itemsParts = [\mb_substr ($ parts [0 ], 0 , \mb_strlen ( $ parts [ 0 ]) - 2 )];
106+ $ itemsParts = [\mb_substr ($ parts [0 ], 0 , - 2 )];
124107 \array_push ($ itemsParts , ...\array_slice ($ parts , 1 ));
125108
126109 return [
0 commit comments