Skip to content

Commit a286671

Browse files
authored
Merge pull request #6 from arnedesmedt/multipleEnunTypes
enable multiple types for enum
2 parents dc8830d + b59bbf7 commit a286671

File tree

5 files changed

+93
-4
lines changed

5 files changed

+93
-4
lines changed

src/JsonSchema.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
use EventEngine\JsonSchema\Type\EmailType;
1818
use EventEngine\JsonSchema\Type\EnumType;
1919
use EventEngine\JsonSchema\Type\FloatType;
20+
use EventEngine\JsonSchema\Type\IntEnumType;
2021
use EventEngine\JsonSchema\Type\IntType;
2122
use EventEngine\JsonSchema\Type\ObjectType;
23+
use EventEngine\JsonSchema\Type\StringEnumType;
2224
use EventEngine\JsonSchema\Type\StringType;
2325
use EventEngine\JsonSchema\Type\TypeRef;
2426
use EventEngine\JsonSchema\Type\UnionType;
@@ -105,9 +107,20 @@ public static function boolean(): BoolType
105107
return new BoolType();
106108
}
107109

108-
public static function enum(array $entries): EnumType
110+
public static function enum(array $entries, string $type = JsonSchema::TYPE_STRING): EnumType
109111
{
110-
return new EnumType(...$entries);
112+
switch($type) {
113+
case JsonSchema::TYPE_STRING:
114+
$class = StringEnumType::class;
115+
break;
116+
case JsonSchema::TYPE_INT:
117+
$class = IntEnumType::class;
118+
break;
119+
default:
120+
$class = EnumType::class;
121+
}
122+
123+
return new $class(...$entries);
111124
}
112125

113126
public static function nullOr(Type $schema): Type

src/Type/EnumType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class EnumType implements AnnotatedType
2323
/**
2424
* @var string|array
2525
*/
26-
private $type = JsonSchema::TYPE_STRING;
26+
protected $type = JsonSchema::TYPE_STRING;
2727

2828
/**
2929
* @var string[]
3030
*/
31-
private $entries;
31+
protected $entries;
3232

3333
public function __construct(string ...$entries)
3434
{

src/Type/IntEnumType.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace EventEngine\JsonSchema\Type;
6+
7+
use EventEngine\JsonSchema\JsonSchema;
8+
9+
class IntEnumType extends EnumType
10+
{
11+
/**
12+
* @var string|array
13+
*/
14+
protected $type = JsonSchema::TYPE_INT;
15+
16+
/**
17+
* @var int[]
18+
*/
19+
protected $entries;
20+
21+
public function __construct(int ...$entries)
22+
{
23+
$this->entries = $entries;
24+
}
25+
}

src/Type/StringEnumType.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace EventEngine\JsonSchema\Type;
6+
7+
use EventEngine\JsonSchema\JsonSchema;
8+
9+
class StringEnumType extends EnumType
10+
{
11+
/**
12+
* @var string|array
13+
*/
14+
protected $type = JsonSchema::TYPE_STRING;
15+
}

tests/Type/EnumTypeTest.php

Lines changed: 36 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,39 @@ public function it_creates_nullable_enum_type()
4748
$enumType->toArray()
4849
);
4950
}
51+
52+
53+
/**
54+
* @test
55+
*/
56+
public function it_creates_int_enum_type()
57+
{
58+
$enumType = JsonSchema::enum([0, 1], 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_int_nullable_enum_type()
74+
{
75+
$enumType = JsonSchema::enum([0, 1], JsonSchema::TYPE_INT)
76+
->asNullable();
77+
78+
$this->assertEquals(
79+
[
80+
'type' => ['integer', 'null'],
81+
'enum' => [0, 1, null]
82+
],
83+
$enumType->toArray()
84+
);
85+
}
5086
}

0 commit comments

Comments
 (0)