Skip to content

Commit 97052f5

Browse files
committed
Add message format types
1 parent 30723cb commit 97052f5

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ public function routeNotificationForPushover() {
113113
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('')`.
114114

115115
- `content($message)`: Accepts a string value for the message text.
116+
- `html()`: Sets the message type to [HTML](https://pushover.net/api#html).
117+
- `monospace()`: Sets the message type to monospace.
118+
- `plain()`: Sets the message type to plain text, this is the default.
116119
- `title($title)`: Accepts a string value for the message title.
117120
- `time($timestamp)`: Accepts either a `Carbon` object or a UNIX timestamp.
118121
- `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.

src/PushoverMessage.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ class PushoverMessage
1414
*/
1515
public $content;
1616

17+
/**
18+
* The format of the message.
19+
*
20+
* Either "plain", "html" or "monospace".
21+
*
22+
* @var string
23+
*/
24+
public $format = self::FORMAT_PLAIN;
25+
1726
/**
1827
* The (optional) title of the message.
1928
*
@@ -72,6 +81,13 @@ class PushoverMessage
7281
*/
7382
public $sound;
7483

84+
/**
85+
* Message formats.
86+
*/
87+
const FORMAT_PLAIN = 0;
88+
const FORMAT_HTML = 1;
89+
const FORMAT_MONOSPACE = 2;
90+
7591
/**
7692
* Message priorities.
7793
*/
@@ -112,6 +128,42 @@ public function content($content)
112128
return $this;
113129
}
114130

131+
/**
132+
* Set the formatting type to plain text.
133+
*
134+
* @return $this
135+
*/
136+
public function plain()
137+
{
138+
$this->format = static::FORMAT_PLAIN;
139+
140+
return $this;
141+
}
142+
143+
/**
144+
* Set the formatting type to HTML.
145+
*
146+
* @return $this
147+
*/
148+
public function html()
149+
{
150+
$this->format = static::FORMAT_HTML;
151+
152+
return $this;
153+
}
154+
155+
/**
156+
* Set the formatting type to monospace.
157+
*
158+
* @return $this
159+
*/
160+
public function monospace()
161+
{
162+
$this->format = self::FORMAT_MONOSPACE;
163+
164+
return $this;
165+
}
166+
115167
/**
116168
* Set the title of the Pushover message.
117169
*
@@ -260,6 +312,8 @@ public function toArray()
260312
'sound' => $this->sound,
261313
'retry' => $this->retry,
262314
'expire' => $this->expire,
315+
'html' => $this->format === static::FORMAT_HTML,
316+
'monospace' => $this->format === static::FORMAT_MONOSPACE,
263317
];
264318
}
265319

tests/IntegrationTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public function it_can_send_a_pushover_notification_with_a_global_token()
5656
'sound' => 'boing',
5757
'url' => 'http://example.com',
5858
'url_title' => 'Example Website',
59+
'html' => false,
60+
'monospace' => false,
5961
]);
6062

6163
$pushover = new Pushover($this->guzzleClient, 'global-application-token');
@@ -70,7 +72,8 @@ public function it_can_send_a_pushover_notification_with_a_global_token()
7072
/** @test */
7173
public function it_can_send_a_pushover_notification_with_an_overridden_token()
7274
{
73-
$message = (new PushoverMessage('Message text'))
75+
$message = (new PushoverMessage('Message <b>text</b>'))
76+
->html()
7477
->title('Message title')
7578
->emergencyPriority(60, 600)
7679
->time(123456789)
@@ -81,7 +84,7 @@ public function it_can_send_a_pushover_notification_with_an_overridden_token()
8184
'token' => 'overridden-application-token',
8285
'user' => 'pushover-key',
8386
'device' => 'iphone,desktop',
84-
'message' => 'Message text',
87+
'message' => 'Message <b>text</b>',
8588
'title' => 'Message title',
8689
'priority' => 2,
8790
'retry' => 60,
@@ -90,6 +93,8 @@ public function it_can_send_a_pushover_notification_with_an_overridden_token()
9093
'sound' => 'boing',
9194
'url' => 'http://example.com',
9295
'url_title' => 'Example Website',
96+
'html' => true,
97+
'monospace' => false,
9398
]);
9499

95100
$pushover = new Pushover($this->guzzleClient, 'global-application-token');

tests/PushoverMessageTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@ public function it_can_set_content()
5050
$this->assertEquals('message text', $this->message->content);
5151
}
5252

53+
/** @test */
54+
public function it_can_set_the_message_format_to_plain()
55+
{
56+
$this->message->plain();
57+
58+
$this->assertEquals(PushoverMessage::FORMAT_PLAIN, $this->message->format);
59+
}
60+
61+
/** @test */
62+
public function it_can_set_the_message_format_to_html()
63+
{
64+
$this->message->html();
65+
66+
$this->assertEquals(PushoverMessage::FORMAT_HTML, $this->message->format);
67+
}
68+
69+
/** @test */
70+
public function it_can_set_the_message_format_to_monospace()
71+
{
72+
$this->message->monospace();
73+
74+
$this->assertEquals(PushoverMessage::FORMAT_MONOSPACE, $this->message->format);
75+
}
76+
5377
/** @test */
5478
public function it_can_set_a_title()
5579
{

0 commit comments

Comments
 (0)