|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace StringObject; |
|
4
|
|
|
|
|
5
|
|
|
abstract class AnyString implements \ArrayAccess, \Countable, \Iterator |
|
6
|
|
|
{ |
|
7
|
|
|
// CONSTANTS |
|
8
|
|
|
|
|
9
|
|
|
const START = 0; |
|
10
|
|
|
const END = 1; |
|
11
|
|
|
const BOTH_ENDS = 2; |
|
12
|
|
|
const NORMAL = 0; |
|
13
|
|
|
const CASE_INSENSITIVE = 1; |
|
14
|
|
|
const REVERSE = 2; |
|
15
|
|
|
const EXACT_POSITION = 4; |
|
16
|
|
|
const CURRENT_LOCALE = 2; |
|
17
|
|
|
const NATURAL_ORDER = 4; |
|
18
|
|
|
const FIRST_N = 8; |
|
19
|
|
|
const C_STYLE = 1; |
|
20
|
|
|
const META = 2; |
|
21
|
|
|
const GREEDY = 0; |
|
22
|
|
|
const LAZY = 1; |
|
23
|
|
|
|
|
24
|
|
|
// PROPERTIES |
|
25
|
|
|
|
|
26
|
|
|
protected $raw; |
|
27
|
|
|
protected $caret = 0; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param mixed $thing Anything that can be cast to a string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($thing) |
|
34
|
|
|
{ |
|
35
|
|
|
self::testStringableObject($thing); |
|
36
|
|
|
$this->raw = (string) $thing; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __get($name) |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->$name; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __toString() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->raw; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// ArrayAccess methods { |
|
56
|
|
|
|
|
57
|
|
|
public function offsetExists($offset) |
|
58
|
|
|
{ |
|
59
|
|
|
$offset = (int) $offset; |
|
60
|
|
|
return ($offset >= 0 && $offset < $this->count()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// END ArrayAccess methods } |
|
64
|
|
|
|
|
65
|
|
|
// Iterator methods { |
|
66
|
|
|
|
|
67
|
|
|
public function key() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->caret; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function next() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->caret++; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function rewind() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->caret = 0; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function valid() |
|
83
|
|
|
{ |
|
84
|
|
|
return ($this->caret < $this->count()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// END Iterator methods } |
|
88
|
|
|
|
|
89
|
|
|
public function equals($str) |
|
90
|
|
|
{ |
|
91
|
|
|
self::testStringableObject($str); |
|
92
|
|
|
|
|
93
|
|
|
$str = (string) $str; |
|
94
|
|
|
return ($str == $this->raw); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
abstract public function isAscii(); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
abstract public function isEmpty(); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
abstract public function toArray($delim = '', $limit = null); |
|
105
|
|
|
|
|
106
|
|
|
abstract public function append($str); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
abstract public function compareTo($str, $mode = self::NORMAL, $length = 1); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
public function concat($str) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
return $this->append($str); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return AnyString |
|
117
|
|
|
*/ |
|
118
|
|
|
abstract public function escape($mode = self::NORMAL, $charlist = ''); |
|
119
|
|
|
|
|
120
|
|
|
public function hexDecode() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->replaceWhole(\hex2bin($this->raw)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function hexEncode() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->replaceWhole(\bin2hex($this->raw)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function nextToken($delim) |
|
131
|
|
|
{ |
|
132
|
|
|
if ($this->token) { |
|
|
|
|
|
|
133
|
|
|
return new static(\strtok($delim)); |
|
134
|
|
|
} |
|
135
|
|
|
$this->token = true; |
|
|
|
|
|
|
136
|
|
|
return new static(\strtok($this->raw, $delim)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
abstract public function prepend($str); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
abstract public function remove($str, $mode = self::NORMAL); |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
abstract public function removeSubstr($start, $length = null); |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
abstract public function repeat($times); |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string $replace |
|
149
|
|
|
*/ |
|
150
|
|
|
abstract public function replace($search, $replace, $mode = self::NORMAL); |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
abstract public function replaceSubstr($replacement, $start, $length = null); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return AnyString |
|
156
|
|
|
*/ |
|
157
|
|
|
public function replaceWhole($replacement = '') |
|
158
|
|
|
{ |
|
159
|
|
|
return new static($replacement); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function resetToken() |
|
163
|
|
|
{ |
|
164
|
|
|
$this->token = false; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function times($times) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
return $this->repeat($times); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
abstract public function translate($search, $replace = ''); |
|
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return AnyString |
|
176
|
|
|
*/ |
|
177
|
|
|
abstract public function trim($mask = " \t\n\r\0\x0B", $mode = self::BOTH_ENDS); |
|
178
|
|
|
|
|
179
|
|
|
abstract public function unescape($mode = self::NORMAL); |
|
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
public function uuDecode() |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->replaceWhole(\convert_uudecode($this->raw)); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function uuEncode() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->replaceWhole(\convert_uuencode($this->raw)); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
protected static function testStringableObject($thing) |
|
192
|
|
|
{ |
|
193
|
|
|
if (is_string($thing)) { |
|
194
|
|
|
return true; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
if (\is_object($thing) && !\method_exists($thing, '__toString')) { |
|
198
|
|
|
throw new \InvalidArgumentException( |
|
199
|
|
|
'Parameter is an object that does not implement __toString() method' |
|
200
|
|
|
); |
|
201
|
|
|
} elseif (\is_array($thing)) { |
|
202
|
|
|
throw new \InvalidArgumentException('Unsure of how to convert array to string'); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.