1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Pushwoosh; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use JsonSerializable; |
8
|
|
|
|
9
|
|
|
class PushwooshRecipient implements JsonSerializable |
10
|
|
|
{ |
11
|
|
|
protected $devices; |
12
|
|
|
protected $geoZone; |
13
|
|
|
protected $platforms; |
14
|
|
|
protected $users; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Create a new recipient. |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
24 |
|
public function __construct() |
22
|
|
|
{ |
23
|
24 |
|
$this->devices = []; |
24
|
24 |
|
$this->platforms = []; |
25
|
24 |
|
$this->users = []; |
26
|
24 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Set the device(s). |
30
|
|
|
* |
31
|
|
|
* @param string ...$devices |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
12 |
|
public function device(string ...$devices) |
35
|
|
|
{ |
36
|
12 |
|
$this->devices = array_merge($this->devices, $devices); |
37
|
12 |
|
$this->users = []; |
38
|
|
|
|
39
|
12 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
6 |
|
protected static function getSupportedPlatforms() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
6 |
|
'amazon' => 9, |
46
|
|
|
'android' => 3, |
47
|
|
|
'blackberry' => 2, |
48
|
|
|
'chrome' => 11, |
49
|
|
|
'firefox' => 12, |
50
|
|
|
'ios' => 1, |
51
|
|
|
'mac' => 7, |
52
|
|
|
'safari' => 10, |
53
|
|
|
'windows' => 8, |
54
|
|
|
'windows_phone' => 5, |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Convert the recipient to something JSON serializable. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
18 |
|
public function jsonSerialize() |
64
|
|
|
{ |
65
|
18 |
|
return array_filter([ |
66
|
18 |
|
'devices' => $this->devices, |
67
|
18 |
|
'geozone' => $this->geoZone, |
68
|
18 |
|
'platforms' => $this->platforms, |
69
|
18 |
|
'users' => $this->users, |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the platform(s). |
75
|
|
|
* |
76
|
|
|
* @param string ...$platforms |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
6 |
|
public function platform(string ...$platforms) |
80
|
|
|
{ |
81
|
6 |
|
$supported = static::getSupportedPlatforms(); |
82
|
|
|
|
83
|
6 |
|
foreach ($platforms as $platform) { |
84
|
6 |
|
$name = Str::slug(strtolower($platform)); |
85
|
|
|
|
86
|
6 |
|
if (!array_key_exists($name, $supported)) { |
87
|
6 |
|
throw new InvalidArgumentException("Unsupported platform $platform"); |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
$this->platforms[] = $supported[$name]; |
91
|
|
|
} |
92
|
|
|
|
93
|
6 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the user(s). |
98
|
|
|
* |
99
|
|
|
* @param string ...$users |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
6 |
|
public function user(string ...$users) |
103
|
|
|
{ |
104
|
6 |
|
$this->devices = []; |
105
|
6 |
|
$this->users = array_merge($this->users, $users); |
106
|
|
|
|
107
|
6 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set the geo zone. |
112
|
|
|
* |
113
|
|
|
* @param float $latitude |
114
|
|
|
* @param float $longitude |
115
|
|
|
* @param int $range |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function within(float $latitude, float $longitude, int $range) |
119
|
|
|
{ |
120
|
|
|
$this->geoZone = [ |
121
|
|
|
'lat' => $latitude, |
122
|
|
|
'lng' => $longitude, |
123
|
|
|
'range' => $range, |
124
|
|
|
]; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|