Completed
Push — master ( 7fe6b0...d8a5ea )
by Paweł
02:19
created

StringType::getMinLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 300
    public function setMinLength(int $minLength): self
70
    {
71 300
        $this->minLength = $minLength;
72
73 300
        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 300
    public function setMaxLength(int $maxLength): self
90
    {
91 300
        $this->maxLength = $maxLength;
92
93 300
        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 434
    public function setAllowedValues($allowedValues): self
110
    {
111 434
        if (is_string($allowedValues)) {
112 346
            $allowedValues = explode(',', $allowedValues);
113
114 346
            foreach ($allowedValues as $allowedValue) {
115 346
                $this->allowedValues[] = trim($allowedValue);
116
            }
117 90
        } elseif (is_array($allowedValues)) {
118 90
            $this->allowedValues = $allowedValues;
119
        }
120
121 434
        return $this;
122
    }
123
124
    /**
125
     * @param string $regex
126
     * @param string $regexGroup
127
     *
128
     * @return \Wszetko\Sitemap\Items\DataTypes\StringType
129
     */
130 372
    public function setValueRegex(string $regex, string $regexGroup): self
131
    {
132 372
        $this->regex = $regex;
133 372
        $this->regexGroup = $regexGroup;
134
135 372
        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 354
    public function setConversion(string $convertion): self
151
    {
152 354
        if (in_array($convertion, ['upper', 'UPPER', 'Upper'])) {
153 296
            $this->conversion = 'upper';
154 354
        } elseif (in_array($convertion, ['lower', 'LOWER', 'Lower'])) {
155 354
            $this->conversion = 'lower';
156
        }
157
158 354
        return $this;
159
    }
160
161 494
    public function getConversion(): ?string
162
    {
163 494
        return $this->conversion;
164
    }
165
166
    /**
167
     * @param null|float|int|object|string $value
168
     * @param array                        $parameters
169
     *
170
     * @return self
171
     */
172 494
    public function setValue($value, ...$parameters): DataType
173
    {
174 494
        if (null !== $value) {
175 494
            $value = (string) $value;
176 494
            $this->checkValue($value);
177
        }
178
179 494
        if (empty($value) && $this->isRequired()) {
180 14
            throw new InvalidArgumentException($this->getName() . ' need to be set.');
181
        }
182
183 488
        parent::setValue($value, $parameters[0] ?? []);
184
185 488
        return $this;
186
    }
187
188
    /**
189
     * @param $value
190
     */
191 494
    private function checkValue(&$value)
192
    {
193 494
        $value = trim($value);
194
195 494
        if (null !== $this->getMinLength() && mb_strlen($value) < $this->getMinLength()) {
196 4
            $value = null;
197
        }
198
199 494
        if (null !== $this->getMaxLength() && null !== $value && mb_strlen($value) > $this->getMaxLength()) {
200 6
            $value = mb_substr($value, 0, $this->getMaxLength());
201
        }
202
203 494
        if ($conversion = $this->getConversion()) {
204 102
            if ('upper' == $conversion) {
205 36
                $value = mb_strtoupper($value);
206 88
            } elseif ('lower' == $conversion) {
207 88
                $value = mb_strtolower($value);
208
            }
209
        }
210
211 494
        if (!empty($this->getAllowedValues())) {
212 68
            $match = preg_grep("/{$value}/i", $this->getAllowedValues());
213
214 68
            if (empty($match)) {
215 12
                $value = null;
216
            } else {
217 64
                $value = array_values($match)[0];
218
            }
219
        }
220
221 494
        $regex = $this->getValueRegex();
222
223 494
        if (!empty($regex)) {
224 120
            preg_match_all(array_values($regex)[0], $value, $matches);
225
226 120
            if (empty($matches[array_key_first($regex)])) {
227 6
                $value = null;
228
            }
229
        }
230 494
    }
231
}
232