1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Defines \TraderInteractive\Util\Strings class. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace TraderInteractive\Util; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Static class with various string functions. |
10
|
|
|
*/ |
11
|
|
|
final class Strings |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Replaces the format items in a specified string with the string representation of n specified objects. |
15
|
|
|
* |
16
|
|
|
* @param string $format A composit format string |
17
|
|
|
* @param mixed $arguments Variable number of items to format. |
18
|
|
|
* |
19
|
|
|
* @return string Returns a copy of format in which the format items have been |
20
|
|
|
* replaced by the string representations of arg0, arg1,... argN. |
21
|
|
|
*/ |
22
|
|
|
public static function format(string $format, string ...$arguments) : string |
23
|
|
|
{ |
24
|
|
|
foreach ($arguments as $key => $value) { |
25
|
|
|
$format = str_replace("{{$key}}", (string)$value, $format); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $format; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Checks if $string ends with $suffix and puts the rest of the $string in $nonSuffix. |
33
|
|
|
* |
34
|
|
|
* @param string $string The string to check |
35
|
|
|
* @param string $suffix The suffix to check for |
36
|
|
|
* @param mixed &$nonSuffix This is the part of the string that is not the suffix. |
37
|
|
|
* |
38
|
|
|
* @return bool whether the $string ended with $suffix or not. |
39
|
|
|
*/ |
40
|
|
|
public static function endsWith(string $string, string $suffix, &$nonSuffix = null) : bool |
41
|
|
|
{ |
42
|
|
|
$suffixLength = strlen($suffix); |
43
|
|
|
|
44
|
|
|
if ($suffixLength === 0) { |
45
|
|
|
$nonSuffix = $string; |
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (empty($string)) { |
50
|
|
|
$nonSuffix = ''; |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (substr_compare($string, $suffix, -$suffixLength, $suffixLength) !== 0) { |
55
|
|
|
$nonSuffix = $string; |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$nonSuffix = substr($string, 0, -$suffixLength); |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Truncates the string to the given length, with an ellipsis at the end. |
65
|
|
|
* |
66
|
|
|
* @param string $string The string to shorten. |
67
|
|
|
* @param int $maxLength The length to truncate the string to. The result will not be longer than this, but may be |
68
|
|
|
* shorter. |
69
|
|
|
* @param string $suffix The string to append when truncating. Typically this will be an ellipsis. |
70
|
|
|
* |
71
|
|
|
* @return string The truncated string with the ellipsis included if truncation occured. |
72
|
|
|
* |
73
|
|
|
* @throws \InvalidArgumentException if $maxLength is negative |
74
|
|
|
*/ |
75
|
|
|
public static function ellipsize(string $string, int $maxLength, string $suffix = '...') : string |
76
|
|
|
{ |
77
|
|
|
if ($maxLength < 0) { |
78
|
|
|
throw new \InvalidArgumentException('$maxLength is negative'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (strlen($string) <= $maxLength) { |
82
|
|
|
return $string; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$trimmedLength = $maxLength - strlen($suffix); |
86
|
|
|
$string = substr($string, 0, max(0, $trimmedLength)); |
87
|
|
|
|
88
|
|
|
if ($string === '') { |
89
|
|
|
return substr($suffix, 0, $maxLength); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $string . $suffix; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|