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