1: <?php namespace Illuminate\Support;
2:
3: class Str {
4:
5: 6: 7: 8: 9:
10: protected static $macros = array();
11:
12: 13: 14: 15: 16: 17:
18: public static function ascii($value)
19: {
20: return \Patchwork\Utf8::toAscii($value);
21: }
22:
23: 24: 25: 26: 27: 28:
29: public static function camel($value)
30: {
31: return lcfirst(static::studly($value));
32: }
33:
34: 35: 36: 37: 38: 39: 40:
41: public static function contains($haystack, $needle)
42: {
43: foreach ((array) $needle as $n)
44: {
45: if (strpos($haystack, $n) !== false) return true;
46: }
47:
48: return false;
49: }
50:
51: 52: 53: 54: 55: 56: 57:
58: public static function endsWith($haystack, $needles)
59: {
60: foreach ((array) $needles as $needle)
61: {
62: if ($needle == substr($haystack, strlen($haystack) - strlen($needle))) return true;
63: }
64:
65: return false;
66: }
67:
68: 69: 70: 71: 72: 73: 74:
75: public static function finish($value, $cap)
76: {
77: return rtrim($value, $cap).$cap;
78: }
79:
80: 81: 82: 83: 84: 85: 86:
87: public static function is($pattern, $value)
88: {
89: if ($pattern == $value) return true;
90:
91: $pattern = preg_quote($pattern, '#');
92:
93:
94:
95:
96: if ($pattern !== '/')
97: {
98: $pattern = str_replace('\*', '.*', $pattern).'\z';
99: }
100: else
101: {
102: $pattern = '/$';
103: }
104:
105: return (bool) preg_match('#^'.$pattern.'#', $value);
106: }
107:
108: 109: 110: 111: 112: 113:
114: public static function length($value)
115: {
116: return mb_strlen($value);
117: }
118:
119: 120: 121: 122: 123: 124: 125: 126:
127: public static function limit($value, $limit = 100, $end = '...')
128: {
129: if (mb_strlen($value) <= $limit) return $value;
130:
131: return mb_substr($value, 0, $limit, 'UTF-8').$end;
132: }
133:
134: 135: 136: 137: 138: 139:
140: public static function lower($value)
141: {
142: return mb_strtolower($value);
143: }
144:
145: 146: 147: 148: 149: 150: 151: 152:
153: public static function words($value, $words = 100, $end = '...')
154: {
155: preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
156:
157: if ( ! isset($matches[0])) return $value;
158:
159: if (strlen($value) == strlen($matches[0])) return $value;
160:
161: return rtrim($matches[0]).$end;
162: }
163:
164: 165: 166: 167: 168: 169: 170:
171: public static function parseCallback($callback, $default)
172: {
173: return static::contains($callback, '@') ? explode('@', $callback, 2) : array($callback, $default);
174: }
175:
176: 177: 178: 179: 180: 181: 182:
183: public static function plural($value, $count = 2)
184: {
185: return Pluralizer::plural($value, $count);
186: }
187:
188: 189: 190: 191: 192: 193:
194: public static function random($length = 16)
195: {
196: if (function_exists('openssl_random_pseudo_bytes'))
197: {
198: $bytes = openssl_random_pseudo_bytes($length * 2);
199:
200: if ($bytes === false)
201: {
202: throw new \RuntimeException('Unable to generate random string.');
203: }
204:
205: return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $length);
206: }
207:
208: return static::quickRandom($length);
209: }
210:
211: 212: 213: 214: 215: 216: 217: 218:
219: public static function quickRandom($length = 16)
220: {
221: $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
222:
223: return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
224: }
225:
226: 227: 228: 229: 230: 231:
232: public static function upper($value)
233: {
234: return mb_strtoupper($value);
235: }
236:
237: 238: 239: 240: 241: 242:
243: public static function singular($value)
244: {
245: return Pluralizer::singular($value);
246: }
247:
248: 249: 250: 251: 252: 253: 254:
255: public static function slug($title, $separator = '-')
256: {
257: $title = static::ascii($title);
258:
259:
260: $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));
261:
262:
263: $flip = $separator == '-' ? '_' : '-';
264:
265: $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
266:
267:
268: $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
269:
270: return trim($title, $separator);
271: }
272:
273: 274: 275: 276: 277: 278: 279:
280: public static function snake($value, $delimiter = '_')
281: {
282: $replace = '$1'.$delimiter.'$2';
283:
284: return ctype_lower($value) ? $value : strtolower(preg_replace('/(.)([A-Z])/', $replace, $value));
285: }
286:
287: 288: 289: 290: 291: 292: 293:
294: public static function startsWith($haystack, $needles)
295: {
296: foreach ((array) $needles as $needle)
297: {
298: if (strpos($haystack, $needle) === 0) return true;
299: }
300:
301: return false;
302: }
303:
304: 305: 306: 307: 308: 309:
310: public static function studly($value)
311: {
312: $value = ucwords(str_replace(array('-', '_'), ' ', $value));
313:
314: return str_replace(' ', '', $value);
315: }
316:
317: 318: 319: 320: 321: 322: 323:
324: public static function macro($name, $macro)
325: {
326: static::$macros[$name] = $macro;
327: }
328:
329: 330: 331: 332: 333: 334: 335:
336: public static function __callStatic($method, $parameters)
337: {
338: if (isset(static::$macros[$method]))
339: {
340: return call_user_func_array(static::$macros[$method], $parameters);
341: }
342:
343: throw new \BadMethodCallException("Method {$method} does not exist.");
344: }
345:
346: }
347: