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