Str.php 4.2 KB

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