1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Pushbullet; |
4
|
|
|
|
5
|
|
|
use NotificationChannels\Pushbullet\Targets\Targetable; |
6
|
|
|
|
7
|
|
|
class PushbulletMessage |
8
|
|
|
{ |
9
|
|
|
const TYPE_NOTE = 'note'; |
10
|
|
|
const TYPE_LINK = 'link'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Type of message (currently: note or link). |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
public $type = 'note'; |
18
|
|
|
|
19
|
|
|
/** @var \NotificationChannels\Pushbullet\Targets\Targetable */ |
20
|
|
|
protected $target; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Notification title. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
public $title; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Notification message. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $message; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Url if notification is of link type. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public $url; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $message |
45
|
|
|
* |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
|
|
public static function create($message) |
49
|
|
|
{ |
50
|
|
|
return new static($message); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $message |
55
|
|
|
*/ |
56
|
|
|
public function __construct($message) |
57
|
|
|
{ |
58
|
|
|
$this->message = $message; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param \NotificationChannels\Pushbullet\Targets\Targetable $targetable |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function target(Targetable $targetable) |
67
|
|
|
{ |
68
|
|
|
$this->target = $targetable; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Specify that notification is of `note` type. |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function note() |
79
|
|
|
{ |
80
|
|
|
$this->type = static::TYPE_NOTE; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Specify that notification is of `link` type. |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function link() |
91
|
|
|
{ |
92
|
|
|
$this->type = static::TYPE_LINK; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set notification title. |
99
|
|
|
* |
100
|
|
|
* @param string $title |
101
|
|
|
* |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function title($title) |
105
|
|
|
{ |
106
|
|
|
$this->title = $title; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set notification message. |
113
|
|
|
* |
114
|
|
|
* @param string $message |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function message($message) |
119
|
|
|
{ |
120
|
|
|
$this->message = $message; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set notification url (if notification is of `link` type). |
127
|
|
|
* |
128
|
|
|
* @param string $url |
129
|
|
|
* |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function url($url) |
133
|
|
|
{ |
134
|
|
|
$this->url = $url; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get array representation of message for Pushbullet client. |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
public function toArray() |
145
|
|
|
{ |
146
|
|
|
$payload = [ |
147
|
|
|
'target' => $this->target->getTarget(), |
148
|
|
|
'type' => $this->type, |
149
|
|
|
'title' => $this->title, |
150
|
|
|
'body' => $this->message, |
151
|
|
|
]; |
152
|
|
|
|
153
|
|
|
if ($this->type === static::TYPE_LINK) { |
154
|
|
|
$payload['url'] = $this->url; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $payload; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|