Str   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A random() 0 10 2
A upper() 0 3 1
A studly() 0 9 2
A camel() 0 7 2
A quickRandom() 0 5 1
A title() 0 3 1
A snake() 0 11 3
A randomBytes() 0 14 5
1
<?php
2
3
/*
4
 * This file is part of the strays/baidu-ai.
5
 *
6
 * (c) strays <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Strays\BaiDuAi\Kernel\Support;
13
14
class Str
15
{
16
    protected static $snakeCache = [];
17
18
    protected static $camelCache = [];
19
20
    protected static $studlyCache = [];
21
22
    public static function camel($value)
23
    {
24
        if (isset(static::$camelCache[$value])) {
25
            return static::$camelCache[$value];
26
        }
27
28
        return static::$camelCache[$value] = lcfirst(static::studly($value));
29
    }
30
31
    public static function random($length = 16)
32
    {
33
        $string = '';
34
        while (($len = strlen($string)) < $length) {
35
            $size = $length - $len;
36
            $bytes = static::randomBytes($size);
37
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
38
        }
39
40
        return $string;
41
    }
42
43
    public static function randomBytes($length = 16)
44
    {
45
        if (function_exists('random_bytes')) {
46
            $bytes = random_bytes($length);
47
        } elseif (function_exists('openssl_random_pseudo_bytes')) {
48
            $bytes = openssl_random_pseudo_bytes($length, $strong);
49
            if (false === $bytes || false === $strong) {
50
                throw new RuntimeException('Unable to generate random string.');
0 ignored issues
show
Bug introduced by
The type Strays\BaiDuAi\Kernel\Support\RuntimeException was not found. Did you mean RuntimeException? If so, make sure to prefix the type with \.
Loading history...
51
            }
52
        } else {
53
            throw new RuntimeException('OpenSSL extension is required for PHP 5 users.');
54
        }
55
56
        return $bytes;
57
    }
58
59
    public static function quickRandom($length = 16)
60
    {
61
        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
62
63
        return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
64
    }
65
66
    public static function upper($value)
67
    {
68
        return mb_strtoupper($value);
69
    }
70
71
    public static function title($value)
72
    {
73
        return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
74
    }
75
76
    public static function snake($value, $delimiter = '_')
77
    {
78
        $key = $value.$delimiter;
79
        if (isset(static::$snakeCache[$key])) {
80
            return static::$snakeCache[$key];
81
        }
82
        if (!ctype_lower($value)) {
83
            $value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.$delimiter, $value));
84
        }
85
86
        return static::$snakeCache[$key] = trim($value, '_');
87
    }
88
89
    public static function studly($value)
90
    {
91
        $key = $value;
92
        if (isset(static::$studlyCache[$key])) {
93
            return static::$studlyCache[$key];
94
        }
95
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
96
97
        return static::$studlyCache[$key] = str_replace(' ', '', $value);
98
    }
99
}
100