Passed
Push — master ( d8a5ea...240098 )
by Paweł
02:27
created

StringType::checkValueRegex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Items\DataTypes;
15
16
use InvalidArgumentException;
17
use Wszetko\Sitemap\Interfaces\DataType;
18
19
/**
20
 * Class StringType.
21
 *
22
 * @package Wszetko\Sitemap\Items\DataTypes
23
 */
24
class StringType extends AbstractDataType
25
{
26
    /**
27
     * @var int
28
     */
29
    protected $minLength;
30
31
    /**
32
     * @var int
33
     */
34
    protected $maxLength;
35
36
    /**
37
     * @var null|array
38
     */
39
    protected $allowedValues;
40
41
    /**
42
     * @var string
43
     */
44
    protected $regex;
45
46
    /**
47
     * @var string
48
     */
49
    protected $regexGroup;
50
51
    /**
52
     * @var string
53
     */
54
    protected $conversion;
55
56
    /**
57
     * @return null|int
58
     */
59 494
    public function getMinLength(): ?int
60
    {
61 494
        return $this->minLength;
62
    }
63
64
    /**
65
     * @param int $minLength
66
     *
67
     * @return \Wszetko\Sitemap\Items\DataTypes\StringType
68
     */
69 302
    public function setMinLength(int $minLength): self
70
    {
71 302
        $this->minLength = $minLength;
72
73 302
        return $this;
74
    }
75
76
    /**
77
     * @return null|int
78
     */
79 494
    public function getMaxLength(): ?int
80
    {
81 494
        return $this->maxLength;
82
    }
83
84
    /**
85
     * @param int $maxLength
86
     *
87
     * @return self
88
     */
89 302
    public function setMaxLength(int $maxLength): self
90
    {
91 302
        $this->maxLength = $maxLength;
92
93 302
        return $this;
94
    }
95
96
    /**
97
     * @return null|array
98
     */
99 494
    public function getAllowedValues(): ?array
100
    {
101 494
        return $this->allowedValues;
102
    }
103
104
    /**
105
     * @param null|array|string $allowedValues
106
     *
107
     * @return \Wszetko\Sitemap\Items\DataTypes\StringType
108
     */
109 440
    public function setAllowedValues($allowedValues): self
110
    {
111 440
        if (is_string($allowedValues)) {
112 352
            $allowedValues = explode(',', $allowedValues);
113
114 352
            foreach ($allowedValues as $allowedValue) {
115 352
                $this->allowedValues[] = trim($allowedValue);
116
            }
117 90
        } elseif (is_array($allowedValues)) {
118 90
            $this->allowedValues = $allowedValues;
119
        }
120
121 440
        return $this;
122
    }
123
124
    /**
125
     * @param string $regex
126
     * @param string $regexGroup
127
     *
128
     * @return \Wszetko\Sitemap\Items\DataTypes\StringType
129
     */
130 378
    public function setValueRegex(string $regex, string $regexGroup): self
131
    {
132 378
        $this->regex = $regex;
133 378
        $this->regexGroup = $regexGroup;
134
135 378
        return $this;
136
    }
137
138
    /**
139
     * @return null|array
140
     */
141 494
    public function getValueRegex(): ?array
142
    {
143 494
        if (!empty($this->regex) && !empty($this->regexGroup)) {
144 120
            return [$this->regexGroup => $this->regex];
145
        }
146
147 492
        return null;
148
    }
149
150
    /**
151
     * @param string $convertion
152
     *
153
     * @return $this
154
     */
155 360
    public function setConversion(string $convertion): self
156
    {
157 360
        if (in_array($convertion, ['upper', 'UPPER', 'Upper'])) {
158 302
            $this->conversion = 'upper';
159 360
        } elseif (in_array($convertion, ['lower', 'LOWER', 'Lower'])) {
160 360
            $this->conversion = 'lower';
161
        }
162
163 360
        return $this;
164
    }
165
166
    /**
167
     * @return string|null
168
     */
169 494
    public function getConversion(): ?string
170
    {
171 494
        return $this->conversion;
172
    }
173
174
    /**
175
     * @param null|float|int|object|string $value
176
     * @param array                        $parameters
177
     *
178
     * @return self
179
     */
180 494
    public function setValue($value, ...$parameters): DataType
181
    {
182 494
        if (null !== $value) {
183 494
            $value = (string) $value;
184 494
            $this->checkValue($value);
185
        }
186
187 494
        if (empty($value) && $this->isRequired()) {
188 14
            throw new InvalidArgumentException($this->getName() . ' need to be set.');
189
        }
190
191 488
        parent::setValue($value, $parameters[0] ?? []);
192
193 488
        return $this;
194
    }
195
196
    /**
197
     * @param $value
198
     */
199 494
    private function checkValue(&$value)
200
    {
201 494
        $value = trim($value);
202 494
        $this->checkValueLength($value);
203 494
        $this->convertValue($value);
204 494
        $this->checkIfValueIsInAllowedValues($value);
205 494
        $this->checkValueRegex($value);
206 494
    }
207
208
    /**
209
     * @param $value
210
     */
211 494
    private function checkValueLength(&$value)
212
    {
213 494
        if (null !== $this->getMinLength() && mb_strlen($value) < $this->getMinLength()) {
214 4
            $value = null;
215
        }
216
217 494
        if (null !== $this->getMaxLength() && null !== $value && mb_strlen($value) > $this->getMaxLength()) {
218 6
            $value = mb_substr($value, 0, $this->getMaxLength());
219
        }
220 494
    }
221
222
    /**
223
     * @param $value
224
     */
225 494
    private function convertValue(&$value)
226
    {
227 494
        if ($conversion = $this->getConversion()) {
228 102
            if ('upper' == $conversion) {
229 36
                $value = mb_strtoupper($value);
230 88
            } elseif ('lower' == $conversion) {
231 88
                $value = mb_strtolower($value);
232
            }
233
        }
234 494
    }
235
236
    /**
237
     * @param $value
238
     */
239 494
    private function checkIfValueIsInAllowedValues(&$value)
240
    {
241 494
        if (!empty($this->getAllowedValues())) {
242 68
            $match = preg_grep("/{$value}/i", $this->getAllowedValues());
243
244 68
            if (empty($match)) {
245 12
                $value = null;
246
            } else {
247 64
                $value = array_values($match)[0];
248
            }
249
        }
250 494
    }
251
252
    /**
253
     * @param $value
254
     */
255 494
    private function checkValueRegex(&$value)
256
    {
257 494
        $regex = $this->getValueRegex();
258
259 494
        if (!empty($regex)) {
260 120
            preg_match_all(array_values($regex)[0], $value, $matches);
261
262 120
            if (empty($matches[array_key_first($regex)])) {
263 6
                $value = null;
264
            }
265
        }
266 494
    }
267
}
268