123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- /*
- * This file is part of the overtrue/wechat.
- *
- * (c) overtrue <i@overtrue.me>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace App\Libs\TikTok\Kernel\Support;
- use App\Libs\TikTok\Kernel\Exceptions\RuntimeException;
- /**
- * Class Str.
- */
- class Str {
- /**
- * The cache of snake-cased words.
- *
- * @var array
- */
- protected static $snakeCache = [];
- /**
- * The cache of camel-cased words.
- *
- * @var array
- */
- protected static $camelCache = [];
- /**
- * The cache of studly-cased words.
- *
- * @var array
- */
- protected static $studlyCache = [];
- /**
- * Convert a value to camel case.
- *
- * @param string $value
- *
- * @return string
- */
- public static function camel(string $value): string {
- return static::$camelCache[$value] ?? (static::$camelCache[$value] = lcfirst(static::studly($value)));
- }
- /**
- * Generate a more truly "random" alpha-numeric string.
- *
- * @param int $length
- *
- * @return string
- *
- * @throws RuntimeException
- */
- public static function random(int $length = 16): string {
- $string = '';
- while (($len = strlen($string)) < $length) {
- $size = $length - $len;
- $bytes = static::randomBytes($size);
- $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
- }
- return $string;
- }
- /**
- * Generate a more truly "random" bytes.
- *
- * @param int $length
- *
- * @return string
- *
- * @throws RuntimeException
- *
- * @codeCoverageIgnore
- *
- * @throws \Exception
- */
- public static function randomBytes(int $length = 16): string {
- if (function_exists('random_bytes')) {
- $bytes = random_bytes($length);
- } elseif (function_exists('openssl_random_pseudo_bytes')) {
- $bytes = openssl_random_pseudo_bytes($length, $strong);
- if (false === $bytes || false === $strong) {
- throw new RuntimeException('Unable to generate random string.');
- }
- } else {
- throw new RuntimeException('OpenSSL extension is required for PHP 5 users.');
- }
- return $bytes;
- }
- /**
- * Generate a "random" alpha-numeric string.
- *
- * Should not be considered sufficient for cryptography, etc.
- *
- * @param int $length
- *
- * @return string
- */
- public static function quickRandom(int $length = 16): string {
- $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
- }
- /**
- * Convert the given string to upper-case.
- *
- * @param string $value
- *
- * @return string
- */
- public static function upper(string $value): string {
- return mb_strtoupper($value);
- }
- /**
- * Convert the given string to title case.
- *
- * @param string $value
- *
- * @return string
- */
- public static function title(string $value): string {
- return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
- }
- /**
- * Convert a string to snake case.
- *
- * @param string $value
- * @param string $delimiter
- *
- * @return string
- */
- public static function snake(string $value, string $delimiter = '_'): string {
- $key = $value . $delimiter;
- if (isset(static::$snakeCache[$key])) {
- return static::$snakeCache[$key];
- }
- if (!ctype_lower($value)) {
- $value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1' . $delimiter, $value));
- }
- return static::$snakeCache[$key] = trim($value, '_');
- }
- /**
- * Convert a value to studly caps case.
- *
- * @param string $value
- *
- * @return string
- */
- public static function studly(string $value): string {
- $key = $value;
- if (isset(static::$studlyCache[$key])) {
- return static::$studlyCache[$key];
- }
- $value = ucwords(str_replace(['-', '_'], ' ', $value));
- return static::$studlyCache[$key] = str_replace(' ', '', $value);
- }
- }
|