1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Phootwork package. |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @license MIT License |
8
|
|
|
* @copyright Thomas Gossmann |
9
|
|
|
*/ |
10
|
|
|
namespace phootwork\lang\parts; |
11
|
|
|
|
12
|
|
|
use phootwork\lang\Text; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Internal Text methods |
16
|
|
|
* |
17
|
|
|
* @author Thomas Gossmann |
18
|
|
|
* @author Cristiano Cinotti |
19
|
|
|
*/ |
20
|
|
|
trait InternalPart { |
21
|
|
|
abstract public function length(): int; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @internal |
25
|
|
|
* |
26
|
|
|
* @param int $offset |
27
|
|
|
* |
28
|
|
|
* @return int |
29
|
|
|
*/ |
30
|
24 |
|
protected function prepareOffset(int $offset): int { |
31
|
24 |
|
$len = $this->length(); |
32
|
24 |
|
if ($offset < -$len || $offset > $len) { |
33
|
3 |
|
throw new \InvalidArgumentException('Offset must be in range [-len, len]'); |
34
|
|
|
} |
35
|
|
|
|
36
|
21 |
|
if ($offset < 0) { |
37
|
3 |
|
$offset += $len; |
38
|
|
|
} |
39
|
|
|
|
40
|
21 |
|
return $offset; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @internal |
45
|
|
|
* |
46
|
|
|
* @param int $offset |
47
|
|
|
* @param int $length |
48
|
|
|
* |
49
|
|
|
* @throws \InvalidArgumentException |
50
|
|
|
* |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
14 |
|
protected function prepareLength(int $offset, ?int $length): int { |
54
|
14 |
|
$length = (null === $length) ? ($this->length() - $offset) : ( |
55
|
14 |
|
($length < 0) ? ($length + $this->length() - $offset) : $length); |
56
|
|
|
|
57
|
14 |
|
if ($length < 0) { |
58
|
1 |
|
throw new \InvalidArgumentException('Length too small'); |
59
|
|
|
} |
60
|
|
|
|
61
|
13 |
|
if ($offset + $length > $this->length()) { |
62
|
1 |
|
throw new \InvalidArgumentException('Length too large'); |
63
|
|
|
} |
64
|
|
|
|
65
|
12 |
|
return $length; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @internal |
70
|
|
|
* |
71
|
|
|
* @param string|Text $string |
72
|
|
|
* @param string $name |
73
|
|
|
* |
74
|
|
|
* @throws \InvalidArgumentException |
75
|
|
|
*/ |
76
|
3 |
|
protected function verifyNotEmpty($string, string $name): void { |
77
|
3 |
|
if (empty($string)) { |
78
|
1 |
|
throw new \InvalidArgumentException("$name cannot be empty"); |
79
|
|
|
} |
80
|
2 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @internal |
84
|
|
|
* |
85
|
|
|
* @param int $value |
86
|
|
|
* @param string $name |
87
|
|
|
* |
88
|
|
|
* @throws \InvalidArgumentException |
89
|
|
|
*/ |
90
|
3 |
|
protected function verifyPositive(int $value, string $name): void { |
91
|
3 |
|
if ($value <= 0) { |
92
|
2 |
|
throw new \InvalidArgumentException("$name has to be positive"); |
93
|
|
|
} |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @internal |
98
|
|
|
* |
99
|
|
|
* @param int $value |
100
|
|
|
* @param string $name |
101
|
|
|
* |
102
|
|
|
* @throws \InvalidArgumentException |
103
|
|
|
*/ |
104
|
2 |
|
protected function verifyNotNegative(int $value, string $name): void { |
105
|
2 |
|
if ($value < 0) { |
106
|
1 |
|
throw new \InvalidArgumentException("$name can not be negative"); |
107
|
|
|
} |
108
|
1 |
|
} |
109
|
|
|
} |
110
|
|
|
|