|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace StringObject; |
|
4
|
|
|
|
|
5
|
|
|
abstract class AnyStrObj |
|
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
|
|
|
public abstract function compareTo($str, $mode = self::NORMAL, $length = 1); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
public function asciify($removeUnsupported = true) |
|
34
|
|
|
{ |
|
35
|
|
|
$str = $this->raw; |
|
36
|
|
|
foreach (self::$asciimap as $key => $value) { |
|
37
|
|
|
$str = \str_replace($value, $key, $str); |
|
38
|
|
|
} |
|
39
|
|
|
if ($removeUnsupported) { |
|
40
|
|
|
$str = \preg_replace('/[^\x20-\x7E]/u', '', $str); |
|
41
|
|
|
} |
|
42
|
|
|
return new self($str); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public abstract function escape($mode = self::NORMAL, $charlist = ''); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
public abstract function nextToken($delim); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
public abstract function remove($str, $mode = self::NORMAL); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
public abstract function repeat($times); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $replace |
|
55
|
|
|
*/ |
|
56
|
|
|
public abstract function replace($search, $replace, $mode = self::NORMAL); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
public abstract function resetToken(); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
public abstract function times($times); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
public abstract function translate($search, $replace = ''); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
public abstract function trim($mask = " \t\n\r\0\x0B", $mode = self::BOTH_ENDS); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
public abstract function unescape($mode = self::NORMAL); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
public abstract function uuDecode(); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
public abstract function uuEncode(); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
public function equals($str) |
|
73
|
|
|
{ |
|
74
|
|
|
self::testStringableObject($str); |
|
75
|
|
|
|
|
76
|
|
|
$str = (string) $str; |
|
77
|
|
|
return ($str == $this->raw); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public abstract function isAscii(); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
public abstract function isEmpty(); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
protected static function testStringableObject($thing) |
|
85
|
|
|
{ |
|
86
|
|
|
if (\is_object($thing) && !\method_exists($thing, '__toString')) { |
|
87
|
|
|
throw new \InvalidArgumentException( |
|
88
|
|
|
'Parameter is an object that does not implement __toString() method' |
|
89
|
|
|
); |
|
90
|
|
|
} elseif (\is_array($thing)) { |
|
91
|
|
|
throw new \InvalidArgumentException('Unsure of how to convert array to string'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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.