Completed
Push — master ( cea9be...6e27bb )
by Arthur
07:08
created

Alert::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Pushok package.
5
 *
6
 * (c) Arthur Edamov <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pushok\Payload;
13
14
/**
15
 * Class Alert
16
 *
17
 * @package Pushok\Payload
18
 *
19
 * @see http://bit.ly/payload-key-reference
20
 */
21
class Alert
22
{
23
    const ALERT_TITLE_KEY = 'title';
24
    const ALERT_BODY_KEY = 'body';
25
    const ALERT_TITLE_LOC_KEY = 'title-loc-key';
26
    const ALERT_TITLE_LOC_ARGS_KEY = 'title-loc-args';
27
    const ALERT_ACTION_LOC_KEY = 'action-loc-key';
28
    const ALERT_LOC_KEY = 'loc-key';
29
    const ALERT_LOC_ARGS_KEY = 'loc-args';
30
    const ALERT_LAUNCH_IMAGE_KEY = 'launch-image';
31
    const ALERT_MUTABLE_CONTENT_KEY = 'mutable-content';
32
33
    /**
34
     * A short string describing the purpose of the notification.
35
     *
36
     * @var string
37
     */
38
    private $title;
39
40
    /**
41
     * The text of the alert message.
42
     *
43
     * @var string
44
     */
45
    private $body;
46
47
    /**
48
     * The key to a title string in the Localizable.strings file for the current localization.
49
     *
50
     * @var string|null
51
     */
52
    private $titleLocKey;
53
54
    /**
55
     * Variable string values to appear in place of the format specifiers in title-loc-key.
56
     *
57
     * @var string[]|null
58
     */
59
    private $titleLocArgs;
60
61
    /**
62
     * If a string is specified, the iOS system displays an alert that includes the Close and View buttons.
63
     *
64
     * @var string|null
65
     */
66
    private $actionLocKey;
67
68
    /**
69
     * A key to an alert-message string in a Localizable.strings file for the current localization.
70
     *
71
     * @var string
72
     */
73
    private $locKey;
74
75
    /**
76
     * Variable string values to appear in place of the format specifiers in loc-key.
77
     *
78
     * @var string[]
79
     */
80
    private $locArgs;
81
82
    /**
83
     * The filename of an image file in the app bundle, with or without the filename extension.
84
     *
85
     * @var string
86
     */
87
    private $launchImage;
88
89
    /**
90
     * Access to modify the content of remote notifications before they are delivered to the user.
91
     *
92
     * @var bool
93
     */
94
    private $mutableContent;
95
96
    protected function __constructor()
97
    {
98
    }
99
100
    public static function create()
101
    {
102
        return new self();
103
    }
104
105
    /**
106
     * Set Alert title
107
     *
108
     * @param string $value
109
     * @return $this
110
     */
111
    public function setTitle(string $value)
112
    {
113
        $this->title = $value;
114
115
        return $this;
116
    }
117
118
    /**
119
     * Set Alert body
120
     *
121
     * @param string $value
122
     * @return $this
123
     */
124
    public function setBody(string $value)
125
    {
126
        $this->body = $value;
127
128
        return $this;
129
    }
130
131
    /**
132
     * Set title-loc-key
133
     *
134
     * @param string|null $value
135
     * @return $this
136
     */
137
    public function setTitleLocKey(string $value = null)
138
    {
139
        $this->titleLocKey = $value;
140
141
        return $this;
142
    }
143
144
    /**
145
     * Set title-loc-args
146
     *
147
     * @param array|null $value
148
     * @return $this
149
     */
150
    public function setTitleLocArgs(array $value = null)
151
    {
152
        $this->titleLocArgs = $value;
153
154
        return $this;
155
    }
156
157
    /**
158
     * Set action-loc-key
159
     *
160
     * @param string|null $value
161
     * @return $this
162
     */
163
    public function setActionLocKey(string $value = null)
164
    {
165
        $this->actionLocKey = $value;
166
167
        return $this;
168
    }
169
170
    /**
171
     * Set loc-key
172
     *
173
     * @param string $value
174
     * @return $this
175
     */
176
    public function setLocKey(string $value)
177
    {
178
        $this->locKey = $value;
179
180
        return $this;
181
    }
182
183
    /**
184
     * Set loc-args
185
     *
186
     * @param array $value
187
     * @return $this
188
     */
189
    public function setLocArgs(array $value)
190
    {
191
        $this->locArgs = $value;
192
193
        return $this;
194
    }
195
196
    /**
197
     * Set launch-image
198
     *
199
     * @param string $value
200
     * @return $this
201
     */
202
    public function setLaunchImage(string $value)
203
    {
204
        $this->launchImage = $value;
205
206
        return $this;
207
    }
208
209
    /**
210
     * Set the mutable-content key for Notification Service Extensions on iOS10
211
     * @see http://bit.ly/mutable-content
212
     *
213
     * @param bool $value
214
     * @return $this
215
     */
216
    public function isContentMutable(bool $value)
217
    {
218
        $this->mutableContent = $value;
219
220
        return $this;
221
    }
222
223
    /**
224
     * Transform Alert object to array.
225
     *
226
     * @return array
227
     */
228
    public function transform(): array
229
    {
230
        $alert= [];
231
232
        if (is_string($this->title)) {
233
            $alert[self::ALERT_TITLE_KEY] = $this->title;
234
        }
235
236
        if (is_string($this->body)) {
237
            $alert[self::ALERT_BODY_KEY] = $this->body;
238
        }
239
240
        if (is_string($this->titleLocKey)) {
241
            $alert[self::ALERT_TITLE_LOC_KEY] = $this->titleLocKey;
242
        }
243
244
        if (is_array($this->titleLocArgs)) {
245
            $alert[self::ALERT_TITLE_LOC_ARGS_KEY] = $this->titleLocArgs;
246
        }
247
248
        if (is_string($this->actionLocKey)) {
249
            $alert[self::ALERT_ACTION_LOC_KEY] = $this->actionLocKey;
250
        }
251
252
        if (is_string($this->locKey)) {
253
            $alert[self::ALERT_LOC_KEY] = $this->locKey;
254
        }
255
256
        if (is_array($this->locArgs)) {
257
            $alert[self::ALERT_LOC_ARGS_KEY] = $this->locArgs;
258
        }
259
260
        if (is_string($this->launchImage)) {
261
            $alert[self::ALERT_LAUNCH_IMAGE_KEY] = $this->launchImage;
262
        }
263
264
        if (is_bool($this->mutableContent)) {
265
            $alert[self::ALERT_MUTABLE_CONTENT_KEY] = $this->mutableContent;
266
        }
267
268
        return $alert;
269
    }
270
}
271