33namespace NotificationChannels \OneSignal ;
44
55use Illuminate \Support \Arr ;
6+ use NotificationChannels \OneSignal \Traits \Deprecated ;
7+ use NotificationChannels \OneSignal \Traits \Categories \ButtonHelpers ;
8+ use NotificationChannels \OneSignal \Traits \Categories \SilentHelpers ;
9+ use NotificationChannels \OneSignal \Traits \Categories \DeliveryHelpers ;
10+ use NotificationChannels \OneSignal \Traits \Categories \GroupingHelpers ;
11+ use NotificationChannels \OneSignal \Traits \Categories \AppearanceHelpers ;
12+ use NotificationChannels \OneSignal \Traits \Categories \AttachmentHelpers ;
613
714class OneSignalMessage
815{
9- /** @var string */
10- protected $ body ;
11-
12- /** @var string */
13- protected $ subject ;
14-
15- /** @var string */
16- protected $ url ;
17-
18- /** @var string */
19- protected $ icon ;
20-
21- /** @var array */
22- protected $ data = [];
16+ use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers, SilentHelpers, Deprecated;
2317
2418 /** @var array */
25- protected $ buttons = [];
26-
27- /** @var array */
28- protected $ webButtons = [];
29-
30- /** @var array */
31- protected $ extraParameters = [];
19+ protected $ payload = [];
3220
3321 /**
3422 * @param string $body
@@ -45,212 +33,103 @@ public static function create($body = '')
4533 */
4634 public function __construct ($ body = '' )
4735 {
48- $ this ->body = $ body ;
36+ $ this ->setBody ( $ body) ;
4937 }
5038
5139 /**
5240 * Set the message body.
5341 *
54- * @param string $value
55- *
56- * @return $this
57- */
58- public function body ($ value )
59- {
60- $ this ->body = $ value ;
61-
62- return $ this ;
63- }
64-
65- /**
66- * Set the message icon.
67- *
68- * @param string $value
42+ * @param mixed $value
6943 *
7044 * @return $this
7145 */
72- public function icon ($ value )
46+ public function setBody ($ value )
7347 {
74- $ this ->icon = $ value ;
75-
76- return $ this ;
48+ return $ this ->setParameter ('contents ' , $ this ->parseValueToArray ($ value ));
7749 }
7850
7951 /**
8052 * Set the message subject.
8153 *
82- * @param string $value
54+ * @param mixed $value
8355 *
8456 * @return $this
8557 */
86- public function subject ($ value )
58+ public function setSubject ($ value )
8759 {
88- $ this ->subject = $ value ;
89-
90- return $ this ;
60+ return $ this ->setParameter ('headings ' , $ this ->parseValueToArray ($ value ));
9161 }
9262
9363 /**
94- * Set the message url .
64+ * Set the message template_id .
9565 *
9666 * @param string $value
9767 *
9868 * @return $this
9969 */
100- public function url ($ value )
101- {
102- $ this ->url = $ value ;
103-
104- return $ this ;
105- }
106-
107- /**
108- * Set the iOS badge increment count.
109- *
110- * @param int $count
111- *
112- * @return $this
113- */
114- public function incrementIosBadgeCount ($ count = 1 )
70+ public function setTemplate ($ value )
11571 {
116- return $ this ->setParameter ('ios_badgeType ' , 'Increase ' )
117- ->setParameter ('ios_badgeCount ' , $ count );
118- }
72+ Arr::forget ($ this ->payload , 'contents ' );
11973
120- /**
121- * Set the iOS badge decrement count.
122- *
123- * @param int $count
124- *
125- * @return $this
126- */
127- public function decrementIosBadgeCount ($ count = 1 )
128- {
129- return $ this ->setParameter ('ios_badgeType ' , 'Increase ' )
130- ->setParameter ('ios_badgeCount ' , -1 * $ count );
74+ return $ this ->setParameter ('template_id ' , $ value );
13175 }
13276
13377 /**
134- * Set the iOS badge count.
78+ * @param mixed $value
13579 *
136- * @param int $count
137- *
138- * @return $this
80+ * @return array
13981 */
140- public function setIosBadgeCount ( $ count )
82+ protected function parseValueToArray ( $ value )
14183 {
142- return $ this ->setParameter ('ios_badgeType ' , 'SetTo ' )
143- ->setParameter ('ios_badgeCount ' , $ count );
84+ return (is_array ($ value )) ? $ value : ['en ' => $ value ];
14485 }
14586
14687 /**
14788 * Set additional data.
14889 *
14990 * @param string $key
150- * @param string $value
91+ * @param mixed $value
15192 *
15293 * @return $this
15394 */
154- public function setData ($ key , $ value )
95+ public function setData (string $ key , $ value )
15596 {
156- $ this ->data [$ key ] = $ value ;
157-
158- return $ this ;
97+ return $ this ->setParameter ("data. {$ key }" , $ value );
15998 }
16099
161100 /**
162- * Set additional parameters.
101+ * Set parameters.
163102 *
164103 * @param string $key
165- * @param string $value
166- *
167- * @return $this
168- */
169- public function setParameter ($ key , $ value )
170- {
171- $ this ->extraParameters [$ key ] = $ value ;
172-
173- return $ this ;
174- }
175-
176- /**
177- * Add a web button to the message.
178- *
179- * @param OneSignalWebButton $button
104+ * @param mixed $value
180105 *
181106 * @return $this
182107 */
183- public function webButton ( OneSignalWebButton $ button )
108+ public function setParameter ( string $ key , $ value )
184109 {
185- $ this ->webButtons [] = $ button -> toArray ( );
110+ Arr:: set ( $ this ->payload , $ key , $ value );
186111
187112 return $ this ;
188113 }
189114
190115 /**
191- * Add a native button to the message .
116+ * Get parameters .
192117 *
193- * @param OneSignalButton $button
194- *
195- * @return $this
196- */
197- public function button (OneSignalButton $ button )
198- {
199- $ this ->buttons [] = $ button ->toArray ();
200-
201- return $ this ;
202- }
203-
204- /**
205- * Set an image to all possible attachment variables.
206- * @param string $imageUrl
118+ * @param string $key
119+ * @param mixed $default
207120 *
208- * @return $this
121+ * @return mixed
209122 */
210- public function setImageAttachments ( $ imageUrl )
123+ public function getParameter ( string $ key , $ default = null )
211124 {
212- $ this ->extraParameters ['ios_attachments ' ]['id1 ' ] = $ imageUrl ;
213- $ this ->extraParameters ['big_picture ' ] = $ imageUrl ;
214- $ this ->extraParameters ['adm_big_picture ' ] = $ imageUrl ;
215- $ this ->extraParameters ['chrome_big_picture ' ] = $ imageUrl ;
216-
217- return $ this ;
125+ return Arr::get ($ this ->payload , $ key , $ default );
218126 }
219127
220128 /**
221129 * @return array
222130 */
223131 public function toArray ()
224132 {
225- $ message = [
226- 'contents ' => ['en ' => $ this ->body ],
227- 'headings ' => $ this ->subjectToArray (),
228- 'url ' => $ this ->url ,
229- 'buttons ' => $ this ->buttons ,
230- 'web_buttons ' => $ this ->webButtons ,
231- 'chrome_web_icon ' => $ this ->icon ,
232- 'chrome_icon ' => $ this ->icon ,
233- 'adm_small_icon ' => $ this ->icon ,
234- 'small_icon ' => $ this ->icon ,
235- ];
236-
237- foreach ($ this ->extraParameters as $ key => $ value ) {
238- Arr::set ($ message , $ key , $ value );
239- }
240-
241- foreach ($ this ->data as $ data => $ value ) {
242- Arr::set ($ message , 'data. ' .$ data , $ value );
243- }
244-
245- return $ message ;
246- }
247-
248- protected function subjectToArray ()
249- {
250- if ($ this ->subject === null ) {
251- return [];
252- }
253-
254- return ['en ' => $ this ->subject ];
133+ return $ this ->payload ;
255134 }
256135}
0 commit comments