Skip to content

Commit 80ee8dc

Browse files
committed
enable multiple types for enum
1 parent dc8830d commit 80ee8dc

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

src/Type/EnumType.php

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@ class EnumType implements AnnotatedType
2020
use NullableType,
2121
HasAnnotations;
2222

23+
/**
24+
* @var string[]
25+
*/
26+
private const VALID_TYPES = [
27+
JsonSchema::TYPE_STRING,
28+
JsonSchema::TYPE_INT,
29+
JsonSchema::TYPE_FLOAT,
30+
];
31+
2332
/**
2433
* @var string|array
2534
*/
2635
private $type = JsonSchema::TYPE_STRING;
2736

2837
/**
29-
* @var string[]
38+
* @var string[]|int[]|float[]
3039
*/
3140
private $entries;
3241

@@ -54,4 +63,71 @@ public function asNullable(): Type
5463

5564
return $cp;
5665
}
66+
67+
public function setType(string $type) : Type
68+
{
69+
$cp = clone $this;
70+
71+
$nullable = is_array($cp->type) && in_array(JsonSchema::TYPE_NULL, $cp->type);
72+
73+
$cp->type = $nullable ? [$type, JsonSchema::TYPE_NULL] : $type;
74+
$cp->entries = $this->castEntries($type);
75+
76+
return $cp;
77+
}
78+
79+
private function castEntries(string $type)
80+
{
81+
$castCallback = $this->castCallback($type);
82+
83+
return array_map($castCallback, $this->entries);
84+
}
85+
86+
private function castCallback(string $type)
87+
{
88+
$this->typeIsValid($type);
89+
90+
switch ($type) {
91+
case JsonSchema::TYPE_INT:
92+
return static function ($entry) {
93+
if ($entry === null) {
94+
return $entry;
95+
}
96+
97+
return intval($entry);
98+
};
99+
case JsonSchema::TYPE_STRING:
100+
return static function ($entry) {
101+
if ($entry === null) {
102+
return $entry;
103+
}
104+
105+
return (string) $entry;
106+
};
107+
case JsonSchema::TYPE_FLOAT:
108+
return static function ($entry) {
109+
if ($entry === null) {
110+
return $entry;
111+
}
112+
113+
return floatval($entry);
114+
};
115+
}
116+
}
117+
118+
private function typeIsValid(string $type): bool
119+
{
120+
if (!in_array($type, self::VALID_TYPES)) {
121+
throw new \InvalidArgumentException(
122+
sprintf(
123+
'Invalid schema type %s',
124+
$type
125+
)
126+
);
127+
}
128+
129+
return true;
130+
}
131+
132+
57133
}

tests/Type/EnumTypeTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace EventEngineTest\JsonSchema\Type;
1313

14+
use EventEngine\JsonSchema\JsonSchema;
1415
use EventEngine\JsonSchema\Type\EnumType;
1516
use EventEngineTest\JsonSchema\BasicTestCase;
1617

@@ -47,4 +48,40 @@ public function it_creates_nullable_enum_type()
4748
$enumType->toArray()
4849
);
4950
}
51+
52+
53+
/**
54+
* @test
55+
*/
56+
public function it_creates_adjustable_enum_type()
57+
{
58+
$enumType = (new EnumType('0', '1'))->setType(JsonSchema::TYPE_INT);
59+
60+
$this->assertEquals(
61+
[
62+
'type' => 'integer',
63+
'enum' => [0, 1],
64+
],
65+
$enumType->toArray()
66+
);
67+
}
68+
69+
70+
/**
71+
* @test
72+
*/
73+
public function it_creates_adjustable_nullable_enum_type()
74+
{
75+
$enumType = (new EnumType('0', '1'))
76+
->asNullable()
77+
->setType(JsonSchema::TYPE_INT);
78+
79+
$this->assertEquals(
80+
[
81+
'type' => ['integer', 'null'],
82+
'enum' => [0, 1, null],
83+
],
84+
$enumType->toArray()
85+
);
86+
}
5087
}

0 commit comments

Comments
 (0)