1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\OneSignal; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use NotificationChannels\OneSignal\Traits\{ |
7
|
|
|
Categories\AppearanceHelpers, Categories\AttachmentHelpers, Categories\ButtonHelpers, Categories\DeliveryHelpers, Categories\GroupingHelpers, Deprecated |
8
|
|
|
}; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class OneSignalMessage |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers, Deprecated; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
protected $payload = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $body |
21
|
|
|
* |
22
|
|
|
* @return static |
23
|
|
|
*/ |
24
|
1 |
|
public static function create($body = '') |
25
|
|
|
{ |
26
|
1 |
|
return new static($body); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $body |
31
|
|
|
*/ |
32
|
21 |
|
public function __construct($body = '') |
33
|
|
|
{ |
34
|
21 |
|
$this->setBody($body); |
35
|
21 |
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set the message body. |
40
|
|
|
* |
41
|
|
|
* @param mixed $value |
42
|
|
|
* |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
21 |
|
public function setBody($value) |
46
|
|
|
{ |
47
|
21 |
|
return $this->setParameter('contents', $this->parseValueToArray($value)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set the message subject. |
53
|
|
|
* |
54
|
|
|
* @param mixed $value |
55
|
|
|
* |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
1 |
|
public function setSubject($value) |
59
|
|
|
{ |
60
|
1 |
|
return $this->setParameter('headings', $this->parseValueToArray($value)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param mixed $value |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
21 |
|
protected function parseValueToArray($value) |
69
|
|
|
{ |
70
|
21 |
|
return (is_array($value)) ? $value : ['en' => $value]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set additional data. |
76
|
|
|
* |
77
|
|
|
* @param string $key |
78
|
|
|
* @param mixed $value |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
1 |
|
public function setData(string $key, $value) |
83
|
|
|
{ |
84
|
1 |
|
return $this->setParameter("data.{$key}", $value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set parameters. |
89
|
|
|
* |
90
|
|
|
* @param string $key |
91
|
|
|
* @param mixed $value |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
21 |
|
public function setParameter(string $key, $value) |
96
|
|
|
{ |
97
|
21 |
|
Arr::set($this->payload, $key, $value); |
98
|
|
|
|
99
|
21 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get parameters. |
104
|
|
|
* |
105
|
|
|
* @param string $key |
106
|
|
|
* @param mixed $default |
107
|
|
|
* |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
3 |
|
public function getParameter(string $key, $default = null) |
111
|
|
|
{ |
112
|
3 |
|
return Arr::get($this->payload, $key, $default); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
21 |
|
public function toArray() |
119
|
|
|
{ |
120
|
21 |
|
return $this->payload; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|