Skip to content

Commit 0e21375

Browse files
committed
Add support for arbitrary data in JSON schema - #9
1 parent 70f728f commit 0e21375

15 files changed

+216
-31
lines changed

src/Type/ArrayType.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace OpenCodeModeling\JsonSchemaToPhp\Type;
1212

13-
final class ArrayType implements TypeDefinition, TitleAware
13+
final class ArrayType implements TypeDefinition, TitleAware, CustomSupport
1414
{
1515
use PopulateRequired;
1616

@@ -38,6 +38,11 @@ final class ArrayType implements TypeDefinition, TitleAware
3838
protected bool $nullable = false;
3939
protected ?string $title = null;
4040

41+
/**
42+
* @var array<string, mixed>
43+
*/
44+
protected array $custom = [];
45+
4146
private function __construct()
4247
{
4348
}
@@ -134,11 +139,14 @@ public static function fromDefinition(array $definition, ?string $name = null, a
134139
$self->additionalItems = Type::fromDefinition($definitionValue, '', $self->definitions());
135140
break;
136141
case 'definitions':
142+
case 'type':
137143
// handled beforehand
138144
break;
139145
default:
140146
if (\property_exists($self, $definitionKey)) {
141147
$self->$definitionKey = $definitionValue;
148+
} else {
149+
$self->custom[$definitionKey] = $definitionValue;
142150
}
143151
break;
144152
}
@@ -276,4 +284,14 @@ public static function type(): string
276284
{
277285
return self::TYPE_ARRAY;
278286
}
287+
288+
/**
289+
* Returns custom definitions
290+
*
291+
* @return array<string, mixed>
292+
*/
293+
public function custom(): array
294+
{
295+
return $this->custom;
296+
}
279297
}

src/Type/ConstType.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace OpenCodeModeling\JsonSchemaToPhp\Type;
1212

