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