1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Matthew |
5
|
|
|
* Date: 9/11/2015 |
6
|
|
|
* Time: 12:16 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Mpclarkson\Stringy; |
10
|
|
|
|
11
|
|
|
class Stringy { |
12
|
|
|
|
13
|
|
|
private $text; |
14
|
|
|
|
15
|
|
|
public function __construct($string) |
16
|
|
|
{ |
17
|
|
|
if (!is_string($string)) { |
18
|
|
|
throw new StringyException(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$this->text = trim($string); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function string() |
28
|
|
|
{ |
29
|
|
|
return $this->text; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param int $chars |
34
|
|
|
* @param string $appendWith |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
public function truncate($chars = 50, $appendWith = "...") |
38
|
|
|
{ |
39
|
|
|
$len = strlen($this->text); |
40
|
|
|
if ($len > $chars) { |
41
|
|
|
$this->text = substr($this->text, 0, $chars) . $appendWith; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
|
|
public function length() |
51
|
|
|
{ |
52
|
|
|
return strlen($this->text); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $substring |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function contains($substring) |
60
|
|
|
{ |
61
|
|
|
return strpos($this->text, $substring) !== false ? true : false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $substring |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function startsWith($substring) |
69
|
|
|
{ |
70
|
|
|
return strrpos($this->text, $substring, -strlen($substring)) !== false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $substring |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function endsWith($substring) |
78
|
|
|
{ |
79
|
|
|
return ($temp = strlen($this->text) - strlen($substring)) >= 0 && strpos($this->text, $substring, $temp) !== false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $string |
84
|
|
|
* @param string $separator |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function append($string, $separator = " ") |
88
|
|
|
{ |
89
|
|
|
$this->text = $this->text . $separator . $string; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function reverse() |
97
|
|
|
{ |
98
|
|
|
$this->text = strrev($this->text); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function uppercase() |
106
|
|
|
{ |
107
|
|
|
$this->text = strtoupper($this->text); |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function lowercase() |
115
|
|
|
{ |
116
|
|
|
$this->text = strtolower($this->text); |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function lowercaseFirst() |
124
|
|
|
{ |
125
|
|
|
$this->text = lcfirst($this->text); |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function uppercaseFirst() |
133
|
|
|
{ |
134
|
|
|
$this->text = ucfirst($this->text); |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
|
|
public function titleCase() |
142
|
|
|
{ |
143
|
|
|
$this->text = ucwords($this->text); |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function sentenceCase() |
151
|
|
|
{ |
152
|
|
|
$results = []; |
153
|
|
|
|
154
|
|
|
$arr = explode(".", $this->text); |
155
|
|
|
|
156
|
|
|
foreach ($arr as $sentence) { |
157
|
|
|
$results[] = ucfirst(trim($sentence)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->text = implode(". ", $results); |
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param $delimiter |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
public function toArray($delimiter = null) |
169
|
|
|
{ |
170
|
|
|
return !$delimiter ? str_split($this->text) : explode($delimiter, $this->text); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param callable $function |
175
|
|
|
* @return $this |
176
|
|
|
*/ |
177
|
|
|
public function apply(callable $function) |
178
|
|
|
{ |
179
|
|
|
$arr = str_split($this->text); |
180
|
|
|
|
181
|
|
|
$result = []; |
182
|
|
|
|
183
|
|
|
foreach ($arr as $char) { |
184
|
|
|
$result[] = $function($char); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$this->text = implode("", $result); |
188
|
|
|
return $this; |
189
|
|
|
|
190
|
|
|
} |
191
|
|
|
} |