PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Support/Str.php

https://gitlab.com/kimting254/wbms
PHP | 392 lines | 165 code | 55 blank | 172 comment | 21 complexity | 78925ef3e932c2a4dccfebe2051f740f MD5 | raw file
  1. <?php namespace Illuminate\Support;
  2. use RuntimeException;
  3. use Stringy\StaticStringy;
  4. use Illuminate\Support\Traits\Macroable;
  5. class Str {
  6. use Macroable;
  7. /**
  8. * The cache of snake-cased words.
  9. *
  10. * @var array
  11. */
  12. protected static $snakeCache = [];
  13. /**
  14. * The cache of camel-cased words.
  15. *
  16. * @var array
  17. */
  18. protected static $camelCache = [];
  19. /**
  20. * The cache of studly-cased words.
  21. *
  22. * @var array
  23. */
  24. protected static $studlyCache = [];
  25. /**
  26. * Transliterate a UTF-8 value to ASCII.
  27. *
  28. * @param string $value
  29. * @return string
  30. */
  31. public static function ascii($value)
  32. {
  33. return StaticStringy::toAscii($value);
  34. }
  35. /**
  36. * Convert a value to camel case.
  37. *
  38. * @param string $value
  39. * @return string
  40. */
  41. public static function camel($value)
  42. {
  43. if (isset(static::$camelCache[$value]))
  44. {
  45. return static::$camelCache[$value];
  46. }
  47. return static::$camelCache[$value] = lcfirst(static::studly($value));
  48. }
  49. /**
  50. * Determine if a given string contains a given substring.
  51. *
  52. * @param string $haystack
  53. * @param string|array $needles
  54. * @return bool
  55. */
  56. public static function contains($haystack, $needles)
  57. {
  58. foreach ((array) $needles as $needle)
  59. {
  60. if ($needle != '' && strpos($haystack, $needle) !== false) return true;
  61. }
  62. return false;
  63. }
  64. /**
  65. * Determine if a given string ends with a given substring.
  66. *
  67. * @param string $haystack
  68. * @param string|array $needles
  69. * @return bool
  70. */
  71. public static function endsWith($haystack, $needles)
  72. {
  73. foreach ((array) $needles as $needle)
  74. {
  75. if ((string) $needle === substr($haystack, -strlen($needle))) return true;
  76. }
  77. return false;
  78. }
  79. /**
  80. * Cap a string with a single instance of a given value.
  81. *
  82. * @param string $value
  83. * @param string $cap
  84. * @return string
  85. */
  86. public static function finish($value, $cap)
  87. {
  88. $quoted = preg_quote($cap, '/');
  89. return preg_replace('/(?:'.$quoted.')+$/', '', $value).$cap;
  90. }
  91. /**
  92. * Determine if a given string matches a given pattern.
  93. *
  94. * @param string $pattern
  95. * @param string $value
  96. * @return bool
  97. */
  98. public static function is($pattern, $value)
  99. {
  100. if ($pattern == $value) return true;
  101. $pattern = preg_quote($pattern, '#');
  102. // Asterisks are translated into zero-or-more regular expression wildcards
  103. // to make it convenient to check if the strings starts with the given
  104. // pattern such as "library/*", making any string check convenient.
  105. $pattern = str_replace('\*', '.*', $pattern).'\z';
  106. return (bool) preg_match('#^'.$pattern.'#', $value);
  107. }
  108. /**
  109. * Return the length of the given string.
  110. *
  111. * @param string $value
  112. * @return int
  113. */
  114. public static function length($value)
  115. {
  116. return mb_strlen($value);
  117. }
  118. /**
  119. * Limit the number of characters in a string.
  120. *
  121. * @param string $value
  122. * @param int $limit
  123. * @param string $end
  124. * @return string
  125. */
  126. public static function limit($value, $limit = 100, $end = '...')
  127. {
  128. if (mb_strlen($value) <= $limit) return $value;
  129. return rtrim(mb_substr($value, 0, $limit, 'UTF-8')).$end;
  130. }
  131. /**
  132. * Convert the given string to lower-case.
  133. *
  134. * @param string $value
  135. * @return string
  136. */
  137. public static function lower($value)
  138. {
  139. return mb_strtolower($value);
  140. }
  141. /**
  142. * Limit the number of words in a string.
  143. *
  144. * @param string $value
  145. * @param int $words
  146. * @param string $end
  147. * @return string
  148. */
  149. public static function words($value, $words = 100, $end = '...')
  150. {
  151. preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
  152. if ( ! isset($matches[0]) || strlen($value) === strlen($matches[0])) return $value;
  153. return rtrim($matches[0]).$end;
  154. }
  155. /**
  156. * Parse a Class@method style callback into class and method.
  157. *
  158. * @param string $callback
  159. * @param string $default
  160. * @return array
  161. */
  162. public static function parseCallback($callback, $default)
  163. {
  164. return static::contains($callback, '@') ? explode('@', $callback, 2) : array($callback, $default);
  165. }
  166. /**
  167. * Get the plural form of an English word.
  168. *
  169. * @param string $value
  170. * @param int $count
  171. * @return string
  172. */
  173. public static function plural($value, $count = 2)
  174. {
  175. return Pluralizer::plural($value, $count);
  176. }
  177. /**
  178. * Generate a more truly "random" alpha-numeric string.
  179. *
  180. * @param int $length
  181. * @return string
  182. *
  183. * @throws \RuntimeException
  184. */
  185. public static function random($length = 16)
  186. {
  187. $string = '';
  188. while (($len = strlen($string)) < $length)
  189. {
  190. $size = $length - $len;
  191. $bytes = static::randomBytes($size);
  192. $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
  193. }
  194. return $string;
  195. }
  196. /**
  197. * Generate a more truly "random" bytes.
  198. *
  199. * @param int $length
  200. * @return string
  201. *
  202. * @throws \RuntimeException
  203. */
  204. public static function randomBytes($length = 16)
  205. {
  206. if (function_exists('random_bytes'))
  207. {
  208. $bytes = random_bytes($length);
  209. }
  210. elseif (function_exists('openssl_random_pseudo_bytes'))
  211. {
  212. $bytes = openssl_random_pseudo_bytes($length, $strong);
  213. if ($bytes === false || $strong === false)
  214. {
  215. throw new RuntimeException('Unable to generate random string.');
  216. }
  217. }
  218. else
  219. {
  220. throw new RuntimeException('OpenSSL extension is required for PHP 5 users.');
  221. }
  222. return $bytes;
  223. }
  224. /**
  225. * Generate a "random" alpha-numeric string.
  226. *
  227. * Should not be considered sufficient for cryptography, etc.
  228. *
  229. * @param int $length
  230. * @return string
  231. */
  232. public static function quickRandom($length = 16)
  233. {
  234. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  235. return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
  236. }
  237. /**
  238. * Convert the given string to upper-case.
  239. *
  240. * @param string $value
  241. * @return string
  242. */
  243. public static function upper($value)
  244. {
  245. return mb_strtoupper($value);
  246. }
  247. /**
  248. * Convert the given string to title case.
  249. *
  250. * @param string $value
  251. * @return string
  252. */
  253. public static function title($value)
  254. {
  255. return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
  256. }
  257. /**
  258. * Get the singular form of an English word.
  259. *
  260. * @param string $value
  261. * @return string
  262. */
  263. public static function singular($value)
  264. {
  265. return Pluralizer::singular($value);
  266. }
  267. /**
  268. * Generate a URL friendly "slug" from a given string.
  269. *
  270. * @param string $title
  271. * @param string $separator
  272. * @return string
  273. */
  274. public static function slug($title, $separator = '-')
  275. {
  276. $title = static::ascii($title);
  277. // Convert all dashes/underscores into separator
  278. $flip = $separator == '-' ? '_' : '-';
  279. $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
  280. // Remove all characters that are not the separator, letters, numbers, or whitespace.
  281. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));
  282. // Replace all separator characters and whitespace by a single separator
  283. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  284. return trim($title, $separator);
  285. }
  286. /**
  287. * Convert a string to snake case.
  288. *
  289. * @param string $value
  290. * @param string $delimiter
  291. * @return string
  292. */
  293. public static function snake($value, $delimiter = '_')
  294. {
  295. $key = $value.$delimiter;
  296. if (isset(static::$snakeCache[$key]))
  297. {
  298. return static::$snakeCache[$key];
  299. }
  300. if ( ! ctype_lower($value))
  301. {
  302. $value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.$delimiter, $value));
  303. }
  304. return static::$snakeCache[$key] = $value;
  305. }
  306. /**
  307. * Determine if a given string starts with a given substring.
  308. *
  309. * @param string $haystack
  310. * @param string|array $needles
  311. * @return bool
  312. */
  313. public static function startsWith($haystack, $needles)
  314. {
  315. foreach ((array) $needles as $needle)
  316. {
  317. if ($needle != '' && strpos($haystack, $needle) === 0) return true;
  318. }
  319. return false;
  320. }
  321. /**
  322. * Convert a value to studly caps case.
  323. *
  324. * @param string $value
  325. * @return string
  326. */
  327. public static function studly($value)
  328. {
  329. $key = $value;
  330. if (isset(static::$studlyCache[$key]))
  331. {
  332. return static::$studlyCache[$key];
  333. }
  334. $value = ucwords(str_replace(array('-', '_'), ' ', $value));
  335. return static::$studlyCache[$key] = str_replace(' ', '', $value);
  336. }
  337. }