From 97052f5c9fc334e81172f0c4ab14dabd637106e7 Mon Sep 17 00:00:00 2001 From: Casper Boone Date: Sun, 13 Sep 2020 21:32:20 +0200 Subject: [PATCH] Add message format types --- README.md | 3 ++ src/PushoverMessage.php | 54 +++++++++++++++++++++++++++++++++++ tests/IntegrationTest.php | 9 ++++-- tests/PushoverMessageTest.php | 24 ++++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4037478..a3a9af4 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,9 @@ public function routeNotificationForPushover() { Please note that only the message content is mandatory, all other methods are optional. The message content can be set via `content('')`, via the create method `PushoverMessage::create('')` or via the constructor `new PushoverMessage('')`. - `content($message)`: Accepts a string value for the message text. +- `html()`: Sets the message type to [HTML](https://pushover.net/api#html). +- `monospace()`: Sets the message type to monospace. +- `plain()`: Sets the message type to plain text, this is the default. - `title($title)`: Accepts a string value for the message title. - `time($timestamp)`: Accepts either a `Carbon` object or a UNIX timestamp. - `url($url[, $title])`: Accepts a string value for a [supplementary url](https://pushover.net/api#urls) and an optional string value for the title of the url. diff --git a/src/PushoverMessage.php b/src/PushoverMessage.php index 487a5f5..fcecd3b 100644 --- a/src/PushoverMessage.php +++ b/src/PushoverMessage.php @@ -14,6 +14,15 @@ class PushoverMessage */ public $content; + /** + * The format of the message. + * + * Either "plain", "html" or "monospace". + * + * @var string + */ + public $format = self::FORMAT_PLAIN; + /** * The (optional) title of the message. * @@ -72,6 +81,13 @@ class PushoverMessage */ public $sound; + /** + * Message formats. + */ + const FORMAT_PLAIN = 0; + const FORMAT_HTML = 1; + const FORMAT_MONOSPACE = 2; + /** * Message priorities. */ @@ -112,6 +128,42 @@ public function content($content) return $this; } + /** + * Set the formatting type to plain text. + * + * @return $this + */ + public function plain() + { + $this->format = static::FORMAT_PLAIN; + + return $this; + } + + /** + * Set the formatting type to HTML. + * + * @return $this + */ + public function html() + { + $this->format = static::FORMAT_HTML; + + return $this; + } + + /** + * Set the formatting type to monospace. + * + * @return $this + */ + public function monospace() + { + $this->format = self::FORMAT_MONOSPACE; + + return $this; + } + /** * Set the title of the Pushover message. * @@ -260,6 +312,8 @@ public function toArray() 'sound' => $this->sound, 'retry' => $this->retry, 'expire' => $this->expire, + 'html' => $this->format === static::FORMAT_HTML, + 'monospace' => $this->format === static::FORMAT_MONOSPACE, ]; } diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index ee652da..9416812 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -56,6 +56,8 @@ public function it_can_send_a_pushover_notification_with_a_global_token() 'sound' => 'boing', 'url' => 'http://example.com', 'url_title' => 'Example Website', + 'html' => false, + 'monospace' => false, ]); $pushover = new Pushover($this->guzzleClient, 'global-application-token'); @@ -70,7 +72,8 @@ public function it_can_send_a_pushover_notification_with_a_global_token() /** @test */ public function it_can_send_a_pushover_notification_with_an_overridden_token() { - $message = (new PushoverMessage('Message text')) + $message = (new PushoverMessage('Message text')) + ->html() ->title('Message title') ->emergencyPriority(60, 600) ->time(123456789) @@ -81,7 +84,7 @@ public function it_can_send_a_pushover_notification_with_an_overridden_token() 'token' => 'overridden-application-token', 'user' => 'pushover-key', 'device' => 'iphone,desktop', - 'message' => 'Message text', + 'message' => 'Message text', 'title' => 'Message title', 'priority' => 2, 'retry' => 60, @@ -90,6 +93,8 @@ public function it_can_send_a_pushover_notification_with_an_overridden_token() 'sound' => 'boing', 'url' => 'http://example.com', 'url_title' => 'Example Website', + 'html' => true, + 'monospace' => false, ]); $pushover = new Pushover($this->guzzleClient, 'global-application-token'); diff --git a/tests/PushoverMessageTest.php b/tests/PushoverMessageTest.php index 73f4466..a2c371e 100644 --- a/tests/PushoverMessageTest.php +++ b/tests/PushoverMessageTest.php @@ -50,6 +50,30 @@ public function it_can_set_content() $this->assertEquals('message text', $this->message->content); } + /** @test */ + public function it_can_set_the_message_format_to_plain() + { + $this->message->plain(); + + $this->assertEquals(PushoverMessage::FORMAT_PLAIN, $this->message->format); + } + + /** @test */ + public function it_can_set_the_message_format_to_html() + { + $this->message->html(); + + $this->assertEquals(PushoverMessage::FORMAT_HTML, $this->message->format); + } + + /** @test */ + public function it_can_set_the_message_format_to_monospace() + { + $this->message->monospace(); + + $this->assertEquals(PushoverMessage::FORMAT_MONOSPACE, $this->message->format); + } + /** @test */ public function it_can_set_a_title() {