Skip to content

Commit 615b660

Browse files
committed
enable multiple types for enum
1 parent dc8830d commit 615b660

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

src/Type/EnumType.php

Lines changed: 63 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,57 @@ public function asNullable(): Type
5463

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

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+
->setType(JsonSchema::TYPE_INT)
77+
->asNullable();
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)