Completed
Pull Request — master (#38)
by Romain
02:53
created

ValidatorTrait::isValidString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
namespace Kerox\Messenger\Helper;
3
4
use InvalidArgumentException;
5
6
trait ValidatorTrait
7
{
8
9
    /**
10
     * @param string $value
11
     * @return void
12
     * @throws \InvalidArgumentException
13
     */
14 2
    protected function isValidColor(string $value)
15
    {
16 2
        if (!preg_match('/^#[A-Fa-f0-9]{6}$/', $value)) {
17 1
            throw new InvalidArgumentException("The color must be expressed in #rrggbb format.");
18
        }
19 1
    }
20
21
    /**
22
     * @param string $value
23
     * @param int $length
24
     * @return void
25
     * @throws \InvalidArgumentException
26
     */
27 27
    protected function isValidString(string $value, int $length = 20)
28
    {
29 27
        if (mb_strlen($value) > $length) {
30 1
            throw new InvalidArgumentException("String should not exceed {$length} characters.");
31
        }
32 26
    }
33
34
    /**
35
     * @param string $value
36
     * @return void
37
     * @throws \InvalidArgumentException
38
     */
39 30
    protected function isValidUrl(string $value)
40
    {
41 30
        if (!preg_match('/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)$/', $value)) {
42 1
            throw new InvalidArgumentException("{$value} is not a valid url.");
43
        }
44 29
    }
45
46
    /**
47
     * @param string $value
48
     * @return void
49
     * @throws \InvalidArgumentException
50
     */
51 7
    protected function isValidLocale(string $value)
52
    {
53 7
        if (!preg_match('/^[a-z]{2}_[A-Z]{2}$/', $value)) {
54 1
            throw new InvalidArgumentException("{$value} is not valid. Locale must be in ISO-639-1 and ISO-3166-1 format like fr_FR.");
55
        }
56 6
    }
57
58
    /**
59
     * @param string $value
60
     * @return void
61
     * @throws \InvalidArgumentException
62
     */
63 1
    protected function isValidCountry(string $value)
64
    {
65 1
        if (!preg_match('/^[A-Z]{2}$/', $value)) {
66
            throw new InvalidArgumentException("{$value} is not valid. Country must be in ISO 3166 Alpha-2 format like FR.");
67
        }
68 1
    }
69
70
    /**
71
     * @param string $value
72
     * @return void
73
     * @throws \InvalidArgumentException
74
     */
75 14
    protected function isValidDateTime(string $value)
76
    {
77 14
        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)) {
78 1
            throw new InvalidArgumentException("{$value} is not valid. DateTime must be in ISO-8601 AAAA-MM-JJThh:mm format");
79
        }
80 13
    }
81
82
    /**
83
     * @param array $array
84
     * @param int $maxSize
85
     * @param int $minSize
86
     * @return void
87
     * @throws \InvalidArgumentException
88
     */
89 24
    protected function isValidArray(array $array, int $maxSize, int $minSize = null)
90
    {
91 24
        $countArray = count($array);
92 24
        if ($minSize !== null && $countArray < $minSize) {
93 1
            throw new InvalidArgumentException("The minimum number of items for this array is {$minSize}.");
94
        }
95 23
        if ($countArray > $maxSize) {
96 3
            throw new InvalidArgumentException("The maximum number of items for this array is {$maxSize}.");
97
        }
98 22
    }
99
100
    /**
101
     * @param string $value
102
     * @return void
103
     * @throws \InvalidArgumentException
104
     */
105 7
    protected function isValidCurrency(string $value)
106
    {
107 7
        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)) {
108 1
            throw new InvalidArgumentException("{$value} is not a valid currency. Currency must be in ISO-4217-3 format.");
109
        }
110 6
    }
111
112
    /**
113
     * @param string $filename
114
     * @param array $allowedExtension
115
     * @return void
116
     * @throws \InvalidArgumentException
117
     */
118 3
    protected function isValidExtension(string $filename, array $allowedExtension)
119
    {
120 3
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
121 3
        if (empty($ext) || !in_array($ext, $allowedExtension)) {
122 1
            throw new InvalidArgumentException("{$filename} doesn't have a valid extension. Allowed extensions are " . implode(', ', $allowedExtension));
123
        }
124 2
    }
125
}
126