PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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