100% of 592OPs |
96% of 72Lines |
79% of 63Branches |
54% of 26Paths |
# | |
---|---|
1 |
<?php |
2 | |
3 |
namespace mageekguy\atoum\asserters; |
4 | |
5 |
use |
6 |
mageekguy\atoum, |
7 |
mageekguy\atoum\tools, |
8 |
mageekguy\atoum\asserter, |
9 |
mageekguy\atoum\asserters, |
10 |
mageekguy\atoum\exceptions |
11 |
; |
12 | |
13 |
class utf8String extends asserters\phpString |
14 |
{ |
15 |
protected $adapter = null; |
16 | |
17 |
public function __construct(asserter\generator $generator = null, tools\variable\analyzer $analyzer = null, atoum\locale $locale = null)100% |
18 |
{ |
19 |
if (extension_loaded('mbstring') === false) |
20 |
{ |
21 |
throw new exceptions\runtime('mbstring PHP extension is mandatory to use utf8String asserter'); |
22 |
} |
23 | |
24 |
parent::__construct($generator, $analyzer, $locale); |
25 |
} |
26 | |
27 |
public function __toString()100% |
28 |
{ |
29 |
return (is_string($this->value) === false ? parent::__toString() : $this->_('string(%s) \'%s\'', mb_strlen($this->value, 'UTF-8'), addcslashes($this->value, $this->charlist))); |
30 |
} |
31 | |
32 |
public function setWith($value, $charlist = null, $checkType = true)100% |
33 |
{ |
34 |
parent::setWith($value, $charlist, $checkType); |
35 | |
36 |
if ($checkType === true) |
37 |
{ |
38 |
if ($this->analyzer->isUtf8($this->value) === true) |
39 |
{ |
40 |
$this->pass(); |
41 |
} |
42 |
else |
43 |
{ |
44 |
$this->fail($this->_('%s is not an UTF-8 string', $this)); |
45 |
} |
46 |
} |
47 | |
48 |
return $this; |
49 |
} |
50 | |
51 |
public function hasLength($length, $failMessage = null)100% |
52 |
{ |
53 |
if (mb_strlen($this->valueIsSet()->value, 'UTF-8') == $length) |
54 |
{ |
55 |
$this->pass(); |
56 |
} |
57 |
else |
58 |
{ |
59 |
$this->fail($failMessage ?: $this->_('length of %s is not %d', $this, $length)); |
60 |
} |
61 | |
62 |
return $this; |
63 |
} |
64 | |
65 |
public function hasLengthGreaterThan($length, $failMessage = null)100% |
66 |
{ |
67 |
if (mb_strlen($this->valueIsSet()->value, 'UTF-8') > $length) |
68 |
{ |
69 |
$this->pass(); |
70 |
} |
71 |
else |
72 |
{ |
73 |
$this->fail($failMessage ?: $this->_('length of %s is not greater than %d', $this, $length)); |
74 |
} |
75 | |
76 |
return $this; |
77 |
} |
78 | |
79 |
public function hasLengthLessThan($length, $failMessage = null)100% |
80 |
{ |
81 |
if (mb_strlen($this->valueIsSet()->value, 'UTF-8') < $length) |
82 |
{ |
83 |
$this->pass(); |
84 |
} |
85 |
else |
86 |
{ |
87 |
$this->fail($failMessage ?: $this->_('length of %s is not less than %d', $this, $length)); |
88 |
} |
89 | |
90 |
return $this; |
91 |
} |
92 | |
93 |
public function contains($fragment, $failMessage = null)100% |
94 |
{ |
95 |
if (mb_strpos($this->valueIsSet()->value, $fragment, 0, 'UTF-8') !== false) |
96 |
{ |
97 |
$this->pass(); |
98 |
} |
99 |
else |
100 |
{ |
101 |
$this->fail($failMessage ?: $this->_('%s does not contain %s', $this, $fragment)); |
102 |
} |
103 | |
104 |
return $this; |
105 |
} |
106 | |
107 |
public function notContains($fragment, $failMessage = null)83% |
108 |
{ |
109 |
if (mb_strpos($this->valueIsSet()->value, $fragment, 0, 'UTF-8') !== false) |
110 |
{ |
111 |
$this->fail($failMessage ?: $this->_('%s contains %s', $this, $fragment)); |
112 |
} |
113 |
else |
114 |
{ |
115 |
$this->pass(); |
116 |
} |
117 | |
118 |
return $this; |
119 |
} |
120 | |
121 |
public function startWith($fragment, $failMessage = null)100% |
122 |
{ |
123 |
if (mb_strpos($this->valueIsSet()->value, $fragment, 0, 'UTF-8') === 0) |
124 |
{ |
125 |
$this->pass(); |
126 |
} |
127 |
else |
128 |
{ |
129 |
$this->fail($failMessage ?: $this->_('%s does not start with %s', $this, $fragment)); |
130 |
} |
131 | |
132 |
return $this; |
133 |
} |
134 | |
135 |
public function notStartWith($fragment, $failMessage = null)86% |
136 |
{ |
137 |
if (mb_strpos($this->valueIsSet()->value, $fragment, 0, 'UTF-8') === 0) |
138 |
{ |
139 |
$this->fail($failMessage ?: $this->_('%s start with %s', $this, $fragment)); |
140 |
} |
141 |
else |
142 |
{ |
143 |
$this->pass(); |
144 |
} |
145 | |
146 |
return $this; |
147 |
} |
148 | |
149 |
public function endWith($fragment, $failMessage = null)100% |
150 |
{ |
151 |
if (mb_strpos($this->valueIsSet()->value, $fragment, 0, 'UTF-8') === (mb_strlen($this->valueIsSet()->value, 'UTF-8') - mb_strlen($fragment, 'UTF-8'))) |
152 |
{ |
153 |
$this->pass(); |
154 |
} |
155 |
else |
156 |
{ |
157 |
$this->fail($failMessage ?: $this->_('%s does not end with %s', $this, $fragment)); |
158 |
} |
159 | |
160 |
return $this; |
161 |
} |
162 | |
163 |
public function notEndWith($fragment, $failMessage = null)83% |
164 |
{ |
165 |
if (mb_strpos($this->valueIsSet()->value, $fragment, 0, 'UTF-8') === (mb_strlen($this->valueIsSet()->value, 'UTF-8') - mb_strlen($fragment, 'UTF-8'))) |
166 |
{ |
167 |
$this->fail($failMessage ?: $this->_('%s end with %s', $this, $fragment)); |
168 |
} |
169 |
else |
170 |
{ |
171 |
$this->pass(); |
172 |
} |
173 | |
174 |
return $this; |
175 |
} |
176 | |
177 |
protected function getLengthAsserter()100% |
178 |
{ |
179 |
return $this->generator->__call('integer', array(mb_strlen($this->valueIsSet()->value, 'UTF-8'))); |
180 |
} |
181 |
} |