|
1
|
|
|
<?php namespace Gears\String\Methods; |
|
2
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
3
|
|
|
// __________ __ ________ __________ |
|
4
|
|
|
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___ |
|
5
|
|
|
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ / |
|
6
|
|
|
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > < |
|
7
|
|
|
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \ |
|
8
|
|
|
// \/|__| \/ \/ \/ \/ \/ |
|
9
|
|
|
// ----------------------------------------------------------------------------- |
|
10
|
|
|
// Designed and Developed by Brad Jones <brad @="bjc.id.au" /> |
|
11
|
|
|
// ----------------------------------------------------------------------------- |
|
12
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
13
|
|
|
|
|
14
|
|
|
use voku\helper\UTF8; |
|
15
|
|
|
|
|
16
|
|
|
trait Misc |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Normalise all line endings in string to `$newLineEnding`. |
|
20
|
|
|
* |
|
21
|
|
|
* @link http://stackoverflow.com/questions/7836632 |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $newLineEnding Defaults to a LINE FEED. You may provide |
|
24
|
|
|
* any string to replace all line endings. |
|
25
|
|
|
* |
|
26
|
|
|
* @return static |
|
27
|
|
|
*/ |
|
28
|
|
|
public function normaliseLineEndings($newLineEnding = "\n") |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->regexReplace('\R', $newLineEnding); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Returns a lowercase and trimmed string separated by the given delimiter. |
|
35
|
|
|
* |
|
36
|
|
|
* Delimiters are inserted before uppercase characters (with the exception |
|
37
|
|
|
* of the first character of the string), and in place of spaces, dashes, |
|
38
|
|
|
* and underscores. Alpha delimiters are not converted to lowercase. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $delimiter Sequence used to separate parts of the string |
|
41
|
|
|
* |
|
42
|
|
|
* @return static |
|
43
|
|
|
*/ |
|
44
|
|
|
public function delimit($delimiter) |
|
45
|
|
|
{ |
|
46
|
|
|
return $this |
|
47
|
|
|
->trim() |
|
48
|
|
|
->regexReplace('\B([A-Z])', '-\1') |
|
49
|
|
|
->toLowerCase() |
|
50
|
|
|
->regexReplace('[-_\s]+', $delimiter) |
|
51
|
|
|
; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Inserts $substring into the string at the $index provided. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $substring String to be inserted. |
|
58
|
|
|
* |
|
59
|
|
|
* @param int $index The index at which to insert the substring. |
|
60
|
|
|
* |
|
61
|
|
|
* @return static String after the insertion. |
|
62
|
|
|
* |
|
63
|
|
|
* @throws \OutOfBoundsException When the index is greater than the length. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function insert($substring, $index) |
|
66
|
|
|
{ |
|
67
|
|
|
if ($index > $this->getLength()) throw new \OutOfBoundsException(); |
|
68
|
|
|
|
|
69
|
|
|
$start = UTF8::substr |
|
70
|
|
|
( |
|
71
|
|
|
$this->scalarString, |
|
72
|
|
|
0, |
|
73
|
|
|
$index, |
|
74
|
|
|
$this->encoding |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$end = UTF8::substr |
|
78
|
|
|
( |
|
79
|
|
|
$this->scalarString, |
|
80
|
|
|
$index, |
|
81
|
|
|
$this->getLength(), |
|
82
|
|
|
$this->encoding |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
return $this->newSelf($start.$substring.$end); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Splits on newlines and carriage returns. |
|
90
|
|
|
* |
|
91
|
|
|
* @return static[] |
|
92
|
|
|
*/ |
|
93
|
|
|
public function lines() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->split('[\r\n]{1,2}', null, false); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns a repeated string given a multiplier. |
|
100
|
|
|
* |
|
101
|
|
|
* @param int $multiplier The number of times to repeat the string |
|
102
|
|
|
* |
|
103
|
|
|
* @return static |
|
104
|
|
|
*/ |
|
105
|
|
|
public function repeat($multiplier) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->newSelf |
|
108
|
|
|
( |
|
109
|
|
|
UTF8::str_repeat($this->scalarString, $multiplier) |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Returns a reversed string. A multibyte version of strrev(). |
|
115
|
|
|
* |
|
116
|
|
|
* @return static Object with a reversed $str |
|
117
|
|
|
*/ |
|
118
|
|
|
public function reverse() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->newSelf(UTF8::strrev($this->scalarString)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* A multibyte string shuffle function. |
|
125
|
|
|
* |
|
126
|
|
|
* It returns a string with its characters in random order. |
|
127
|
|
|
* |
|
128
|
|
|
* @return static |
|
129
|
|
|
*/ |
|
130
|
|
|
public function shuffle() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->newSelf(UTF8::str_shuffle($this->scalarString)); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns the substring beginning at $start, and up to, but not including |
|
137
|
|
|
* the index specified by $end. If $end is omitted, the function extracts |
|
138
|
|
|
* the remaining string. If $end is negative, it is computed from the end |
|
139
|
|
|
* of the string. |
|
140
|
|
|
* |
|
141
|
|
|
* @param int $start Initial index from which to begin extraction. |
|
142
|
|
|
* |
|
143
|
|
|
* @param int|null $end Optional index at which to end extraction. |
|
144
|
|
|
* |
|
145
|
|
|
* @return static The extracted substring. |
|
146
|
|
|
*/ |
|
147
|
|
|
public function slice($start, $end = null) |
|
148
|
|
|
{ |
|
149
|
|
|
if ($end === null) |
|
150
|
|
|
{ |
|
151
|
|
|
$length = $this->getLength(); |
|
152
|
|
|
} |
|
153
|
|
|
elseif ($end >= 0 && $end <= $start) |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->newSelf(''); |
|
156
|
|
|
} |
|
157
|
|
|
elseif ($end < 0) |
|
158
|
|
|
{ |
|
159
|
|
|
$length = $this->getLength() + $end - $start; |
|
160
|
|
|
} |
|
161
|
|
|
else |
|
162
|
|
|
{ |
|
163
|
|
|
$length = $end - $start; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $this->newSelf |
|
167
|
|
|
( |
|
168
|
|
|
UTF8::substr($this->scalarString, $start, $length, $this->encoding) |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Surrounds string with the given substring. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $substring The substring to add to both sides. |
|
176
|
|
|
* |
|
177
|
|
|
* @return static String with $substring both prepended |
|
178
|
|
|
* and appended. |
|
179
|
|
|
*/ |
|
180
|
|
|
public function surround($substring) |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->newSelf |
|
183
|
|
|
( |
|
184
|
|
|
implode('', [$substring, $this->scalarString, $substring]) |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Returns a string with smart quotes, ellipsis characters, and dashes from |
|
190
|
|
|
* Windows-1252 (commonly used in Word documents) replaced by their ASCII |
|
191
|
|
|
* equivalents. |
|
192
|
|
|
* |
|
193
|
|
|
* @return static |
|
194
|
|
|
*/ |
|
195
|
|
|
public function tidy() |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->newSelf(UTF8::normalize_msword($this->scalarString)); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Returns the number of occurrences of $substring in the given string. |
|
202
|
|
|
* |
|
203
|
|
|
* By default, the comparison is case-sensitive, but can be made |
|
204
|
|
|
* insensitive by setting $caseSensitive to false. |
|
205
|
|
|
* |
|
206
|
|
|
* @param string $substring The substring to search for. |
|
207
|
|
|
* |
|
208
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity. |
|
209
|
|
|
* |
|
210
|
|
|
* @return int The number of $substring occurrences |
|
|
|
|
|
|
211
|
|
|
*/ |
|
212
|
|
|
public function countSubstr($substring, $caseSensitive = true) |
|
213
|
|
|
{ |
|
214
|
|
|
if (!isset($this->scalarString[0])) |
|
215
|
|
|
{ |
|
216
|
|
|
return 0; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
if ($caseSensitive) |
|
220
|
|
|
{ |
|
221
|
|
|
return UTF8::substr_count |
|
222
|
|
|
( |
|
223
|
|
|
$this->scalarString, |
|
224
|
|
|
$substring |
|
225
|
|
|
); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
$str = UTF8::strtoupper($this->scalarString, $this->encoding); |
|
229
|
|
|
$substring = UTF8::strtoupper($substring, $this->encoding); |
|
230
|
|
|
return UTF8::substr_count($str, $substring); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Formats the current string, using the provided array of arguments. |
|
235
|
|
|
* |
|
236
|
|
|
* For details on the syntax of the $format string: |
|
237
|
|
|
* http://php.net/manual/en/function.sprintf.php |
|
238
|
|
|
* |
|
239
|
|
|
* @param array $args The arguments that will be inserted |
|
240
|
|
|
* into the $format string. |
|
241
|
|
|
* |
|
242
|
|
|
* @return static |
|
243
|
|
|
*/ |
|
244
|
|
|
public function format($args) |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->newSelf(vsprintf($this->scalarString, $args)); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.