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
|
|
|
public function __construct($thing) |
31
|
|
|
{ |
32
|
|
|
self::testStringableObject($thing); |
33
|
|
|
$this->raw = (string) $thing; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function __get($name) |
40
|
|
|
{ |
41
|
|
|
return $this->$name; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function __toString() |
48
|
|
|
{ |
49
|
|
|
return $this->raw; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// ArrayAccess methods { |
53
|
|
|
|
54
|
|
|
public function offsetExists($offset) |
55
|
|
|
{ |
56
|
|
|
$offset = (int) $offset; |
57
|
|
|
return ($offset >= 0 && $offset < $this->count()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// END ArrayAccess methods } |
61
|
|
|
|
62
|
|
|
// Iterator methods { |
63
|
|
|
|
64
|
|
|
public function key() |
65
|
|
|
{ |
66
|
|
|
return $this->caret; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function next() |
70
|
|
|
{ |
71
|
|
|
$this->caret++; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function rewind() |
75
|
|
|
{ |
76
|
|
|
$this->caret = 0; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function valid() |
80
|
|
|
{ |
81
|
|
|
return ($this->caret < $this->count()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// END Iterator methods } |
85
|
|
|
|
86
|
|
|
abstract public function toArray($delim = '', $limit = null); |
|
|
|
|
87
|
|
|
|
88
|
|
|
abstract public function compareTo($str, $mode = self::NORMAL, $length = 1); |
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return AnyString |
92
|
|
|
*/ |
93
|
|
|
abstract public function escape($mode = self::NORMAL, $charlist = ''); |
94
|
|
|
|
95
|
|
|
abstract public function nextToken($delim); |
|
|
|
|
96
|
|
|
|
97
|
|
|
abstract public function remove($str, $mode = self::NORMAL); |
|
|
|
|
98
|
|
|
|
99
|
|
|
abstract public function repeat($times); |
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $replace |
103
|
|
|
*/ |
104
|
|
|
abstract public function replace($search, $replace, $mode = self::NORMAL); |
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return AnyString |
108
|
|
|
*/ |
109
|
|
|
public function replaceWhole($replacement = '') |
110
|
|
|
{ |
111
|
|
|
return new static($replacement); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
abstract public function resetToken(); |
|
|
|
|
115
|
|
|
|
116
|
|
|
abstract public function times($times); |
|
|
|
|
117
|
|
|
|
118
|
|
|
abstract public function translate($search, $replace = ''); |
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return AnyString |
122
|
|
|
*/ |
123
|
|
|
abstract public function trim($mask = " \t\n\r\0\x0B", $mode = self::BOTH_ENDS); |
124
|
|
|
|
125
|
|
|
abstract public function unescape($mode = self::NORMAL); |
|
|
|
|
126
|
|
|
|
127
|
|
|
abstract public function uuDecode(); |
|
|
|
|
128
|
|
|
|
129
|
|
|
abstract public function uuEncode(); |
|
|
|
|
130
|
|
|
|
131
|
|
|
public function equals($str) |
132
|
|
|
{ |
133
|
|
|
self::testStringableObject($str); |
134
|
|
|
|
135
|
|
|
$str = (string) $str; |
136
|
|
|
return ($str == $this->raw); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
abstract public function isAscii(); |
|
|
|
|
140
|
|
|
|
141
|
|
|
abstract public function isEmpty(); |
|
|
|
|
142
|
|
|
|
143
|
|
|
protected static function testStringableObject($thing) |
144
|
|
|
{ |
145
|
|
|
if (is_string($thing)) { |
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (\is_object($thing) && !\method_exists($thing, '__toString')) { |
150
|
|
|
throw new \InvalidArgumentException( |
151
|
|
|
'Parameter is an object that does not implement __toString() method' |
152
|
|
|
); |
153
|
|
|
} elseif (\is_array($thing)) { |
154
|
|
|
throw new \InvalidArgumentException('Unsure of how to convert array to string'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
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
@return
doc comment to communicate to implementors of these methods what they are expected to return.