1
|
|
|
<?php
|
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Dgame\Ensurance; |
4
|
|
|
|
5
|
|
|
/**
|
6
|
|
|
* Class StringEnsurance
|
7
|
|
|
* @package Dgame\Ensurance
|
8
|
|
|
*/
|
9
|
|
|
final class StringEnsurance implements EnsuranceInterface |
10
|
|
|
{ |
11
|
|
|
use EnsuranceTrait;
|
12
|
|
|
|
13
|
|
|
/**
|
14
|
|
|
* StringEnsurance constructor.
|
15
|
|
|
*
|
16
|
|
|
* @param EnsuranceInterface $ensurance
|
17
|
|
|
*/
|
18
|
|
|
public function __construct(EnsuranceInterface $ensurance) |
19
|
|
|
{ |
20
|
|
|
$this->transferEnsurance($ensurance); |
21
|
|
|
}
|
22
|
|
|
|
23
|
12 |
|
/**
|
24
|
|
|
* @param string $str
|
25
|
12 |
|
*
|
26
|
12 |
|
* @return StringEnsurance
|
27
|
|
|
*/
|
28
|
|
|
public function isEqualTo(string $str): self |
29
|
|
|
{ |
30
|
|
|
$this->ensure($this->value === $str)->orThrow('"%s" is not equal to "%s"', $this->value, $str);
|
31
|
|
|
|
32
|
|
|
return $this; |
33
|
1 |
|
}
|
34
|
|
|
|
35
|
1 |
|
/**
|
36
|
|
|
* @param string $str
|
37
|
1 |
|
*
|
38
|
|
|
* @return StringEnsurance
|
39
|
|
|
*/
|
40
|
|
|
public function isNotEqualTo(string $str): self |
41
|
|
|
{ |
42
|
|
|
$this->ensure($this->value !== $str)->orThrow('"%s" is equal to "%s"', $this->value, $str);
|
43
|
|
|
|
44
|
|
|
return $this; |
45
|
1 |
|
}
|
46
|
|
|
|
47
|
1 |
|
/**
|
48
|
|
|
* @param string $pattern
|
49
|
1 |
|
*
|
50
|
|
|
* @return StringEnsurance
|
51
|
|
|
*/
|
52
|
|
|
public function matches(string $pattern): self |
53
|
|
|
{ |
54
|
|
|
$this->ensure((bool) preg_match($pattern, $this->value))->orThrow('"%s" does not match pattern "%s"', $this->value, $pattern);
|
55
|
|
|
|
56
|
|
|
return $this; |
57
|
1 |
|
}
|
58
|
|
|
|
59
|
1 |
|
/**
|
60
|
|
|
* @param int $length
|
61
|
1 |
|
*
|
62
|
|
|
* @return StringEnsurance
|
63
|
|
|
*/
|
64
|
|
|
public function hasLengthOf(int $length): self |
65
|
|
|
{ |
66
|
|
|
$len = strlen($this->value);
|
67
|
|
|
$this->ensure($len === $length)->orThrow('"%s" (%d) has not the length of %d', $this->value, $len, $length);
|
68
|
|
|
|
69
|
1 |
|
return $this; |
70
|
|
|
}
|
71
|
1 |
|
|
72
|
1 |
|
/**
|
73
|
|
|
* @param int $length
|
74
|
1 |
|
*
|
75
|
|
|
* @return StringEnsurance
|
76
|
|
|
*/
|
77
|
|
|
public function isShorterThan(int $length): self |
78
|
|
|
{ |
79
|
|
|
$len = strlen($this->value);
|
80
|
|
|
$this->ensure($len < $length)->orThrow('"%s" (%d) is not shorter than %d', $this->value, $len, $length);
|
81
|
|
|
|
82
|
1 |
|
return $this; |
83
|
|
|
}
|
84
|
1 |
|
|
85
|
1 |
|
/**
|
86
|
|
|
* @param int $length
|
87
|
1 |
|
*
|
88
|
|
|
* @return StringEnsurance
|
89
|
|
|
*/
|
90
|
|
|
public function isShorterOrEqualTo(int $length): self |
91
|
|
|
{ |
92
|
|
|
$len = strlen($this->value);
|
93
|
|
|
$this->ensure($len <= $length)->orThrow('"%s" (%d) is not shorter or equal to %d', $this->value, $len, $length);
|
94
|
|
|
|
95
|
1 |
|
return $this; |
96
|
|
|
}
|
97
|
1 |
|
|
98
|
1 |
|
/**
|
99
|
|
|
* @param int $length
|
100
|
1 |
|
*
|
101
|
|
|
* @return StringEnsurance
|
102
|
|
|
*/
|
103
|
|
|
public function isLongerThan(int $length): self |
104
|
|
|
{ |
105
|
|
|
$len = strlen($this->value);
|
106
|
|
|
$this->ensure($len > $length)->orThrow('"%s" (%d) is longer than %d', $this->value, $len, $length);
|
107
|
|
|
|
108
|
1 |
|
return $this; |
109
|
|
|
}
|
110
|
1 |
|
|
111
|
1 |
|
/**
|
112
|
|
|
* @param int $length
|
113
|
1 |
|
*
|
114
|
|
|
* @return StringEnsurance
|
115
|
|
|
*/
|
116
|
|
|
public function isLongerOrEqualTo(int $length): self |
117
|
|
|
{ |
118
|
|
|
$len = strlen($this->value);
|
119
|
|
|
$this->ensure($len >= $length)->orThrow('"%s" (%d) is not longer or equal to %d', $this->value, $len, $length);
|
120
|
|
|
|
121
|
1 |
|
return $this; |
122
|
|
|
}
|
123
|
1 |
|
|
124
|
1 |
|
/**
|
125
|
|
|
* @param string $str
|
126
|
1 |
|
*
|
127
|
|
|
* @return StringEnsurance
|
128
|
|
|
*/
|
129
|
|
|
public function beginsWith(string $str): self |
130
|
|
|
{ |
131
|
|
|
$len = strlen($str);
|
132
|
|
|
$this->ensure(substr($this->value, 0, $len) === $str)->orThrow('"%s" did not begin with "%s"', $this->value, $str);
|
133
|
|
|
|
134
|
1 |
|
return $this; |
135
|
|
|
}
|
136
|
1 |
|
|
137
|
1 |
|
/**
|
138
|
|
|
* @param string $str
|
139
|
1 |
|
*
|
140
|
|
|
* @return StringEnsurance
|
141
|
|
|
*/
|
142
|
|
|
public function endsWith(string $str): self |
143
|
|
|
{ |
144
|
|
|
$len = strlen($str);
|
145
|
|
|
$this->ensure(substr($this->value, $len * -1) === $str)->orThrow('"%s" did not end with "%s"', $this->value, $str);
|
146
|
|
|
|
147
|
1 |
|
return $this; |
148
|
|
|
}
|
149
|
1 |
|
|
150
|
1 |
|
/**
|
151
|
|
|
* @return StringEnsurance
|
152
|
1 |
|
*/
|
153
|
|
|
public function isCallable(): self |
154
|
|
|
{ |
155
|
|
|
$this->ensure(is_callable($this->value))->orThrow('"%s" is not a callable', $this->value);
|
156
|
|
|
|
157
|
|
|
return $this; |
158
|
1 |
|
}
|
159
|
|
|
|
160
|
1 |
|
/**
|
161
|
1 |
|
* @return ReflectionEnsurance
|
162
|
|
|
* @throws \ReflectionException
|
163
|
|
|
*/
|
164
|
|
|
public function isClass(): ReflectionEnsurance |
165
|
|
|
{ |
166
|
1 |
|
$this->ensure(class_exists($this->value))->orThrow('"%s" is not a callable', $this->value);
|
167
|
|
|
|
168
|
1 |
|
return new ReflectionEnsurance($this); |
169
|
|
|
} |
170
|
|
|
} |