1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EntWeChat\Support; |
4
|
|
|
|
5
|
|
|
use EntWeChat\Core\Exceptions\RuntimeException; |
6
|
|
|
|
7
|
|
|
class Str |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The cache of snake-cased words. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected static $snakeCache = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The cache of camel-cased words. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected static $camelCache = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The cache of studly-cased words. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected static $studlyCache = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Convert a value to camel case. |
32
|
|
|
* |
33
|
|
|
* @param string $value |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public static function camel($value) |
38
|
|
|
{ |
39
|
|
|
if (isset(static::$camelCache[$value])) { |
40
|
|
|
return static::$camelCache[$value]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return static::$camelCache[$value] = lcfirst(static::studly($value)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Generate a more truly "random" alpha-numeric string. |
48
|
|
|
* |
49
|
|
|
* @param int $length |
50
|
|
|
* |
51
|
|
|
* @throws \RuntimeException |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public static function random($length = 16) |
56
|
|
|
{ |
57
|
|
|
$string = ''; |
58
|
|
|
|
59
|
|
|
while (($len = strlen($string)) < $length) { |
60
|
|
|
$size = $length - $len; |
61
|
|
|
|
62
|
|
|
$bytes = static::randomBytes($size); |
63
|
|
|
|
64
|
|
|
$string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $string; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Generate a more truly "random" bytes. |
72
|
|
|
* |
73
|
|
|
* @param int $length |
74
|
|
|
* |
75
|
|
|
* @throws RuntimeException |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public static function randomBytes($length = 16) |
80
|
|
|
{ |
81
|
|
|
if (function_exists('random_bytes')) { |
82
|
|
|
$bytes = random_bytes($length); |
83
|
|
|
} elseif (function_exists('openssl_random_pseudo_bytes')) { |
84
|
|
|
$bytes = openssl_random_pseudo_bytes($length, $strong); |
85
|
|
|
if ($bytes === false || $strong === false) { |
86
|
|
|
throw new RuntimeException('Unable to generate random string.'); |
87
|
|
|
} |
88
|
|
|
} else { |
89
|
|
|
throw new RuntimeException('OpenSSL extension is required for PHP 5 users.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $bytes; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Generate a "random" alpha-numeric string. |
97
|
|
|
* |
98
|
|
|
* Should not be considered sufficient for cryptography, etc. |
99
|
|
|
* |
100
|
|
|
* @param int $length |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public static function quickRandom($length = 16) |
105
|
|
|
{ |
106
|
|
|
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
107
|
|
|
|
108
|
|
|
return substr(str_shuffle(str_repeat($pool, $length)), 0, $length); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Convert the given string to upper-case. |
113
|
|
|
* |
114
|
|
|
* @param string $value |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public static function upper($value) |
119
|
|
|
{ |
120
|
|
|
return mb_strtoupper($value); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Convert the given string to title case. |
125
|
|
|
* |
126
|
|
|
* @param string $value |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public static function title($value) |
131
|
|
|
{ |
132
|
|
|
return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Convert a string to snake case. |
137
|
|
|
* |
138
|
|
|
* @param string $value |
139
|
|
|
* @param string $delimiter |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public static function snake($value, $delimiter = '_') |
144
|
|
|
{ |
145
|
|
|
$key = $value.$delimiter; |
146
|
|
|
|
147
|
|
|
if (isset(static::$snakeCache[$key])) { |
148
|
|
|
return static::$snakeCache[$key]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (!ctype_lower($value)) { |
152
|
|
|
$value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.$delimiter, $value)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return static::$snakeCache[$key] = $value; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Convert a value to studly caps case. |
160
|
|
|
* |
161
|
|
|
* @param string $value |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public static function studly($value) |
166
|
|
|
{ |
167
|
|
|
$key = $value; |
168
|
|
|
|
169
|
|
|
if (isset(static::$studlyCache[$key])) { |
170
|
|
|
return static::$studlyCache[$key]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$value = ucwords(str_replace(['-', '_'], ' ', $value)); |
174
|
|
|
|
175
|
|
|
return static::$studlyCache[$key] = str_replace(' ', '', $value); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|