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