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

ValidatorTrait::isValidColor()   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 1
crap 2
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
     * @return void
130
     * @throws \InvalidArgumentException
131
     */
132 6
    protected function isValidButtons(array $buttons)
133
    {
134 6
        $allowedButtonsType = $this->getAllowedButtonsType();
0 ignored issues
show
Bug introduced by
It seems like getAllowedButtonsType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
135
136
        /** @var \Kerox\Messenger\Model\Common\Buttons\AbstractButtons $button */
137 6
        foreach ($buttons as $button) {
138 6
            if (!in_array($button->getType(), $allowedButtonsType)) {
139 6
                throw new \InvalidArgumentException('Buttons can only be an instance of ' . implode(', ', $allowedButtonsType));
140
            }
141
        }
142 5
    }
143
}
144