Skip to content

Commit 5b1c551

Browse files
committed
Introduce OneOfType, AllOfType, AnyOfType, ConstType, MixedType and NotType
1 parent eb4f096 commit 5b1c551

File tree

8 files changed

+363
-5
lines changed

8 files changed

+363
-5
lines changed

src/Type/AllOfType.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
final class AllOfType extends OfType
14+
{
15+
public static function type(): string
16+
{
17+
return 'allOf';
18+
}
19+
}

src/Type/AnyOfType.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
final class AnyOfType extends OfType
14+
{
15+
public static function type(): string
16+
{
17+
return 'anyOf';
18+
}
19+
}

src/Type/ConstType.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
class ConstType implements TypeDefinition
14+
{
15+
private ?string $name = null;
16+
17+
/**
18+
* @var mixed
19+
*/
20+
private $value;
21+
22+
final private function __construct()
23+
{
24+
}
25+
26+
public function isNullable(): bool
27+
{
28+
return false;
29+
}
30+
31+
public function isRequired(): bool
32+
{
33+
return false;
34+
}
35+
36+
public function name(): ?string
37+
{
38+
return $this->name;
39+
}
40+
41+
public function setName(?string $name): void
42+
{
43+
$this->name = $name;
44+
}
45+
46+
public static function fromDefinition(array $definition, ?string $name = null): self
47+
{
48+
$self = new static();
49+
$self->name = $name;
50+
$self->value = $definition['const'];
51+
52+
return $self;
53+
}
54+
55+
/**
56+
* @return mixed
57+
*/
58+
public function value()
59+
{
60+
return $this->value;
61+
}
62+
63+
public static function type(): string
64+
{
65+
return 'const';
66+
}
67+
}

src/Type/MixedType.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
/**
14+
* @internal
15+
*/
16+
class MixedType implements TypeDefinition, RequiredAware
17+
{
18+
private ?string $name = null;
19+
protected bool $isRequired = false;
20+
21+
final private function __construct()
22+
{
23+
}
24+
25+
public function isNullable(): bool
26+
{
27+
return false;
28+
}
29+
30+
public function isRequired(): bool
31+
{
32+
return false;
33+
}
34+
35+
public function name(): ?string
36+
{
37+
return $this->name;
38+
}
39+
40+
public function setName(?string $name): void
41+
{
42+
$this->name = $name;
43+
}
44+
45+
public static function fromDefinition(array $definition, ?string $name = null): self
46+
{
47+
$self = new static();
48+
$self->name = $name;
49+
50+
return $self;
51+
}
52+
53+
public function setIsRequired(bool $required): void
54+
{
55+
$this->isRequired = $required;
56+
}
57+
58+
public static function type(): string
59+
{
60+
return 'mixed';
61+
}
62+
}

src/Type/NotType.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
final class NotType implements TypeDefinition
14+
{
15+
private ?string $name = null;
16+
17+
private TypeSet $typeSet;
18+
19+
private function __construct()
20+
{
21+
}
22+
23+
public function isNullable(): bool
24+
{
25+
return false;
26+
}
27+
28+
public function isRequired(): bool
29+
{
30+
return false;
31+
}
32+
33+
public function name(): ?string
34+
{
35+
return $this->name;
36+
}
37+
38+
public function setName(?string $name): void
39+
{
40+
$this->name = $name;
41+
}
42+
43+
public static function fromDefinition(array $definition, ?string $name = null): self
44+
{
45+
$self = new static();
46+
$self->name = $name;
47+
$self->typeSet = Type::fromDefinition($definition['not']);
48+
49+
return $self;
50+
}
51+
52+
public function getTypeSet(): TypeSet
53+
{
54+
return $this->typeSet;
55+
}
56+
57+
public static function type(): string
58+
{
59+
return 'not';
60+
}
61+
}

