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