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