src/Type/OfType.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
/**
14+
* @internal
15+
*/
16+
abstract class OfType implements TypeDefinition
17+
{
18+
private ?string $name = null;
19+
20+
/**
21+
* @var TypeSet[]
22+
*/
23+
private array $typeSets = [];
24+
25+
final private function __construct()
26+
{
27+
}
28+
29+
public function isNullable(): bool
30+
{
31+
return false;
32+
}
33+
34+
public function isRequired(): bool
35+
{
36+
return false;
37+
}
38+
39+
public function name(): ?string
40+
{
41+
return $this->name;
42+
}
43+
44+
public function setName(?string $name): void
45+
{
46+
$this->name = $name;
47+
}
48+
49+
public static function fromDefinition(array $definition, ?string $name = null): self
50+
{
51+
$self = new static();
52+
$self->name = $name;
53+
54+
$type = $definition['type'] ?: static::type();
55+
56+
foreach ($definition[$type] as $typeDefinition) {
57+
$self->typeSets[] = Type::fromDefinition($typeDefinition);
58+
}
59+
60+
return $self;
61+
}
62+
63+
/**
64+
* @return TypeSet[]
65+
*/
66+
public function getTypeSets(): array
67+
{
68+
return $this->typeSets;
69+
}
70+
}

src/Type/OneOfType.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
final class OneOfType extends OfType
14+
{
15+
public static function type(): string
16+
{
17+
return 'oneOf';
18+
}
19+
}

src/Type/Type.php

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,35 @@ public static function fromShorthand(array $shorthand, ?string $name = null): Ty
3232
public static function fromDefinition(array $definition, ?string $name = null): TypeSet
3333
{
3434
if (! isset($definition['type'])) {
35-
if (isset($definition['$ref'])) {
36-
return new TypeSet(ReferenceType::fromDefinition($definition, $name));
35+
switch (true) {
36+
case isset($definition['$ref']):
37+
return new TypeSet(ReferenceType::fromDefinition($definition, $name));
38+
case isset($definition['anyOf']):
39+
$definition['type'] = AnyOfType::type();
40+
break;
41+
case isset($definition['oneOf']):
42+
$definition['type'] = OneOfType::type();
43+
break;
44+
case isset($definition['allOf']):
45+
$definition['type'] = AllOfType::type();
46+
break;
47+
case isset($definition['not']):
48+
$definition['type'] = NotType::type();
49+
break;
50+
case isset($definition['const']):
51+
$definition['type'] = ConstType::type();
52+
break;
53+
case isset($definition['patternProperties'])
54+
|| isset($definition['properties'])
55+
|| isset($definition['additionalProperties'])
56+
|| isset($definition['required']):
57+
$definition['type'] = ObjectType::type();
58+
break;
59+
case \count($definition) === 0:
60+
return new TypeSet(MixedType::fromDefinition($definition, $name));
61+
default:
62+
throw new \RuntimeException(\sprintf('The "type" is missing in schema definition for "%s"', $name));
3763
}
38-
39-
throw new \RuntimeException(\sprintf('The "type" is missing in schema definition for "%s"', $name));
4064
}
4165

4266
$definitionTypes = (array) $definition['type'];
@@ -67,6 +91,21 @@ public static function fromDefinition(array $definition, ?string $name = null):
6791
case 'array':
6892
$types[] = ArrayType::fromDefinition($definition, $name);
6993
break;
94+
case 'oneOf':
95+
$types[] = OneOfType::fromDefinition($definition, $name);
96+
break;
97+
case 'anyOf':
98+
$types[] = AnyOfType::fromDefinition($definition, $name);
99+
break;
100+
case 'allOf':
101+
$types[] = AllOfType::fromDefinition($definition, $name);
102+
break;
103+
case 'not':
104+
$types[] = NotType::fromDefinition($definition, $name);
105+
break;
106+
case 'const':
107+
$types[] = ConstType::fromDefinition($definition, $name);
108+
break;
70109
case 'null':
71110
case 'Null':
72111
case 'NULL':
@@ -84,7 +123,9 @@ public static function fromDefinition(array $definition, ?string $name = null):
84123
}
85124

86125
foreach ($types as $type) {
87-
$type->setNullable($isNullable);
126+
if ($type instanceof NullableAware) {
127+
$type->setNullable($isNullable);
128+
}
88129
}
89130

90131
return new TypeSet(...$types);

0 commit comments

Comments
 (0)