1 | <?php |
||
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) |
||
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) |
||
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 = '_') |
||
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) |
||
195 | } |
||
196 |