diff --git a/src/RocketChatAttachment.php b/src/RocketChatAttachment.php index 75db712..6db1281 100644 --- a/src/RocketChatAttachment.php +++ b/src/RocketChatAttachment.php @@ -4,8 +4,7 @@ namespace NotificationChannels\RocketChat; -use DateTime; -use DateTimeZone; +use DateTimeInterface; use Illuminate\Support\Str; use InvalidArgumentException; @@ -103,18 +102,24 @@ public function text(string $text): self } /** - * @param string|\DateTime $timestamp + * @param string|\DateTimeInterface $timestamp * @return \NotificationChannels\RocketChat\RocketChatAttachment */ public function timestamp($timestamp): self { - if (! ($timestamp instanceof DateTime) && ! is_string($timestamp)) { - throw new InvalidArgumentException('Timestamp must be string or DateTime, '.gettype($timestamp).' given.'); + if (! ($timestamp instanceof DateTimeInterface) && ! is_string($timestamp)) { + $invalidType = is_object($timestamp) + ? get_class($timestamp) + : gettype($timestamp); + + throw new InvalidArgumentException(sprintf( + 'Timestamp must be string or DateTime, %s given.', + $invalidType + )); } - if ($timestamp instanceof DateTime) { - $date = clone $timestamp; - $timestamp = $date->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d\TH:i:s.v\Z'); + if ($timestamp instanceof DateTimeInterface) { + $timestamp = $timestamp->format(DateTimeInterface::RFC3339); } $this->timestamp = $timestamp; diff --git a/tests/RocketChatAttachmentTest.php b/tests/RocketChatAttachmentTest.php index 4dc9b9b..b767fe4 100644 --- a/tests/RocketChatAttachmentTest.php +++ b/tests/RocketChatAttachmentTest.php @@ -4,6 +4,10 @@ namespace NotificationChannels\RocketChat\Test; +use DateTime; +use DateTimeImmutable; +use DateTimeInterface; +use InvalidArgumentException; use NotificationChannels\RocketChat\RocketChatAttachment; use PHPUnit\Framework\TestCase; @@ -63,11 +67,31 @@ public function it_can_set_the_timestamp(): void /** @test */ public function it_can_set_the_timestamp_as_datetime(): void { - $date = \DateTime::createFromFormat('Y-m-d H:i:s.u', '2020-02-19 19:00:00.000'); + $date = DateTime::createFromFormat('Y-m-d H:i:s.u', '2020-02-19 19:00:00.000'); $attachment = new RocketChatAttachment(); $attachment->timestamp($date); - $this->assertEquals(['ts' => '2020-02-19T19:00:00.000Z'], $attachment->toArray()); + $this->assertEquals(['ts' => $date->format(DateTimeInterface::ATOM)], $attachment->toArray()); + } + + /** @test */ + public function it_can_set_the_timestamp_as_immutable_datetime(): void + { + $date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', '2020-02-19 19:00:00.000'); + $attachment = new RocketChatAttachment(); + $attachment->timestamp($date); + + $this->assertSame(['ts' => $date->format(DateTimeInterface::ATOM)], $attachment->toArray()); + } + + /** @test */ + public function it_cannot_set_the_timestamp_as_integer(): void + { + $this->expectException(InvalidArgumentException::class); + + $date = 1234567890; + $attachment = new RocketChatAttachment(); + $attachment->timestamp($date); } /** @test */