1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OneSignal\Resolver; |
4
|
|
|
|
5
|
|
|
use OneSignal\Config; |
6
|
|
|
use Symfony\Component\OptionsResolver\Options; |
7
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
8
|
|
|
|
9
|
|
|
class NotificationResolver implements ResolverInterface |
10
|
|
|
{ |
11
|
|
|
const SEND_AFTER_FORMAT = 'Y-m-d H:i:sO'; |
12
|
|
|
const DELIVERY_TIME_OF_DAY_FORMAT = 'g:iA'; |
13
|
|
|
|
14
|
|
|
private $config; |
15
|
|
|
|
16
|
|
|
public function __construct(Config $config) |
17
|
|
|
{ |
18
|
|
|
$this->config = $config; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function resolve(array $data) |
25
|
|
|
{ |
26
|
|
|
return (new OptionsResolver()) |
27
|
|
|
->setDefined('contents') |
28
|
|
|
->setAllowedTypes('contents', 'array') |
29
|
|
|
->setDefined('headings') |
30
|
|
|
->setAllowedTypes('headings', 'array') |
31
|
|
|
->setDefined('subtitle') |
32
|
|
|
->setAllowedTypes('subtitle', 'array') |
33
|
|
|
->setDefined('isIos') |
34
|
|
|
->setAllowedTypes('isIos', 'bool') |
35
|
|
|
->setDefined('isAndroid') |
36
|
|
|
->setAllowedTypes('isAndroid', 'bool') |
37
|
|
|
->setDefined('isWP') |
38
|
|
|
->setAllowedTypes('isWP', 'bool') |
39
|
|
|
->setDefined('isWP_WNS') |
40
|
|
|
->setAllowedTypes('isWP_WNS', 'bool') |
41
|
|
|
->setDefined('isAdm') |
42
|
|
|
->setAllowedTypes('isAdm', 'bool') |
43
|
|
|
->setDefined('isChrome') |
44
|
|
|
->setAllowedTypes('isChrome', 'bool') |
45
|
|
|
->setDefined('isChromeWeb') |
46
|
|
|
->setAllowedTypes('isChromeWeb', 'bool') |
47
|
|
|
->setDefined('isFirefox') |
48
|
|
|
->setAllowedTypes('isFirefox', 'bool') |
49
|
|
|
->setDefined('isSafari') |
50
|
|
|
->setAllowedTypes('isSafari', 'bool') |
51
|
|
|
->setDefined('isAnyWeb') |
52
|
|
|
->setAllowedTypes('isAnyWeb', 'bool') |
53
|
|
|
->setDefined('included_segments') |
54
|
|
|
->setAllowedTypes('included_segments', 'array') |
55
|
|
|
->setDefined('excluded_segments') |
56
|
|
|
->setAllowedTypes('excluded_segments', 'array') |
57
|
|
|
->setDefined('include_player_ids') |
58
|
|
|
->setAllowedTypes('include_player_ids', 'array') |
59
|
|
|
->setDefined('include_ios_tokens') |
60
|
|
|
->setAllowedTypes('include_ios_tokens', 'array') |
61
|
|
|
->setDefined('include_android_reg_ids') |
62
|
|
|
->setAllowedTypes('include_android_reg_ids', 'array') |
63
|
|
|
->setDefined('include_wp_uris') |
64
|
|
|
->setAllowedTypes('include_wp_uris', 'array') |
65
|
|
|
->setDefined('include_wp_wns_uris') |
66
|
|
|
->setAllowedTypes('include_wp_wns_uris', 'array') |
67
|
|
|
->setDefined('include_amazon_reg_ids') |
68
|
|
|
->setAllowedTypes('include_amazon_reg_ids', 'array') |
69
|
|
|
->setDefined('include_chrome_reg_ids') |
70
|
|
|
->setAllowedTypes('include_chrome_reg_ids', 'array') |
71
|
|
|
->setDefined('include_chrome_web_reg_ids') |
72
|
|
|
->setAllowedTypes('include_chrome_web_reg_ids', 'array') |
73
|
|
|
->setDefined('app_ids') |
74
|
|
|
->setAllowedTypes('app_ids', 'array') |
75
|
|
|
->setDefined('filters') |
76
|
|
|
->setAllowedTypes('filters', 'array') |
77
|
|
|
->setNormalizer('filters', function (Options $options, array $values) { |
78
|
|
|
return $this->normalizeFilters($options, $values); |
79
|
|
|
}) |
80
|
|
|
->setDefined('ios_badgeType') |
81
|
|
|
->setAllowedTypes('ios_badgeType', 'string') |
82
|
|
|
->setAllowedValues('ios_badgeType', ['None', 'SetTo', 'Increase']) |
83
|
|
|
->setDefined('ios_badgeCount') |
84
|
|
|
->setAllowedTypes('ios_badgeCount', 'int') |
85
|
|
|
->setDefined('ios_sound') |
86
|
|
|
->setAllowedTypes('ios_sound', 'string') |
87
|
|
|
->setDefined('android_sound') |
88
|
|
|
->setAllowedTypes('android_sound', 'string') |
89
|
|
|
->setDefined('adm_sound') |
90
|
|
|
->setAllowedTypes('adm_sound', 'string') |
91
|
|
|
->setDefined('wp_sound') |
92
|
|
|
->setAllowedTypes('wp_sound', 'string') |
93
|
|
|
->setDefined('wp_wns_sound') |
94
|
|
|
->setAllowedTypes('wp_wns_sound', 'string') |
95
|
|
|
->setDefined('data') |
96
|
|
|
->setAllowedTypes('data', 'array') |
97
|
|
|
->setDefined('buttons') |
98
|
|
|
->setAllowedTypes('buttons', 'array') |
99
|
|
|
->setNormalizer('buttons', function (Options $options, array $values) { |
100
|
|
|
return $this->normalizeButtons($values); |
101
|
|
|
}) |
102
|
|
|
->setDefined('android_channel_id') |
103
|
|
|
->setAllowedTypes('android_channel_id', 'string') |
104
|
|
|
->setDefined('existing_android_channel_id') |
105
|
|
|
->setAllowedTypes('existing_android_channel_id', 'string') |
106
|
|
|
->setDefined('android_background_layout') |
107
|
|
|
->setAllowedTypes('android_background_layout', 'array') |
108
|
|
|
->setAllowedValues('android_background_layout', function ($layouts) { |
109
|
|
|
return $this->filterAndroidBackgroundLayout($layouts); |
110
|
|
|
}) |
111
|
|
|
->setDefined('small_icon') |
112
|
|
|
->setAllowedTypes('small_icon', 'string') |
113
|
|
|
->setDefined('large_icon') |
114
|
|
|
->setAllowedTypes('large_icon', 'string') |
115
|
|
|
->setDefined('ios_attachments') |
116
|
|
|
->setAllowedTypes('ios_attachments', 'array') |
117
|
|
|
->setAllowedValues('ios_attachments', function ($attachments) { |
118
|
|
|
return $this->filterIosAttachments($attachments); |
119
|
|
|
}) |
120
|
|
|
->setDefined('big_picture') |
121
|
|
|
->setAllowedTypes('big_picture', 'string') |
122
|
|
|
->setDefined('adm_small_icon') |
123
|
|
|
->setAllowedTypes('adm_small_icon', 'string') |
124
|
|
|
->setDefined('adm_large_icon') |
125
|
|
|
->setAllowedTypes('adm_large_icon', 'string') |
126
|
|
|
->setDefined('adm_big_picture') |
127
|
|
|
->setAllowedTypes('adm_big_picture', 'string') |
128
|
|
|
->setDefined('web_buttons') |
129
|
|
|
->setAllowedTypes('web_buttons', 'array') |
130
|
|
|
->setAllowedValues('web_buttons', function ($buttons) { |
131
|
|
|
return $this->filterWebButtons($buttons); |
132
|
|
|
}) |
133
|
|
|
->setDefined('ios_category') |
134
|
|
|
->setAllowedTypes('ios_category', 'string') |
135
|
|
|
->setDefined('chrome_icon') |
136
|
|
|
->setAllowedTypes('chrome_icon', 'string') |
137
|
|
|
->setDefined('chrome_big_picture') |
138
|
|
|
->setAllowedTypes('chrome_big_picture', 'string') |
139
|
|
|
->setDefined('chrome_web_icon') |
140
|
|
|
->setAllowedTypes('chrome_web_icon', 'string') |
141
|
|
|
->setDefined('chrome_web_image') |
142
|
|
|
->setAllowedTypes('chrome_web_image', 'string') |
143
|
|
|
->setDefined('firefox_icon') |
144
|
|
|
->setAllowedTypes('firefox_icon', 'string') |
145
|
|
|
->setDefined('url') |
146
|
|
|
->setAllowedTypes('url', 'string') |
147
|
|
|
->setAllowedValues('url', function ($value) { |
148
|
|
|
return $this->filterUrl($value); |
149
|
|
|
}) |
150
|
|
|
->setDefined('send_after') |
151
|
|
|
->setAllowedTypes('send_after', '\DateTimeInterface') |
152
|
|
|
->setNormalizer('send_after', function (Options $options, \DateTime $value) { |
153
|
|
|
return $this->normalizeDateTime($options, $value, self::SEND_AFTER_FORMAT); |
154
|
|
|
}) |
155
|
|
|
->setDefined('delayed_option') |
156
|
|
|
->setAllowedTypes('delayed_option', 'string') |
157
|
|
|
->setAllowedValues('delayed_option', ['timezone', 'last-active']) |
158
|
|
|
->setDefined('delivery_time_of_day') |
159
|
|
|
->setAllowedTypes('delivery_time_of_day', '\DateTimeInterface') |
160
|
|
|
->setNormalizer('delivery_time_of_day', function (Options $options, \DateTime $value) { |
161
|
|
|
return $this->normalizeDateTime($options, $value, self::DELIVERY_TIME_OF_DAY_FORMAT); |
162
|
|
|
}) |
163
|
|
|
->setDefined('android_led_color') |
164
|
|
|
->setAllowedTypes('android_led_color', 'string') |
165
|
|
|
->setDefined('android_accent_color') |
166
|
|
|
->setAllowedTypes('android_accent_color', 'string') |
167
|
|
|
->setDefined('android_visibility') |
168
|
|
|
->setAllowedTypes('android_visibility', 'int') |
169
|
|
|
->setAllowedValues('android_visibility', [-1, 0, 1]) |
170
|
|
|
->setDefined('collapse_id') |
171
|
|
|
->setAllowedTypes('collapse_id', 'string') |
172
|
|
|
->setDefined('content_available') |
173
|
|
|
->setAllowedTypes('content_available', 'bool') |
174
|
|
|
->setDefined('mutable_content') |
175
|
|
|
->setAllowedTypes('mutable_content', 'bool') |
176
|
|
|
->setDefined('android_background_data') |
177
|
|
|
->setAllowedTypes('android_background_data', 'bool') |
178
|
|
|
->setDefined('amazon_background_data') |
179
|
|
|
->setAllowedTypes('amazon_background_data', 'bool') |
180
|
|
|
->setDefined('template_id') |
181
|
|
|
->setAllowedTypes('template_id', 'string') |
182
|
|
|
->setDefined('android_group') |
183
|
|
|
->setAllowedTypes('android_group', 'string') |
184
|
|
|
->setDefined('android_group_message') |
185
|
|
|
->setAllowedTypes('android_group_message', 'array') |
186
|
|
|
->setDefined('adm_group') |
187
|
|
|
->setAllowedTypes('adm_group', 'string') |
188
|
|
|
->setDefined('adm_group_message') |
189
|
|
|
->setAllowedTypes('adm_group_message', 'array') |
190
|
|
|
->setDefined('ttl') |
191
|
|
|
->setAllowedTypes('ttl', 'int') |
192
|
|
|
->setDefined('priority') |
193
|
|
|
->setAllowedTypes('priority', 'int') |
194
|
|
|
->setDefault('app_id', $this->config->getApplicationId()) |
195
|
|
|
->setAllowedTypes('app_id', 'string') |
196
|
|
|
->resolve($data); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
private function normalizeFilters(Options $options, array $values) |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
$filters = []; |
202
|
|
|
|
203
|
|
|
foreach ($values as $filter) { |
204
|
|
|
if (isset($filter['field'])) { |
205
|
|
|
$filters[] = $filter; |
206
|
|
|
} elseif (isset($filter['operator'])) { |
207
|
|
|
$filters[] = ['operator' => 'OR']; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $filters; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
private function filterUrl($value) |
215
|
|
|
{ |
216
|
|
|
return (bool) filter_var($value, FILTER_VALIDATE_URL); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
private function normalizeButtons($values) |
220
|
|
|
{ |
221
|
|
|
$buttons = []; |
222
|
|
|
|
223
|
|
|
foreach ($values as $button) { |
224
|
|
|
if (!isset($button['text'])) { |
225
|
|
|
continue; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$buttons[] = [ |
229
|
|
|
'id' => (isset($button['id']) ? $button['id'] : mt_rand()), |
230
|
|
|
'text' => $button['text'], |
231
|
|
|
'icon' => (isset($button['icon']) ? $button['icon'] : null), |
232
|
|
|
]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $buttons; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
private function filterAndroidBackgroundLayout($layouts) |
239
|
|
|
{ |
240
|
|
|
if (empty($layouts)) { |
241
|
|
|
return false; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$requiredKeys = ['image', 'headings_color', 'contents_color']; |
245
|
|
|
|
246
|
|
|
foreach ($layouts as $k => $v) { |
247
|
|
|
if (!in_array($k, $requiredKeys) || !is_string($v)) { |
248
|
|
|
return false; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return true; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
private function filterIosAttachments($attachments) |
256
|
|
|
{ |
257
|
|
|
foreach ($attachments as $key => $value) { |
258
|
|
|
if (!is_string($key) || !is_string($value)) { |
259
|
|
|
return false; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return true; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
private function filterWebButtons($buttons) |
267
|
|
|
{ |
268
|
|
|
$requiredKeys = ['id', 'text', 'icon', 'url']; |
269
|
|
|
foreach ($buttons as $button) { |
270
|
|
|
if (!is_array($button)) { |
271
|
|
|
return false; |
272
|
|
|
} |
273
|
|
|
if (count(array_intersect_key(array_flip($requiredKeys), $button)) != count($requiredKeys)) { |
274
|
|
|
return false; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return true; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
private function normalizeDateTime(Options $options, \DateTime $value, $format) |
|
|
|
|
282
|
|
|
{ |
283
|
|
|
return $value->format($format); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.