File tree Expand file tree Collapse file tree 3 files changed +78
-6
lines changed Expand file tree Collapse file tree 3 files changed +78
-6
lines changed Original file line number Diff line number Diff line change 13
13
14
14
use EventEngine \JsonSchema \AnnotatedType ;
15
15
use EventEngine \JsonSchema \JsonSchema ;
16
+ use EventEngine \JsonSchema \Type ;
16
17
17
18
class EnumType implements AnnotatedType
18
19
{
@@ -41,4 +42,16 @@ public function toArray(): array
41
42
'enum ' => $ this ->entries ,
42
43
], $ this ->annotations ());
43
44
}
45
+
46
+ public function asNullable (): Type
47
+ {
48
+ $ cp = clone $ this ;
49
+
50
+ $ this ->assertTypeCanBeConvertedToNullableType ($ cp );
51
+
52
+ $ cp ->type = [$ cp ->type , JsonSchema::TYPE_NULL ];
53
+ $ cp ->entries [] = null ;
54
+
55
+ return $ cp ;
56
+ }
44
57
}
Original file line number Diff line number Diff line change @@ -20,16 +20,25 @@ public function asNullable(): Type
20
20
{
21
21
$ cp = clone $ this ;
22
22
23
+ $ this ->assertTypeCanBeConvertedToNullableType ($ cp );
24
+
25
+ $ cp ->type = [$ cp ->type , JsonSchema::TYPE_NULL ];
26
+
27
+ return $ cp ;
28
+ }
29
+
30
+ protected function assertTypeCanBeConvertedToNullableType (Type $ cp ): void
31
+ {
23
32
if (! isset ($ cp ->type )) {
24
- throw new \RuntimeException ('Type cannot be converted to nullable type. No json schema type set for ' . \get_class ($ this ));
33
+ throw new \RuntimeException (
34
+ 'Type cannot be converted to nullable type. No json schema type set for ' . \get_class ($ this )
35
+ );
25
36
}
26
37
27
38
if (! \is_string ($ cp ->type )) {
28
- throw new \RuntimeException ('Type cannot be converted to nullable type. JSON schema type is not a string ' );
39
+ throw new \RuntimeException (
40
+ 'Type cannot be converted to nullable type. JSON schema type is not a string '
41
+ );
29
42
}
30
-
31
- $ cp ->type = [$ cp ->type , JsonSchema::TYPE_NULL ];
32
-
33
- return $ cp ;
34
43
}
35
44
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+ /**
3
+ * This file is part of the event-engine/php-json-schema.
4
+ * (c) 2017-2019 prooph software GmbH <[email protected] >
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+
10
+ declare (strict_types=1 );
11
+
12
+ namespace EventEngineTest \JsonSchema \Type ;
13
+
14
+ use EventEngine \JsonSchema \Type \EnumType ;
15
+ use EventEngineTest \JsonSchema \BasicTestCase ;
16
+
17
+ final class EnumTypeTest extends BasicTestCase
18
+ {
19
+ /**
20
+ * @test
21
+ */
22
+ public function it_creates_enum_type ()
23
+ {
24
+ $ enumType = new EnumType ('a ' , 'b ' );
25
+
26
+ $ this ->assertEquals (
27
+ [
28
+ 'type ' => 'string ' ,
29
+ 'enum ' => ['a ' , 'b ' ],
30
+ ],
31
+ $ enumType ->toArray ()
32
+ );
33
+ }
34
+
35
+ /**
36
+ * @test
37
+ */
38
+ public function it_creates_nullable_enum_type ()
39
+ {
40
+ $ enumType = (new EnumType ('a ' , 'b ' ))->asNullable ();
41
+
42
+ $ this ->assertEquals (
43
+ [
44
+ 'type ' => ['string ' , 'null ' ],
45
+ 'enum ' => ['a ' , 'b ' , null ]
46
+ ],
47
+ $ enumType ->toArray ()
48
+ );
49
+ }
50
+ }
You can’t perform that action at this time.
0 commit comments