1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stringy; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* INFO: "Method Parameter Information" via PhpStorm | |
9
|
|
|
* https://www.jetbrains.com/phpstorm/help/viewing-method-parameter-information.html |
10
|
|
|
* |
11
|
|
|
* @method static Stringy append(string $stringInput, string $stringAppend, string $encoding = null) |
12
|
|
|
* @method static Stringy appendPassword(string $stringInput, int $length) |
13
|
|
|
* @method static Stringy appendUniqueIdentifier(string $stringInput, string $extraPrefix = '') |
14
|
|
|
* @method static Stringy appendRandomString(string $stringInput, int $length, string $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') |
15
|
|
|
* @method static Stringy at(string $stringInput, int $index, string $encoding = null) |
16
|
|
|
* @method static Stringy between(string $stringInput, string $start, string $end, int $offset = 0, string $encoding = null) |
17
|
|
|
* @method static Stringy camelize(string $stringInput, string $encoding = null) |
18
|
|
|
* @method static string[] chars(string $stringInput, string $encoding = null) |
19
|
|
|
* @method static Stringy collapseWhitespace(string $stringInput, string $encoding = null) |
20
|
|
|
* @method static bool contains(string $stringInput, string $needle, bool $caseSensitive = true, string $encoding = null) |
21
|
|
|
* @method static bool containsAll(string $stringInput, array $needle, bool $caseSensitive = true, string $encoding = null) |
22
|
|
|
* @method static bool containsAny(string $stringInput, string $needle, bool $caseSensitive = true, string $encoding = null) |
23
|
|
|
* @method static int count(string $stringInput, string $encoding = null) |
24
|
|
|
* @method static int countSubstr(string $stringInput, string $substring, bool $caseSensitive = true, string $encoding = null) |
25
|
|
|
* @method static Stringy dasherize(string $stringInput, string $encoding = null) |
26
|
|
|
* @method static Stringy delimit(string $stringInput, string $delimiter, string $encoding = null) |
27
|
|
|
* @method static bool endsWith(string $stringInput, string $substring, bool $caseSensitive = true, string $encoding = null) |
28
|
|
|
* @method static Stringy ensureLeft(string $stringInput, string $substring, string $encoding = null) |
29
|
|
|
* @method static Stringy ensureRight(string $stringInput, string $substring, string $encoding = null) |
30
|
|
|
* @method static Stringy escape(string $stringInput, string $encoding = null) |
31
|
|
|
* @method static Stringy extractText(string $stringInput, string $search = '', int $length = null, string $ellipsis = '...') |
32
|
|
|
* @method static Stringy first(string $stringInput, int $n, string $encoding = null) |
33
|
|
|
* @method static bool hasLowerCase(string $stringInput, string $encoding = null) |
34
|
|
|
* @method static bool hasUpperCase(string $stringInput, string $encoding = null) |
35
|
|
|
* @method static Stringy htmlDecode(string $stringInput, int $flags = ENT_COMPAT, string $encoding = null) |
36
|
|
|
* @method static Stringy htmlEncode(string $stringInput, int $flags = ENT_COMPAT, string $encoding = null) |
37
|
|
|
* @method static Stringy humanize(string $stringInput, string $encoding = null) |
38
|
|
|
* @method static int|bool indexOf(string $stringInput, string $needle, int $offset = 0, string $encoding = null) |
39
|
|
|
* @method static int|bool indexOfLast(string $stringInput, string $needle, int $offset = 0, string $encoding = null) |
40
|
|
|
* @method static Stringy insert(string $stringInput, string $substring, int $index = 0, string $encoding = null) |
41
|
|
|
* @method static bool is(string $stringInput, string $pattern, string $encoding = null) |
42
|
|
|
* @method static bool isAlpha(string $stringInput, string $encoding = null) |
43
|
|
|
* @method static bool isAlphanumeric(string $stringInput, string $encoding = null) |
44
|
|
|
* @method static bool isBase64(string $stringInput, string $encoding = null) |
45
|
|
|
* @method static bool isBlank(string $stringInput, string $encoding = null) |
46
|
|
|
* @method static bool isHexadecimal(string $stringInput, string $encoding = null) |
47
|
|
|
* @method static bool isHtml(string $stringInput, string $encoding = null) |
48
|
|
|
* @method static bool isJson(string $stringInput, string $encoding = null) |
49
|
|
|
* @method static bool isLowerCase(string $stringInput, string $encoding = null) |
50
|
|
|
* @method static bool isSerialized(string $stringInput, string $encoding = null) |
51
|
|
|
* @method static bool isUpperCase(string $stringInput, string $encoding = null) |
52
|
|
|
* @method static Stringy last(string $stringInput, string $encoding = null) |
53
|
|
|
* @method static int length(string $stringInput, string $encoding = null) |
54
|
|
|
* @method static string lineWrapAfterWord(string $stringInput, int $limit) |
55
|
|
|
* @method static Stringy[] lines(string $stringInput, string $encoding = null) |
56
|
|
|
* @method static Stringy longestCommonPrefix(string $stringInput, string $otherStr, string $encoding = null) |
57
|
|
|
* @method static Stringy longestCommonSuffix(string $stringInput, string $otherStr, string $encoding = null) |
58
|
|
|
* @method static Stringy longestCommonSubstring(string $stringInput, string $otherStr, string $encoding = null) |
59
|
|
|
* @method static Stringy lowerCaseFirst(string $stringInput, string $encoding = null) |
60
|
|
|
* @method static bool offsetExists(string $stringInput, mixed $offset, string $encoding = null) |
61
|
|
|
* @method static string offsetGet(string $stringInput, mixed $offset, string $encoding = null) |
62
|
|
|
* @method static Stringy pad(string $stringInput, int $length, string $padStr = ' ', string $padType = 'right', string $encoding = null) |
63
|
|
|
* @method static Stringy padBoth(string $stringInput, int $length, string $padStr = ' ', string $encoding = null) |
64
|
|
|
* @method static Stringy padLeft(string $stringInput, int $length, string $padStr = ' ', string $encoding = null) |
65
|
|
|
* @method static Stringy padRight(string $stringInput, int $length, string $padStr = ' ', string $encoding = null) |
66
|
|
|
* @method static Stringy prepend(string $stringInput, string $string, string $encoding = null) |
67
|
|
|
* @method static Stringy regexReplace(string $stringInput, string $pattern, string $replacement, string $delimiter = '/') |
68
|
|
|
* @method static Stringy removeLeft(string $stringInput, string $substring, string $encoding = null) |
69
|
|
|
* @method static Stringy removeRight(string $stringInput, string $substring, string $encoding = null) |
70
|
|
|
* @method static Stringy removeHtml(string $stringInput, string $allowableTags = null, string $encoding = null) |
71
|
|
|
* @method static Stringy removeXss(string $stringInput, string $encoding = null) |
72
|
|
|
* @method static Stringy repeat(string $stringInput, int $multiplier, string $encoding = null) |
73
|
|
|
* @method static Stringy replace(string $stringInput, string $search, string $replacement, bool $caseSensitive, string $encoding = null) |
74
|
|
|
* @method static Stringy replaceAll(string $stringInput, array $search, string $replacement, bool $caseSensitive, string $encoding = null) |
75
|
|
|
* @method static Stringy reverse(string $stringInput, string $encoding = null) |
76
|
|
|
* @method static Stringy safeTruncate(string $stringInput, int $length, string $substring = '', string $encoding = null) |
77
|
|
|
* @method static Stringy shuffle(string $stringInput, string $encoding = null) |
78
|
|
|
* @method static Stringy shortenAfterWord(string $stringInput, int $length, string $strAddOn) |
79
|
|
|
* @method static Stringy slugify(string $stringInput, string $replacement = '-', string $language = 'de', boolean $strToLower = true) |
80
|
|
|
* @method static Stringy stripeCssMediaQueries(string $stringInput) |
81
|
|
|
* @method static Stringy stripeEmptyHtmlTags(string $stringInput) |
82
|
|
|
* @method static Stringy utf8ify(string $stringInput) |
83
|
|
|
* @method static Stringy snakeize(string $stringInput, string $encoding = null) |
84
|
|
|
* @method static bool startsWith(string $stringInput, string $substring, bool $caseSensitive = true, string $encoding = null) |
85
|
|
|
* @method static Stringy slice(string $stringInput, int $start, int $end = null, string $encoding = null) |
86
|
|
|
* @method static Stringy[] split(string $stringInput, string $pattern, int $limit = null, string $encoding = null) |
87
|
|
|
* @method static Stringy substr(string $stringInput, int $start, int $length = null, string $encoding = null) |
88
|
|
|
* @method static Stringy surround(string $stringInput, string $substring, string $encoding = null) |
89
|
|
|
* @method static Stringy swapCase(string $stringInput, string $encoding = null) |
90
|
|
|
* @method static Stringy tidy(string $stringInput, string $encoding = null) |
91
|
|
|
* @method static Stringy titleize(string $stringInput, string $encoding = null) |
92
|
|
|
* @method static Stringy toAscii(string $stringInput) |
93
|
|
|
* @method static Stringy toBoolean(string $stringInput, string $encoding = null) |
94
|
|
|
* @method static Stringy toString(string $stringInput) |
95
|
|
|
* @method static Stringy toLowerCase(string $stringInput, string $encoding = null) |
96
|
|
|
* @method static Stringy toSpaces(string $stringInput, int $tabLength = 4, string $encoding = null) |
97
|
|
|
* @method static Stringy toTabs(string $stringInput, int $tabLength = 4, string $encoding = null) |
98
|
|
|
* @method static Stringy toTitleCase(string $stringInput, string $encoding = null) |
99
|
|
|
* @method static Stringy toUpperCase(string $stringInput, string $encoding = null) |
100
|
|
|
* @method static Stringy trim(string $stringInput, string $chars = null, string $encoding = null) |
101
|
|
|
* @method static Stringy trimLeft(string $stringInput, string $chars = null, string $encoding = null) |
102
|
|
|
* @method static Stringy trimRight(string $stringInput, string $chars = null, string $encoding = null) |
103
|
|
|
* @method static Stringy truncate(string $stringInput, int $length, string $substring = '', string $encoding = null) |
104
|
|
|
* @method static Stringy underscored(string $stringInput, string $encoding = null) |
105
|
|
|
* @method static Stringy upperCamelize(string $stringInput, string $encoding = null) |
106
|
|
|
* @method static Stringy upperCaseFirst(string $stringInput, string $encoding = null) |
107
|
|
|
*/ |
108
|
|
|
class StaticStringy |
109
|
|
|
{ |
110
|
|
|
/** |
111
|
|
|
* A mapping of method names to the numbers of arguments it accepts. Each |
112
|
|
|
* should be two more than the equivalent Stringy method. Necessary as |
113
|
|
|
* static methods place the optional $encoding as the last parameter. |
114
|
|
|
* |
115
|
|
|
* @var array |
116
|
|
|
*/ |
117
|
|
|
protected static $methodArgs = null; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Creates an instance of Stringy and invokes the given method with the |
121
|
|
|
* rest of the passed arguments. The optional encoding is expected to be |
122
|
|
|
* the last argument. For example, the following: |
123
|
|
|
* StaticStringy::slice('fòôbàř', 0, 3, 'UTF-8'); translates to |
124
|
|
|
* Stringy::create('fòôbàř', 'UTF-8')->slice(0, 3); |
125
|
|
|
* The result is not cast, so the return value may be of type Stringy, |
126
|
|
|
* integer, boolean, etc. |
127
|
|
|
* |
128
|
|
|
* @param string $name |
129
|
|
|
* @param mixed[] $arguments |
130
|
|
|
* |
131
|
|
|
* @return Stringy |
132
|
|
|
*/ |
133
|
5 |
|
public static function __callStatic($name, array $arguments) |
134
|
|
|
{ |
135
|
5 |
|
if (!static::$methodArgs) { |
|
|
|
|
136
|
1 |
|
$stringyClass = new \ReflectionClass(Stringy::class); |
137
|
1 |
|
$methods = $stringyClass->getMethods(\ReflectionMethod::IS_PUBLIC); |
138
|
|
|
|
139
|
1 |
|
foreach ($methods as $method) { |
140
|
1 |
|
$params = $method->getNumberOfParameters() + 2; |
141
|
1 |
|
static::$methodArgs[$method->name] = $params; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
5 |
|
if (!isset(static::$methodArgs[$name])) { |
146
|
1 |
|
throw new \BadMethodCallException($name . ' is not a valid method'); |
147
|
|
|
} |
148
|
|
|
|
149
|
4 |
|
$numArgs = \count($arguments); |
150
|
4 |
|
$str = ($numArgs) ? $arguments[0] : ''; |
151
|
|
|
|
152
|
4 |
|
if ($numArgs === static::$methodArgs[$name]) { |
153
|
1 |
|
$args = \array_slice($arguments, 1, -1); |
154
|
1 |
|
$encoding = $arguments[$numArgs - 1]; |
155
|
|
|
} else { |
156
|
3 |
|
$args = \array_slice($arguments, 1); |
157
|
3 |
|
$encoding = null; |
158
|
|
|
} |
159
|
|
|
|
160
|
4 |
|
$stringy = Stringy::create($str, $encoding); |
161
|
|
|
|
162
|
4 |
|
return \call_user_func_array([$stringy, $name], $args); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string|string[]|Stringy|Stringy[]|null $input |
167
|
|
|
* |
168
|
|
|
* @return CollectionStringy |
169
|
|
|
*/ |
170
|
1 |
|
public static function collection($input = null): CollectionStringy |
171
|
|
|
{ |
172
|
|
|
// init |
173
|
1 |
|
$newCollection = new CollectionStringy(); |
174
|
|
|
|
175
|
1 |
|
if ($input === null) { |
176
|
1 |
|
return $newCollection; |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
if (!\is_array($input)) { |
180
|
1 |
|
$input = [$input]; |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
foreach ($input as &$stringOrStringy) { |
184
|
1 |
|
if (\is_string($stringOrStringy)) { |
185
|
1 |
|
$stringOrStringy = new Stringy($stringOrStringy); |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
$newCollection[] = $stringOrStringy; |
189
|
|
|
} |
190
|
|
|
|
191
|
1 |
|
return $newCollection; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.