Completed
Pull Request — master (#47)
by Romain
02:37
created

ValidatorTrait::isValidButtons()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Kerox\Messenger\Helper;
4
5
use InvalidArgumentException;
6
7
trait ValidatorTrait
8
{
9
10
    /**
11
     * @param string $value
12
     * @return void
13
     * @throws \InvalidArgumentException
14
     */
15 2
    protected function isValidColor(string $value)
16
    {
17 2
        if (!preg_match('/^#[A-Fa-f0-9]{6}$/', $value)) {
18 1
            throw new InvalidArgumentException("The color must be expressed in #rrggbb format.");
19
        }
20 1
    }
21
22
    /**
23
     * @param string $value
24
     * @param int $length
25
     * @return void
26
     * @throws \InvalidArgumentException
27
     */
28 28
    protected function isValidString(string $value, int $length = 20)
29
    {
30 28
        if (mb_strlen($value) > $length) {
31 1
            throw new InvalidArgumentException("String should not exceed {$length} characters.");
32
        }
33 27
    }
34
35
    /**
36
     * @param string $value
37
     * @return void
38
     * @throws \InvalidArgumentException
39
     */
40 30
    protected function isValidUrl(string $value)
41
    {
42 30
        if (!preg_match('/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)$/', $value)) {
43 1
            throw new InvalidArgumentException("{$value} is not a valid url.");
44
        }
45 29
    }
46
47
    /**
48
     * @param string $value
49
     * @return void
50
     * @throws \InvalidArgumentException
51
     */
52 7
    protected function isValidLocale(string $value)
53
    {
54 7
        if (!preg_match('/^[a-z]{2}_[A-Z]{2}$/', $value)) {
55 1
            throw new InvalidArgumentException("{$value} is not valid. Locale must be in ISO-639-1 and ISO-3166-1 format like fr_FR.");
56
        }
57 6
    }
58
59
    /**
60
     * @param string $value
61
     * @return void
62
     * @throws \InvalidArgumentException
63
     */
64 2
    protected function isValidCountry(string $value)
65
    {
66 2
        if (!preg_match('/^[A-Z]{2}$/', $value)) {
67 1
            throw new InvalidArgumentException("{$value} is not valid. Country must be in ISO 3166 Alpha-2 format like FR.");
68
        }
69 1
    }
70
71
    /**
72
     * @param string $value
73
     * @return void
74
     * @throws \InvalidArgumentException
75
     */
76 10
    protected function isValidDateTime(string $value)
77
    {
78 10
        if (!preg_match('/^(\d{4})-(0[1-9]|1[0-2])-([12]\d|0[1-9]|3[01])T(0[0-9]|1\d|2[0-3]):([0-5]\d)$/', $value)) {
79 1
            throw new InvalidArgumentException("{$value} is not valid. DateTime must be in ISO-8601 AAAA-MM-JJThh:mm format");
80
        }
81 9
    }
82
83
    /**
84
     * @param array $array
85
     * @param int $maxSize
86
     * @param int $minSize
87
     * @return void
88
     * @throws \InvalidArgumentException
89
     */
90 24
    protected function isValidArray(array $array, int $maxSize, int $minSize = null)
91
    {
92 24
        $countArray = count($array);
93 24
        if ($minSize !== null && $countArray < $minSize) {
94 1
            throw new InvalidArgumentException("The minimum number of items for this array is {$minSize}.");
95
        }
96 23
        if ($countArray > $maxSize) {
97 3
            throw new InvalidArgumentException("The maximum number of items for this array is {$maxSize}.");
98
        }
99 22
    }
100
101
    /**
102
     * @param string $value
103
     * @return void
104
     * @throws \InvalidArgumentException
105
     */
106 6
    protected function isValidCurrency(string $value)
107
    {
108 6
        if (!preg_match('/^SGD|RON|EUR|TRY|SEK|ZAR|HKD|CHF|NIO|JPY|ISK|TWD|NZD|CZK|AUD|THB|BOB|BRL|MXN|USD|ILS|HNL|MOP|COP|UYU|CRC|DKK|QAR|PYG|CAD|INR|KRW|GTQ|AED|VEF|SAR|NOK|CNY|ARS|PLN|GBP|PEN|PHP|VND|RUB|HUF|MYR|CLP|IDR$/', $value)) {
109 1
            throw new InvalidArgumentException("{$value} is not a valid currency. Currency must be in ISO-4217-3 format.");
110
        }
111 5
    }
112
113
    /**
114
     * @param string $filename
115
     * @param array $allowedExtension
116
     * @return void
117
     * @throws \InvalidArgumentException
118
     */
119 3
    protected function isValidExtension(string $filename, array $allowedExtension)
120
    {
121 3
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
122 3
        if (empty($ext) || !in_array($ext, $allowedExtension)) {
123 1
            throw new InvalidArgumentException("{$filename} doesn't have a valid extension. Allowed extensions are " . implode(', ', $allowedExtension));
124
        }
125 2
    }
126
127
    /**
128
     * @param \Kerox\Messenger\Model\Common\Buttons\AbstractButtons[] $buttons
129
     * @param array $allowedButtonsType
130
     * @return void
131
     */
132 6
    protected function isValidButtons(array $buttons, array $allowedButtonsType)
133
    {
134
        /** @var \Kerox\Messenger\Model\Common\Buttons\AbstractButtons $button */
135 6
        foreach ($buttons as $button) {
136 6
            if (!in_array($button->getType(), $allowedButtonsType)) {
137 6
                throw new \InvalidArgumentException('Buttons can only be an instance of ' . implode(', ', $allowedButtonsType));
138
            }
139
        }
140 5
    }
141
}
142