Skip to content

Commit d3c210a

Browse files
committed
Introduce exception classes
1 parent 4b81071 commit d3c210a

File tree

6 files changed

+185
-29
lines changed

6 files changed

+185
-29
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
}
3232
},
3333
"require": {
34-
"php": "^7.4 || ^8.0",
35-
"ext-json": "*"
34+
"php": "^7.4 || ^8.0"
3635
},
3736
"require-dev": {
3837
"jangregor/phpstan-prophecy": "^0.8.0",

src/Exception/ExceptionInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/json-schema-to-php for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/json-schema-to-php/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/json-schema-to-php/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types = 1);
10+
11+
namespace OpenCodeModeling\JsonSchemaToPhp\Exception;
12+
13+
/**
14+
* Base exception interface
15+
*
16+
* All exceptions must implements this exception to catch exceptions of this library
17+
*/
18+
interface ExceptionInterface
19+
{
20+
}

src/Exception/InvalidShorthand.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/json-schema-to-php for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/json-schema-to-php/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/json-schema-to-php/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\JsonSchemaToPhp\Exception;
12+
13+
final class InvalidShorthand extends LogicException
14+
{
15+
/**
16+
* @var array<string, mixed>
17+
*/
18+
private array $shorthand;
19+
20+
/**
21+
* @param string $schemaProperty
22+
* @param array<string, mixed> $shorthand
23+
* @return static
24+
*/
25+
public static function cannotParseProperty(string $schemaProperty, array $shorthand): self
26+
{
27+
$self = new static(
28+
\sprintf(
29+
'I tried to parse JSONSchema for property: "%s", but it is neither a string nor an object.',
30+
$schemaProperty
31+
)
32+
);
33+
$self->shorthand = $shorthand;
34+
35+
return $self;
36+
}
37+
38+
/**
39+
* @param array<string, mixed> $shorthand $shorthand
40+
* @return static
41+
*/
42+
public static function emptyString(array $shorthand): self
43+
{
44+
$self = new static('Shorthand contains an empty or non string property. Cannot deal with that!');
45+
$self->shorthand = $shorthand;
46+
47+
return $self;
48+
}
49+
50+
/**
51+
* @param array<string, mixed> $shorthand $shorthand
52+
* @return static
53+
*/
54+
public static function refNotString(array $shorthand): self
55+
{
56+
$self = new static(
57+
'Detected a top level shorthand reference using a "$ref" property, but the value of the property is not a string.'
58+
);
59+
$self->shorthand = $shorthand;
60+
61+
return $self;
62+
}
63+
64+
/**
65+
* @param array<string, mixed> $shorthand $shorthand
66+
* @return static
67+
*/
68+
public static function itemsNotString(array $shorthand): self
69+
{
70+
$self = new static(
71+
'Detected a top level shorthand array using an "$items" property, but the value of the property is not a string.'
72+
);
73+
$self->shorthand = $shorthand;
74+
75+
return $self;
76+
}
77+
78+
/**
79+
* @param array<string, mixed> $shorthand $shorthand
80+
* @return static
81+
*/
82+
public static function refWithOtherProperties(array $shorthand): self
83+
{
84+
$self = new static(
85+
'Shorthand contains a top level ref property "$ref", but it is not the only property!'
86+
. ' A top level reference cannot have other properties then "$ref".'
87+
);
88+
$self->shorthand = $shorthand;
89+
90+
return $self;
91+
}
92+
93+
/**
94+
* @param array<string, mixed> $shorthand $shorthand
95+
* @return static
96+
*/
97+
public static function itemsWithOtherProperties(array $shorthand): self
98+
{
99+
$self = new static(
100+
'Shorthand %s contains a top level array property "$items", but it is not the only property!'
101+
. ' A top level array cannot have other properties then "$items".'
102+
);
103+
$self->shorthand = $shorthand;
104+
105+
return $self;
106+
}
107+
108+
/**
109+
* @return array<string, mixed>
110+
*/
111+
public function shorthand(): array
112+
{
113+
return $this->shorthand;
114+
}
115+
}

src/Exception/LogicException.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/json-schema-to-php for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/json-schema-to-php/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/json-schema-to-php/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types = 1);
10+
11+
namespace OpenCodeModeling\JsonSchemaToPhp\Exception;
12+
13+
use LogicException as PhpLogicException;
14+
15+
class LogicException extends PhpLogicException implements ExceptionInterface
16+
{
17+
}

src/Exception/RuntimeException.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/json-schema-to-php for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/json-schema-to-php/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/json-schema-to-php/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types = 1);
10+
11+
namespace OpenCodeModeling\JsonSchemaToPhp\Exception;
12+
13+
use RuntimeException as PhpRuntimeException;
14+
15+
/**
16+
* Runtime exception
17+
*
18+
* Use this exception if the code has not the capacity to handle the request.
19+
*/
20+
class RuntimeException extends PhpRuntimeException implements ExceptionInterface
21+
{
22+
}

src/Shorthand/Shorthand.php

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
namespace OpenCodeModeling\JsonSchemaToPhp\Shorthand;
1212

13-
use LogicException;
13+
use OpenCodeModeling\JsonSchemaToPhp\Exception\InvalidShorthand;
14+
use OpenCodeModeling\JsonSchemaToPhp\Exception\LogicException;
1415

1516
final 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

Comments
 (0)