1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlexLisenkov\LaravelWebPush; |
4
|
|
|
|
5
|
|
|
use AlexLisenkov\LaravelWebPush\Contracts\MessageActionContract; |
6
|
|
|
|
7
|
|
|
class MessageAction implements MessageActionContract |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* A DOMString identifying a user action to be displayed on the notification. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $action; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A DOMString containing action text to be shown to the user. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $title; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A USVString containing the URL of an icon to display with the action. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $icon; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get Action |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
6 |
|
public function getAction(): string |
36
|
|
|
{ |
37
|
6 |
|
return $this->action; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set Action |
42
|
|
|
* |
43
|
|
|
* @param string $action |
44
|
|
|
* |
45
|
|
|
* @return MessageAction |
46
|
|
|
*/ |
47
|
5 |
|
public function setAction(string $action): MessageAction |
48
|
|
|
{ |
49
|
5 |
|
$this->action = $action; |
50
|
|
|
|
51
|
5 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get Title |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
6 |
|
public function getTitle(): string |
60
|
|
|
{ |
61
|
6 |
|
return $this->title; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set Title |
66
|
|
|
* |
67
|
|
|
* @param string $title |
68
|
|
|
* |
69
|
|
|
* @return MessageAction |
70
|
|
|
*/ |
71
|
5 |
|
public function setTitle(string $title): MessageAction |
72
|
|
|
{ |
73
|
5 |
|
$this->title = $title; |
74
|
|
|
|
75
|
5 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get Icon |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
6 |
|
public function getIcon(): ?string |
84
|
|
|
{ |
85
|
6 |
|
return $this->icon; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set Icon |
90
|
|
|
* |
91
|
|
|
* @param string $icon |
92
|
|
|
* |
93
|
|
|
* @return MessageAction |
94
|
|
|
*/ |
95
|
5 |
|
public function setIcon(string $icon): MessageAction |
96
|
|
|
{ |
97
|
5 |
|
$this->icon = $icon; |
98
|
|
|
|
99
|
5 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the instance as an array. |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
5 |
|
public function toArray(): array |
108
|
|
|
{ |
109
|
|
|
return [ |
110
|
5 |
|
'action' => $this->getAction(), |
111
|
5 |
|
'title' => $this->getTitle(), |
112
|
5 |
|
'icon' => $this->getIcon(), |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Convert the object to its JSON representation. |
118
|
|
|
* |
119
|
|
|
* @param int $options |
120
|
|
|
* |
121
|
|
|
* @return false|string |
122
|
|
|
*/ |
123
|
3 |
|
public function toJson($options = PushMessage::DEFAULT_ENCODING_OPTIONS) |
124
|
|
|
{ |
125
|
3 |
|
return json_encode($this->toArray(), $options); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
1 |
|
public function __toString(): string |
132
|
|
|
{ |
133
|
1 |
|
if( $string = $this->toJson() ){ |
134
|
1 |
|
return (string) $string; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return ''; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Specify data which should be serialized to JSON |
142
|
|
|
* |
143
|
|
|
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php |
144
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
145
|
|
|
* which is a value of any type other than a resource. |
146
|
|
|
* @since 5.4.0 |
147
|
|
|
*/ |
148
|
1 |
|
public function jsonSerialize(): string |
149
|
|
|
{ |
150
|
1 |
|
return $this->toJson(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|