1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StringObject; |
4
|
|
|
|
5
|
|
|
abstract class AnyString |
6
|
|
|
{ |
7
|
|
|
protected $raw; |
8
|
|
|
|
9
|
|
|
public function __construct($thing) |
10
|
|
|
{ |
11
|
|
|
self::testStringableObject($thing); |
12
|
|
|
$this->raw = (string) $thing; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @return mixed |
17
|
|
|
*/ |
18
|
|
|
public function __get($name) |
19
|
|
|
{ |
20
|
|
|
return $this->$name; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public function __toString() |
27
|
|
|
{ |
28
|
|
|
return $this->raw; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
abstract public function compareTo($str, $mode = self::NORMAL, $length = 1); |
|
|
|
|
32
|
|
|
|
33
|
|
|
abstract public function escape($mode = self::NORMAL, $charlist = ''); |
|
|
|
|
34
|
|
|
|
35
|
|
|
abstract public function nextToken($delim); |
|
|
|
|
36
|
|
|
|
37
|
|
|
abstract public function remove($str, $mode = self::NORMAL); |
|
|
|
|
38
|
|
|
|
39
|
|
|
abstract public function repeat($times); |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $replace |
43
|
|
|
*/ |
44
|
|
|
abstract public function replace($search, $replace, $mode = self::NORMAL); |
|
|
|
|
45
|
|
|
|
46
|
|
|
abstract public function replaceWhole($replacement = ''); |
|
|
|
|
47
|
|
|
|
48
|
|
|
abstract public function resetToken(); |
|
|
|
|
49
|
|
|
|
50
|
|
|
abstract public function times($times); |
|
|
|
|
51
|
|
|
|
52
|
|
|
abstract public function translate($search, $replace = ''); |
|
|
|
|
53
|
|
|
|
54
|
|
|
abstract public function trim($mask = " \t\n\r\0\x0B", $mode = self::BOTH_ENDS); |
|
|
|
|
55
|
|
|
|
56
|
|
|
abstract public function unescape($mode = self::NORMAL); |
|
|
|
|
57
|
|
|
|
58
|
|
|
abstract public function uuDecode(); |
|
|
|
|
59
|
|
|
|
60
|
|
|
abstract public function uuEncode(); |
|
|
|
|
61
|
|
|
|
62
|
|
|
public function equals($str) |
63
|
|
|
{ |
64
|
|
|
self::testStringableObject($str); |
65
|
|
|
|
66
|
|
|
$str = (string) $str; |
67
|
|
|
return ($str == $this->raw); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
abstract public function isAscii(); |
|
|
|
|
71
|
|
|
|
72
|
|
|
abstract public function isEmpty(); |
|
|
|
|
73
|
|
|
|
74
|
|
|
protected static function testStringableObject($thing) |
75
|
|
|
{ |
76
|
|
|
if (\is_object($thing) && !\method_exists($thing, '__toString')) { |
77
|
|
|
throw new \InvalidArgumentException( |
78
|
|
|
'Parameter is an object that does not implement __toString() method' |
79
|
|
|
); |
80
|
|
|
} elseif (\is_array($thing)) { |
81
|
|
|
throw new \InvalidArgumentException('Unsure of how to convert array to string'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.