Str.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the overtrue/wechat.
  9. *
  10. * (c) overtrue <i@overtrue.me>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace ByteDance\Kernel\Support;
  16. use ByteDance\Kernel\Exceptions\RuntimeException;
  17. /**
  18. * Class Str.
  19. */
  20. class Str
  21. {
  22. /**
  23. * The cache of snake-cased words.
  24. *
  25. * @var array
  26. */
  27. protected static $snakeCache = [];
  28. /**
  29. * The cache of camel-cased words.
  30. *
  31. * @var array
  32. */
  33. protected static $camelCache = [];
  34. /**
  35. * The cache of studly-cased words.
  36. *
  37. * @var array
  38. */
  39. protected static $studlyCache = [];
  40. /**
  41. * Convert a value to camel case.
  42. *
  43. * @param string $value
  44. *
  45. * @return string
  46. */
  47. public static function camel($value)
  48. {
  49. if (isset(static::$camelCache[$value])) {
  50. return static::$camelCache[$value];
  51. }
  52. return static::$camelCache[$value] = lcfirst(static::studly($value));
  53. }
  54. /**
  55. * Generate a more truly "random" alpha-numeric string.
  56. *
  57. * @param int $length
  58. *
  59. * @throws \RuntimeException
  60. *
  61. * @return string
  62. */
  63. public static function random($length = 16)
  64. {
  65. $string = '';
  66. while (($len = strlen($string)) < $length) {
  67. $size = $length - $len;
  68. $bytes = static::randomBytes($size);
  69. $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
  70. }
  71. return $string;
  72. }
  73. /**
  74. * Generate a more truly "random" bytes.
  75. *
  76. * @param int $length
  77. *
  78. * @throws RuntimeException
  79. * @throws \Exception
  80. *
  81. * @return string
  82. *
  83. *
  84. * @codeCoverageIgnore
  85. */
  86. public static function randomBytes($length = 16)
  87. {
  88. if (function_exists('random_bytes')) {
  89. $bytes = random_bytes($length);
  90. } elseif (function_exists('openssl_random_pseudo_bytes')) {
  91. $bytes = openssl_random_pseudo_bytes($length, $strong);
  92. if (false === $bytes || false === $strong) {
  93. throw new RuntimeException('Unable to generate random string.');
  94. }
  95. } else {
  96. throw new RuntimeException('OpenSSL extension is required for PHP 5 users.');
  97. }
  98. return $bytes;
  99. }
  100. /**
  101. * Generate a "random" alpha-numeric string.
  102. *
  103. * Should not be considered sufficient for cryptography, etc.
  104. *
  105. * @param int $length
  106. *
  107. * @return string
  108. */
  109. public static function quickRandom($length = 16)
  110. {
  111. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  112. return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
  113. }
  114. /**
  115. * Convert the given string to upper-case.
  116. *
  117. * @param string $value
  118. *
  119. * @return string
  120. */
  121. public static function upper($value)
  122. {
  123. return mb_strtoupper($value);
  124. }
  125. /**
  126. * Convert the given string to title case.
  127. *
  128. * @param string $value
  129. *
  130. * @return string
  131. */
  132. public static function title($value)
  133. {
  134. return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
  135. }
  136. /**
  137. * Convert a string to snake case.
  138. *
  139. * @param string $value
  140. * @param string $delimiter
  141. *
  142. * @return string
  143. */
  144. public static function snake($value, $delimiter = '_')
  145. {
  146. $key = $value.$delimiter;
  147. if (isset(static::$snakeCache[$key])) {
  148. return static::$snakeCache[$key];
  149. }
  150. if (!ctype_lower($value)) {
  151. $value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.$delimiter, $value));
  152. }
  153. return static::$snakeCache[$key] = trim($value, '_');
  154. }
  155. /**
  156. * Convert a value to studly caps case.
  157. *
  158. * @param string $value
  159. *
  160. * @return string
  161. */
  162. public static function studly($value)
  163. {
  164. $key = $value;
  165. if (isset(static::$studlyCache[$key])) {
  166. return static::$studlyCache[$key];
  167. }
  168. $value = ucwords(str_replace(['-', '_'], ' ', $value));
  169. return static::$studlyCache[$key] = str_replace(' ', '', $value);
  170. }
  171. }