-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWordUtils.php
More file actions
380 lines (338 loc) · 12.4 KB
/
WordUtils.php
File metadata and controls
380 lines (338 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* FlorianWolters\Component\Core\WordUtils
*
* PHP Version 5.3
*
* @author Florian Wolters <wolters.fl@gmail.com>
* @copyright 2010-2014 Florian Wolters (http://blog.florianwolters.de)
* @license https://gnu.org/licenses/lgpl.txt LGPL-3.0+
* @link https://github.com/FlorianWolters/PHP-Component-Core-StringUtils
*/
namespace FlorianWolters\Component\Core;
/**
* The class {@see WordUtils} offers operations on `string`s that contain words.
*
* This class is inspired by the Java class {@link
* https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/text/WordUtils.html
* WordUtils} from the {@link https://commons.apache.org/lang Apache Commons Lang
* Application Programming Interface (API)}.
*
* @see StringUtils
* @since Class available since Release 0.1.0
*/
final class WordUtils
{
// @codeCoverageIgnoreStart
/**
* {@see WordUtils} instances can **NOT** be constructed in standard
* programming.
*
* Instead, the class should be used as:
*
* WordUtils::wrap('foo bar', 20);
*/
protected function __construct()
{
// NOOP
}
// @codeCoverageIgnoreEnd
/* -------------------------------------------------------------------------
* Abbreviating
* ---------------------------------------------------------------------- */
/**
* Abbreviates a string nicely.
*
* This method searches for the first space after the lower limit and
* abbreviates the `string` there. It will also append any `string` passed
* as a parameter to the end of the `string`.
*
* The upper limit can be specified to forcibly abbreviate a `string`.
*
* @param string $inputString The `string` to be abbreviated. If `null` is
* passed, `null` is returned. If the empty `string` is passed, the empty
* `string` is returned.
* @param integer $lowerLimit The lower limit.
* @param integer $upperLimit The upper limit; specify `-1` if no limit is
* desired. If the upper limit is lower than the lower limit, it will be
* adjusted to be the same as the lower limit.
* @param string $append The `string` to be appended to the end of the
* abbreviated `string`. This is appended ONLY if the string was indeed
* abbreviated. The append does not count towards the lower or upper
* limits.
*
* @return string|null The abbreviated `string` or `null` if `null` `string`
* input.
*/
public static function abbreviate(
$inputString,
$lowerLimit,
$upperLimit = -1,
$append = StringUtils::EMPTY_STR
) {
if (true === StringUtils::isEmpty($inputString)) {
return $inputString;
}
$length = StringUtils::length($inputString);
// if the $lowerLimit value is greater than the length of the string,
// set to the length of the string
if ($lowerLimit > $length) {
$lowerLimit = $length;
}
// if the $upperLimit value is -1 (i.e. no limit) or is greater than the
// length of the string, set to the length of the string
if ((-1 === $upperLimit) || $upperLimit > $length) {
$upperLimit = $length;
}
// if $upperLimit is less than $iLower, raise it to lower
if ($upperLimit < $lowerLimit) {
$upperLimit = $lowerLimit;
}
$index = StringUtils::indexOf($inputString, ' ', $lowerLimit);
if (-1 === $index) {
$result = StringUtils::substring($inputString, 0, $upperLimit);
if ($upperLimit !== $length) {
// only if abbreviation has occured do we append the $append
// value
$result .= $append;
}
} elseif ($index > $upperLimit) {
$result = StringUtils::substring($inputString, 0, $upperLimit);
$result .= $append;
} else {
$result = StringUtils::substring($inputString, 0, $index);
$result .= $append;
}
return $result;
}
/* -------------------------------------------------------------------------
* Capitalizing
* ---------------------------------------------------------------------- */
/**
* Capitalizes all the whitespace separated words in a `string`.
*
* Only the first letter of each word is changed. To convert the rest of
* each word to lowercase at the same time, use {@see capitalizeFully}.
*
* A `null` input `string` returns `null`.
*
* Capitalization uses the unicode title case, normally equivalent to upper
* case.
*
* WordUtils::capitalize(null); // null
* WordUtils::capitalize(''); // ''
* WordUtils::capitalize('i am FINE'); // 'I Am FINE'
*
* @param string $inputString The `string` to capitalize.
*
* @return string|null The capitalized `string` or `null` if `null` `string`
* input.
* @see WordUtils::uncapitalize
*/
public static function capitalize($inputString)
{
return \ucwords($inputString);
}
/**
* Converts all the whitespace separated words in a `string` into
* capitalized words, that is each word is made up of a titlecase character
* and then a series of lowercase characters.
*
* A `null` input `string` returns `null`.
* Capitalization uses the Unicode title case, normally equivalent to upper
* case.
*
* WordUtils::capitalizeFully(null); // null
* WordUtils::capitalizeFully(''); // ''
* WordUtils::capitalizeFully('i am FINE'); // 'I Am Fine'
*
* @param string $inputString The `string` to capitalize.
*
* @return string|null The capitalized `string` or `null` if `null` `string`
* input.
*/
public static function capitalizeFully($inputString)
{
$inputStringLower = StringUtils::lowerCase($inputString);
$result = self::capitalize($inputStringLower);
return $result;
}
/**
* Uncapitalizes all the whitespace separated words in a `string`.
*
* Only the first letter of each word is changed. A `null` input `string`
* returns `null`.
*
* WordUtils::uncapitalize(null); // null
* WordUtils::uncapitalize(''); // ''
* WordUtils::uncapitalize('I Am FINE'); // 'i am fINE'
*
* @param string $inputString The `string` to uncapitalize.
*
* @return string The uncapitalized `string` or `null` if `null` `string`
* input.
* @see WordUtils::capitalize
*/
public static function uncapitalize($inputString)
{
$func = function (array $matches) {
return StringUtils::lowerCase($matches[0]);
};
return \preg_replace_callback(
'~\b\w~',
$func,
$inputString
);
}
/**
* Extracts the initial letters from each word in the `string`.
*
* The first letter of the string and all first letters after the defined
* delimiters are returned as a new string. Their case is not changed.
*
* If the delimiters array is `null`, then Whitespace is used.
* A `null` input `string` returns `null`.
* An empty delimiter `array` returns an empty `string`.
*
* WordUtils::initials(null, null); // null
* WordUtils::initials('', null); // ''
* WordUtils::initials('Ben John Lee', null); // 'BJL'
* WordUtils::initials('Ben J.Lee', null); // 'BJ'
* WordUtils::initials('Ben J.Lee', [' ','.']); // 'BJL'
*
* @param string $inputString The `string` to get initials from.
* @param array|null $delimiters Set of characters to determine words,
* `null` means whitespace.
*
* @return string|null `string` of initial letters or `null` if `null`
* `string` input.
*/
public static function initials($inputString, array $delimiters = null)
{
if (true === StringUtils::isEmpty($inputString)) {
return $inputString;
}
if ((null !== $delimiters) && (0 === \count($delimiters))) {
return StringUtils::EMPTY_STR;
}
$inputStringLen = StringUtils::length($inputString);
$buffer = array();
$lastWasGap = true;
for ($i = 0; $i < $inputStringLen; ++$i) {
$char = StringUtils::charAt($inputString, $i);
$delimiter = self::isDelimiter($char, $delimiters);
if (true === $delimiter) {
$lastWasGap = true;
} elseif (true === $lastWasGap) {
$buffer[] = $char;
$lastWasGap = false;
}
}
return \implode($buffer);
}
/**
* Checks whether the specified string is a delimiter.
*
* @param string $char The string to check.
* @param array|null $delimiters The delimiters.
*
* @return boolean `true` if it is a delimiter; `false` otherwise.
*/
private static function isDelimiter($char, array $delimiters = null)
{
if (null === $delimiters) {
return \ctype_space($char);
}
foreach ($delimiters as $delimiter) {
if ($char === $delimiter) {
return true;
}
}
return false;
}
/**
* Swaps the case of a `string` using a word based algorithm.
*
* * Upper case character converts to Lower case
* * Lower case character converts to Upper case
*
* A `null` input `string` returns `null`.
*
* WordUtils::swapCase(null); // null
* WordUtils::swapCase(''); // ''
* WordUtils::swapCase('The dog has a BONE); // 'tHE DOG HAS A bone'
*
* @param string $inputString The `string` to swap case.
*
* @return string|null The changed `string` or `null` if `null` `string`
* input.
*/
public static function swapCase($inputString)
{
if (true === StringUtils::isEmpty($inputString)) {
return $inputString;
}
$buffer = \str_split($inputString);
$length = \count($buffer);
for ($i = 0; $i < $length; ++$i) {
$char = $buffer[$i];
if (true === \ctype_upper($char)) {
$buffer[$i] = StringUtils::lowerCase($char);
} elseif (true === \ctype_lower($char)) {
$buffer[$i] = StringUtils::upperCase($char);
}
}
return \implode($buffer);
}
/* -------------------------------------------------------------------------
* Wrapping
* ---------------------------------------------------------------------- */
/**
* Wraps a single line of text, identifying words by `' '`.
*
* Leading spaces on a new line are stripped. Trailing spaces are not
* stripped.
*
* WordUtils::wrap(null, null, null, null); // null
* WordUtils::wrap('', null, null, null); // ''
*
* @param string $inputString The `string` to be word wrapped.
* @param integer $wrapLength The column to wrap the words at, less than
* `1` is treated as `1`.
* @param string|null $newLineInsertion The `string` to insert for a new
* line, `null` uses the system property line separator.
* @param boolean $wrapLongWords `true` if long words (such as URLs) should
* be wrapped.
*
* @return string|null A line with newlines inserted, `null` if `null`
* input.
*/
public static function wrap(
$inputString,
$wrapLength,
$newLineInsertion = null,
$wrapLongWords = false
) {
if (null === $inputString) {
return null;
}
if (null === $newLineInsertion) {
$newLineInsertion = \PHP_EOL;
}
if (1 > $wrapLength) {
$wrapLength = 1;
}
$wordWrap = \wordwrap(
StringUtils::stripStart($inputString, null),
$wrapLength,
$newLineInsertion,
$wrapLongWords
);
$result = StringUtils::EMPTY_STR;
$split = StringUtils::split($wordWrap, $newLineInsertion);
foreach ($split as $fragment) {
$result .= StringUtils::stripStart($fragment, null) . $newLineInsertion;
}
return StringUtils::removeEnd($result, $newLineInsertion);
}
}