File tree Expand file tree Collapse file tree 2 files changed +24
-4
lines changed
src/CloudEvents/Serializers/Formatters
tests/CloudEvents/Serializers/Formatters Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -27,11 +27,19 @@ public function encodeTime(?DateTimeInterface $time): ?string
27
27
28
28
public function decodeTime (?string $ time ): ?DateTimeInterface
29
29
{
30
- $ parsed = DateTimeImmutable::createFromFormat (self ::TIME_FORMAT , $ time , new DateTimeZone (self ::TIME_ZONE ));
30
+ if ($ time === null ) {
31
+ return null ;
32
+ }
33
+
34
+ $ decoded = DateTimeImmutable::createFromFormat (self ::TIME_FORMAT , $ time , new DateTimeZone (self ::TIME_ZONE ));
35
+
36
+ if ($ decoded === false ) {
37
+ throw new ValueError (
38
+ \sprintf ('%s::decodeTime(): Argument #1 ($time) is not a valid RFC3339 timestamp ' , self ::class)
39
+ );
40
+ }
31
41
32
- return $ parsed === false
33
- ? null
34
- : $ parsed ;
42
+ return $ decoded ;
35
43
}
36
44
37
45
/**
Original file line number Diff line number Diff line change 5
5
use CloudEvents \Serializers \Formatters \Formatter ;
6
6
use DateTimeImmutable ;
7
7
use PHPUnit \Framework \TestCase ;
8
+ use ValueError ;
8
9
9
10
/**
10
11
* @coversDefaultClass \CloudEvents\Serializers\Formmaters\Formatter
@@ -22,6 +23,17 @@ public function testTime(): void
22
23
$ this ->assertEquals (new DateTimeImmutable ('2018-04-05T17:31:00Z ' ), $ formatter ->decodeTime ('2018-04-05T17:31:00Z ' ));
23
24
}
24
25
26
+ /**
27
+ * @covers ::decodeTime
28
+ */
29
+ public function testDecodeInvalidTime (): void
30
+ {
31
+ $ formatter = new Formatter ();
32
+ $ this ->expectException (ValueError::class);
33
+ $ this ->expectExceptionMessage ('CloudEvents \\Serializers \\Formatters \\Formatter::decodeTime(): Argument #1 ($time) is not a valid RFC3339 timestamp ' );
34
+ $ formatter ->decodeTime ('2018asdsdsafd ' );
35
+ }
36
+
25
37
/**
26
38
* @covers ::encodeData
27
39
* @covers ::decodeData
You can’t perform that action at this time.
0 commit comments