1
|
|
|
<?php |
2
|
|
|
namespace ker0x\Push\Adapter\Fcm\Message; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use Cake\Utility\Inflector; |
6
|
|
|
use ker0x\Push\Adapter\Fcm\Message\Exception\InvalidOptionsException; |
7
|
|
|
|
8
|
|
|
class Options |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var null|string |
12
|
|
|
*/ |
13
|
|
|
protected $collapseKey; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var null|string |
17
|
|
|
*/ |
18
|
|
|
protected $priority; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $contentAvailable = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var null|int |
27
|
|
|
*/ |
28
|
|
|
protected $timeToLive; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var null|string |
32
|
|
|
*/ |
33
|
|
|
protected $restrictedPackageName; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
protected $dryRun = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Options constructor. |
42
|
|
|
* @param array|OptionsBuilder $optionsBuilder |
43
|
|
|
*/ |
44
|
|
|
public function __construct($optionsBuilder) |
45
|
|
|
{ |
46
|
|
|
if (is_array($optionsBuilder)) { |
47
|
|
|
$optionsBuilder = $this->fromArray($optionsBuilder); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->collapseKey = $optionsBuilder->getCollapseKey(); |
51
|
|
|
$this->priority = $optionsBuilder->getPriority(); |
52
|
|
|
$this->contentAvailable = $optionsBuilder->isContentAvailable() ? true : null; |
53
|
|
|
$this->timeToLive = $optionsBuilder->getTimeToLive(); |
54
|
|
|
$this->dryRun = $optionsBuilder->isDryRun() ? true : null; |
55
|
|
|
$this->restrictedPackageName = $optionsBuilder->getRestrictedPackageName(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function build() |
62
|
|
|
{ |
63
|
|
|
$options = [ |
64
|
|
|
'collapse_key' => $this->collapseKey, |
65
|
|
|
'content_available' => $this->contentAvailable, |
66
|
|
|
'dry_run' => $this->dryRun, |
67
|
|
|
'priority' => $this->priority, |
68
|
|
|
'restricted_package_name' => $this->restrictedPackageName, |
69
|
|
|
'time_to_live' => $this->timeToLive, |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
return array_filter($options); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $optionsArray |
77
|
|
|
* @return \ker0x\Push\Adapter\Fcm\Message\OptionsBuilder |
78
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidOptionsException |
79
|
|
|
*/ |
80
|
|
|
private function fromArray(array $optionsArray): OptionsBuilder |
81
|
|
|
{ |
82
|
|
|
if (empty($optionsArray)) { |
83
|
|
|
throw InvalidOptionsException::arrayEmpty(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$optionsBuilder = new OptionsBuilder(); |
87
|
|
|
foreach ($optionsArray as $key => $value) { |
88
|
|
|
$key = Inflector::camelize($key); |
89
|
|
|
$setter = 'set' . $key; |
90
|
|
|
$optionsBuilder->$setter($value); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $optionsBuilder; |
94
|
|
|
} |
95
|
|
|
} |