13-
class ConstType implements TypeDefinition
13+
final class ConstType implements TypeDefinition
1414
{
1515
private ?string $name = null;
1616

@@ -19,10 +19,6 @@ class ConstType implements TypeDefinition
1919
*/
2020
private $value;
2121

22-
final private function __construct()
23-
{
24-
}
25-
2622
public function isNullable(): bool
2723
{
2824
return false;

src/Type/CustomSupport.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Type;
12+
13+
interface CustomSupport
14+
{
15+
/**
16+
* Returns custom definitions
17+
*
18+
* @return array<string, mixed>
19+
*/
20+
public function custom(): array;
21+
}

src/Type/MixedType.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,11 @@
1010

1111
namespace OpenCodeModeling\JsonSchemaToPhp\Type;
1212

13-
/**
14-
* @internal
15-
*/
16-
class MixedType implements TypeDefinition, RequiredAware
13+
final class MixedType implements TypeDefinition, RequiredAware
1714
{
1815
private ?string $name = null;
1916
protected bool $isRequired = false;
2017

21-
final private function __construct()
22-
{
23-
}
24-
2518
public function isNullable(): bool
2619
{
2720
return false;

src/Type/ObjectType.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace OpenCodeModeling\JsonSchemaToPhp\Type;
1212

13-
final class ObjectType implements TypeDefinition, NullableAware, RequiredAware, TitleAware
13+
final class ObjectType implements TypeDefinition, NullableAware, RequiredAware, TitleAware, CustomSupport
1414
{
1515
use PopulateRequired;
1616

@@ -44,6 +44,11 @@ final class ObjectType implements TypeDefinition, NullableAware, RequiredAware,
4444
*/
4545
protected array $definitions = [];
4646

47+
/**
48+
* @var array<string, mixed>
49+
*/
50+
protected array $custom = [];
51+
4752
private function __construct()
4853
{
4954
}
@@ -117,11 +122,14 @@ public static function fromDefinition(array $definition, ?string $name = null, a
117122
: $definitionValue;
118123
break;
119124
case 'definitions':
125+
case 'type':
120126
// handled beforehand
121127
break;
122128
default:
123129
if (\property_exists($self, $definitionKey)) {
124130
$self->$definitionKey = $definitionValue;
131+
} else {
132+
$self->custom[$definitionKey] = $definitionValue;
125133
}
126134
break;
127135
}
@@ -217,4 +225,14 @@ public function definitions(): array
217225
{
218226
return $this->definitions;
219227
}
228+
229+
/**
230+
* Returns custom definitions
231+
*
232+
* @return array<string, mixed>
233+
*/
234+
public function custom(): array
235+
{
236+
return $this->custom;
237+
}
220238
}

src/Type/ReferenceType.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace OpenCodeModeling\JsonSchemaToPhp\Type;
1212

13-
final class ReferenceType implements TypeDefinition, RequiredAware, NullableAware, TitleAware
13+
final class ReferenceType implements TypeDefinition, RequiredAware, NullableAware, TitleAware, CustomSupport
1414
{
1515
protected ?TypeSet $resolvedType = null;
1616
protected ?string $name = null;
@@ -19,6 +19,11 @@ final class ReferenceType implements TypeDefinition, RequiredAware, NullableAwar
1919
protected bool $nullable = false;
2020
protected ?string $title = null;
2121

22+
/**
23+
* @var array<string, mixed>
24+
*/
25+
protected array $custom = [];
26+
2227
private function __construct()
2328
{
2429
}
@@ -39,6 +44,8 @@ public static function fromDefinition(array $definition, ?string $name = null):
3944
foreach ($definition as $definitionKey => $definitionValue) {
4045
if (\property_exists($self, $definitionKey)) {
4146
$self->$definitionKey = $definitionValue;
47+
} elseif ($definitionKey !== '$ref') {
48+
$self->custom[$definitionKey] = $definitionValue;
4249
}
4350
}
4451

@@ -125,4 +132,14 @@ public function setTitle(string $title): void
125132
{
126133
$this->title = $title;
127134
}
135+
136+
/**
137+
* Returns custom definitions
138+
*
139+
* @return array<string, mixed>
140+
*/
141+
public function custom(): array
142+
{
143+
return $this->custom;
144+
}
128145
}

src/Type/ScalarType.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@
1313
/**
1414
* @internal
1515
*/
16-
abstract class ScalarType implements TypeDefinition, RequiredAware, NullableAware, TitleAware
16+
abstract class ScalarType implements TypeDefinition, RequiredAware, NullableAware, TitleAware, CustomSupport
1717
{
1818
protected ?string $format = null;
1919
protected ?string $name = null;
2020
protected bool $isRequired = false;
2121
protected bool $nullable = false;
2222
protected ?string $title = null;
2323

24+
/**
25+
* @var array<string, mixed>
26+
*/
27+
protected array $custom = [];
28+
2429
/**
2530
* @var mixed
2631
*/
@@ -60,6 +65,8 @@ public static function fromDefinition(array $definition, ?string $name = null):
6065
foreach ($definition as $definitionKey => $definitionValue) {
6166
if (\property_exists($self, $definitionKey)) {
6267
$self->$definitionKey = $definitionValue;
68+
} elseif ($definitionKey !== 'type') {
69+
$self->custom[$definitionKey] = $definitionValue;
6370
}
6471
}
6572

@@ -160,4 +167,14 @@ public function setTitle(string $title): void
160167
{
161168
$this->title = $title;
162169
}
170+
171+
/**
172+
* Returns custom definitions
173+
*
174+
* @return array<string, mixed>
175+
*/
176+
public function custom(): array
177+
{
178+
return $this->custom;
179+
}
163180
}

src/Type/Type.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ final class Type
1818
/**
1919
* @param array<string, mixed> $shorthand
2020
* @param string|null $name
21+
* @param string|null $namespace
2122
* @return TypeSet
2223
*/
23-
public static function fromShorthand(array $shorthand, ?string $name = null): TypeSet
24+
public static function fromShorthand(array $shorthand, ?string $name = null, ?string $namespace = null): TypeSet
2425
{
2526
return self::fromDefinition(Shorthand::convertToJsonSchema($shorthand), $name);
2627
}

tests/Type/ArrayTypeTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ private function assertItemOne(TypeSet $itemOne): void
137137
/** @var NumberType $itemOneType */
138138
$itemOneType = $itemOne->first();
139139
$this->assertInstanceOf(NumberType::class, $itemOneType);
140+
$this->assertSame(['namespace' => 'Scalar'], $itemOneType->custom());
140141
}
141142

142143
private function assertItemTwo(TypeSet $itemTwo): void
@@ -154,6 +155,7 @@ private function assertItemThree(TypeSet $itemThree): void
154155
$address = $itemThree->first();
155156
$this->assertInstanceOf(ReferenceType::class, $address);
156157
$this->assertCount(1, $address->resolvedType());
158+
$this->assertSame(['namespace' => 'Address'], $address->custom());
157159

158160
/** @var ObjectType $resolvedType */
159161
$resolvedType = $address->resolvedType()->first();
@@ -176,6 +178,7 @@ private function assertItemThree(TypeSet $itemThree): void
176178
$this->assertInstanceOf(ReferenceType::class, $state);
177179
$this->assertTrue($state->isRequired());
178180
$this->assertFalse($state->isNullable());
181+
$this->assertSame(['namespace' => 'Address'], $state->custom());
179182

180183
$resolvedTypeSet = $state->resolvedType();
181184
$this->assertCount(1, $resolvedTypeSet);

0 commit comments

Comments
 (0)