Passed
Pull Request — master (#123)
by Romain
02:12
created

ValidatorTrait::isValidColor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Helper;
6
7
use Kerox\Messenger\Exception\MessengerException;
8
use Kerox\Messenger\Model\Common\Button\AbstractButton;
9
use Kerox\Messenger\Model\Message;
10
use Kerox\Messenger\Model\Message\Attachment;
11
use Kerox\Messenger\Model\Message\Attachment\Template\GenericTemplate;
12
use Kerox\Messenger\SendInterface;
13
14
trait ValidatorTrait
15
{
16
    /**
17
     * @param string $value
18
     *
19
     * @throws \Kerox\Messenger\Exception\MessengerException
20
     */
21 2
    protected function isValidColor(string $value): void
22
    {
23 2
        if (!preg_match('/^#[A-Fa-f0-9]{6}$/', $value)) {
24 1
            throw new MessengerException('The color must be expressed in #rrggbb format.');
25
        }
26 1
    }
27
28
    /**
29
     * @param string $value
30
     * @param int    $length
31
     *
32
     * @throws \Kerox\Messenger\Exception\MessengerException
33
     */
34 39
    protected function isValidString(string $value, int $length = 20): void
35
    {
36 39
        if (mb_strlen($value) > $length) {
37 1
            throw new MessengerException(sprintf('String should not exceed %s characters.', $length));
38
        }
39 38
    }
40
41
    /**
42
     * @param string $value
43
     *
44
     * @throws \Kerox\Messenger\Exception\MessengerException
45
     */
46 34
    protected function isValidUrl(string $value): void
47
    {
48 34
        if (!preg_match(
49 34
            '/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)$/',
50 34
            $value
51
        )) {
52 1
            throw new MessengerException(sprintf('%s is not a valid url.', $value));
53
        }
54 33
    }
55
56
    /**
57
     * @param string $value
58
     *
59
     * @throws \Kerox\Messenger\Exception\MessengerException
60
     */
61 8
    protected function isValidLocale(string $value): void
62
    {
63 8
        if (!preg_match('/^[a-z]{2}_[A-Z]{2}$/', $value)) {
64 1
            throw new MessengerException(sprintf(
65 1
                '%s is not valid. Locale must be in ISO-639-1 and ISO-3166-1 format like fr_FR.',
66 1
                $value
67
            ));
68
        }
69 7
    }
70
71
    /**
72
     * @param string $value
73
     *
74
     * @throws \Kerox\Messenger\Exception\MessengerException
75
     */
76 2
    protected function isValidCountry(string $value): void
77
    {
78 2
        if (!preg_match('/^[A-Z]{2}$/', $value)) {
79 1
            throw new MessengerException(sprintf(
80 1
                '%s is not valid. Country must be in ISO 3166 Alpha-2 format like FR.',
81 1
                $value
82
            ));
83
        }
84 1
    }
85
86
    /**
87
     * @param string $value
88
     *
89
     * @throws \Kerox\Messenger\Exception\MessengerException
90
     */
91 11
    protected function isValidDateTime(string $value): void
92
    {
93 11
        if (!preg_match('/^(\d{4})-(0[1-9]|1[0-2])-([12]\d|0[1-9]|3[01])T(0\d|1\d|2[0-3]):([0-5]\d)$/', $value)) {
94 1
            throw new MessengerException(sprintf(
95 1
                '%s is not valid. DateTime must be in ISO-8601 AAAA-MM-JJThh:mm format.',
96 1
                $value
97
            ));
98
        }
99 10
    }
100
101
    /**
102
     * @param array $array
103
     * @param int   $maxSize
104
     * @param int   $minSize
105
     *
106
     * @throws \Kerox\Messenger\Exception\MessengerException
107
     */
108 28
    protected function isValidArray(array $array, int $maxSize, ?int $minSize = null): void
109
    {
110 28
        $countArray = \count($array);
111 28
        if ($minSize !== null && $countArray < $minSize) {
112 1
            throw new MessengerException(sprintf('The minimum number of items for this array is %d.', $minSize));
113
        }
114 27
        if ($countArray > $maxSize) {
115 3
            throw new MessengerException(sprintf('The maximum number of items for this array is %d.', $maxSize));
116
        }
117 26
    }
118
119
    /**
120
     * @param string $value
121
     *
122
     * @throws \Kerox\Messenger\Exception\MessengerException
123
     */
124 8
    protected function isValidCurrency(string $value): void
125
    {
126 8
        $allowedCurrency = $this->getAllowedCurrency();
127
128 8
        $regex = '/^' . implode('|', $allowedCurrency) . '$/';
129 8
        if (!preg_match($regex, $value)) {
130 1
            throw new MessengerException(sprintf(
131 1
                '%s is not a valid currency. Currency must be in ISO-4217-3 format.',
132 1
                $value
133
            ));
134
        }
135 7
    }
136
137
    /**
138
     * @param string $filename
139
     * @param array  $allowedExtension
140
     *
141
     * @throws \Kerox\Messenger\Exception\MessengerException
142
     */
143 3
    protected function isValidExtension(string $filename, array $allowedExtension): void
144
    {
145 3
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
146 3
        if (empty($ext) || !\in_array($ext, $allowedExtension, true)) {
147 1
            throw new MessengerException(sprintf(
148 1
                '%s does not have a valid extension. Allowed extensions are "%s".',
149 1
                $filename,
150 1
                implode(', ', $allowedExtension)
151
            ));
152
        }
153 2
    }
154
155
    /**
156
     * @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons
157
     * @param array                                                 $allowedButtonsType
158
     *
159
     * @throws \Kerox\Messenger\Exception\MessengerException
160
     */
161 8
    protected function isValidButtons(array $buttons, array $allowedButtonsType): void
162
    {
163
        /** @var \Kerox\Messenger\Model\Common\Button\AbstractButton $button */
164 8
        foreach ($buttons as $button) {
165 8
            if (!$button instanceof AbstractButton) {
166 1
                throw new MessengerException(
167 1
                    sprintf('Array can only contain instance of %s.', AbstractButton::class)
168
                );
169
            }
170
171 7
            if (!\in_array($button->getType(), $allowedButtonsType, true)) {
172 3
                throw new MessengerException(sprintf(
173 3
                    'Buttons can only be an instance of %s.',
174 7
                    implode(', ', $allowedButtonsType)
175
                ));
176
            }
177
        }
178 5
    }
179
180
    /**
181
     * @param $message
182
     *
183
     * @throws \Exception
184
     *
185
     * @return \Kerox\Messenger\Model\Message
186
     */
187 12
    protected function isValidMessage($message): Message
188
    {
189 12
        if ($message instanceof Message) {
190
            return $message;
191
        }
192
193 12
        if (\is_string($message) || $message instanceof Attachment) {
194 11
            return Message::create($message);
195
        }
196
197 1
        throw new MessengerException(sprintf(
198 1
            'message must be a string or an instance of %s or %s.',
199 1
            Message::class,
200 1
            Attachment::class
201
        ));
202
    }
203
204
    /**
205
     * @param string $action
206
     *
207
     * @throws \Kerox\Messenger\Exception\MessengerException
208
     */
209 2
    protected function isValidSenderAction(string $action): void
210
    {
211 2
        $allowedSenderAction = $this->getAllowedSenderAction();
212 2
        if (!\in_array($action, $allowedSenderAction, true)) {
213 1
            throw new MessengerException(sprintf(
214 1
                'action must be either "%s".',
215 1
                implode(', ', $allowedSenderAction)
216
            ));
217
        }
218 1
    }
219
220
    /**
221
     * @param string $notificationType
222
     *
223
     * @throws \Kerox\Messenger\Exception\MessengerException
224
     */
225 5
    protected function isValidNotificationType(string $notificationType): void
226
    {
227 5
        $allowedNotificationType = $this->getAllowedNotificationType();
228 5
        if (!\in_array($notificationType, $allowedNotificationType, true)) {
229 1
            throw new MessengerException(sprintf(
230 1
                'notificationType must be either "%s".',
231 1
                implode(', ', $allowedNotificationType)
232
            ));
233
        }
234 4
    }
235
236
    /**
237
     * @param string                         $tag
238
     * @param \Kerox\Messenger\Model\Message $message
239
     *
240
     * @throws \Kerox\Messenger\Exception\MessengerException
241
     */
242 3
    protected function isValidTag(string $tag, Message $message = null): void
243
    {
244 3
        $allowedTag = $this->getAllowedTag();
245 3
        if (!\in_array($tag, $allowedTag, true)) {
246 1
            throw new MessengerException(sprintf(
247 1
                'tag must be either "%s".',
248 1
                implode(', ', $allowedTag)
249
            ));
250
        }
251
252 2
        if ($tag === SendInterface::TAG_ISSUE_RESOLUTION && $message !== null && !$message instanceof GenericTemplate) {
253 1
            throw new MessengerException(sprintf(
254 1
                'message must be an instance of %s if tag is set to %s.',
255 1
                GenericTemplate::class,
256 1
                SendInterface::TAG_ISSUE_RESOLUTION
257
            ));
258
        }
259 1
    }
260
261
    /**
262
     * @return array
263
     */
264 2
    public function getAllowedSenderAction(): array
265
    {
266
        return [
267 2
            SendInterface::SENDER_ACTION_TYPING_ON,
268 2
            SendInterface::SENDER_ACTION_TYPING_OFF,
269 2
            SendInterface::SENDER_ACTION_MARK_SEEN,
270
        ];
271
    }
272
273
    /**
274
     * @return array
275
     */
276 5
    public function getAllowedNotificationType(): array
277
    {
278
        return [
279 5
            SendInterface::NOTIFICATION_TYPE_REGULAR,
280 5
            SendInterface::NOTIFICATION_TYPE_SILENT_PUSH,
281 5
            SendInterface::NOTIFICATION_TYPE_NO_PUSH,
282
        ];
283
    }
284
285
    /**
286
     * @return array
287
     */
288 3
    public function getAllowedTag(): array
289
    {
290
        return [
291 3
            SendInterface::TAG_BUSINESS_PRODUCTIVITY,
292 3
            SendInterface::TAG_COMMUNITY_ALERT,
293 3
            SendInterface::TAG_CONFIRMED_EVENT_REMINDER,
294 3
            SendInterface::TAG_NON_PROMOTIONAL_SUBSCRIPTION,
295 3
            SendInterface::TAG_PAIRING_UPDATE,
296 3
            SendInterface::TAG_APPLICATION_UPDATE,
297 3
            SendInterface::TAG_ACCOUNT_UPDATE,
298 3
            SendInterface::TAG_PAYMENT_UPDATE,
299 3
            SendInterface::TAG_PERSONAL_FINANCE_UPDATE,
300 3
            SendInterface::TAG_SHIPPING_UPDATE,
301 3
            SendInterface::TAG_RESERVATION_UPDATE,
302 3
            SendInterface::TAG_ISSUE_RESOLUTION,
303 3
            SendInterface::TAG_APPOINTMENT_UPDATE,
304 3
            SendInterface::TAG_GAME_EVENT,
305 3
            SendInterface::TAG_TRANSPORTATION_UPDATE,
306 3
            SendInterface::TAG_FEATURE_FUNCTIONALITY_UPDATE,
307 3
            SendInterface::TAG_TICKET_UPDATE,
308
        ];
309
    }
310
311
    /**
312
     * @return array
313
     */
314 8
    public function getAllowedCurrency(): array
315
    {
316
        return [
317 8
            'SGD',
318
            'RON',
319
            'EUR',
320
            'TRY',
321
            'SEK',
322
            'ZAR',
323
            'HKD',
324
            'CHF',
325
            'NIO',
326
            'JPY',
327
            'ISK',
328
            'TWD',
329
            'NZD',
330
            'CZK',
331
            'AUD',
332
            'THB',
333
            'BOB',
334
            'BRL',
335
            'MXN',
336
            'USD',
337
            'ILS',
338
            'HNL',
339
            'MOP',
340
            'COP',
341
            'UYU',
342
            'CRC',
343
            'DKK',
344
            'QAR',
345
            'PYG',
346
            'CAD',
347
            'INR',
348
            'KRW',
349
            'GTQ',
350
            'AED',
351
            'VEF',
352
            'SAR',
353
            'NOK',
354
            'CNY',
355
            'ARS',
356
            'PLN',
357
            'GBP',
358
            'PEN',
359
            'PHP',
360
            'VND',
361
            'RUB',
362
            'HUF',
363
            'MYR',
364
            'CLP',
365
            'IDR',
366
        ];
367
    }
368
}
369