Completed
Push — master ( c52910...66a65c )
by lyu
02:53 queued 47s
created

Str   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 193
ccs 0
cts 59
cp 0
rs 10
c 0
b 0
f 0
wmc 21
lcom 2
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A camel() 0 8 2
A random() 0 14 2
A randomBytes() 0 15 5
A quickRandom() 0 6 1
A upper() 0 4 1
A title() 0 4 1
A snake() 0 14 3
A studly() 0 12 2
A startsWith() 0 10 4
1
<?php
2
3
namespace Kaylyu\Alipay\Kernel\Support;
4
5
use Kaylyu\Alipay\Kernel\Exceptions\RuntimeException;
6
7
8
/**
9
 * Class Str.
10
 */
11
class Str
12
{
13
    /**
14
     * The cache of snake-cased words.
15
     *
16
     * @var array
17
     */
18
    protected static $snakeCache = [];
19
20
    /**
21
     * The cache of camel-cased words.
22
     *
23
     * @var array
24
     */
25
    protected static $camelCache = [];
26
27
    /**
28
     * The cache of studly-cased words.
29
     *
30
     * @var array
31
     */
32
    protected static $studlyCache = [];
33
34
    /**
35
     * Convert a value to camel case.
36
     *
37
     * @param string $value
38
     *
39
     * @return string
40
     */
41
    public static function camel($value)
42
    {
43
        if (isset(static::$camelCache[$value])) {
44
            return static::$camelCache[$value];
45
        }
46
47
        return static::$camelCache[$value] = lcfirst(static::studly($value));
48
    }
49
50
    /**
51
     * Generate a more truly "random" alpha-numeric string.
52
     *
53
     * @param int $length
54
     *
55
     * @return string
56
     *
57
     * @throws \RuntimeException
58
     */
59
    public static function random($length = 16)
60
    {
61
        $string = '';
62
63
        while (($len = strlen($string)) < $length) {
64
            $size = $length - $len;
65
66
            $bytes = static::randomBytes($size);
67
68
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
69
        }
70
71
        return $string;
72
    }
73
74
    /**
75
     * Generate a more truly "random" bytes.
76
     *
77
     * @param int $length
78
     *
79
     * @return string
80
     *
81
     * @throws RuntimeException
82
     *
83
     * @codeCoverageIgnore
84
     *
85
     * @throws \Exception
86
     */
87
    public static function randomBytes($length = 16)
88
    {
89
        if (function_exists('random_bytes')) {
90
            $bytes = random_bytes($length);
91
        } elseif (function_exists('openssl_random_pseudo_bytes')) {
92
            $bytes = openssl_random_pseudo_bytes($length, $strong);
93
            if (false === $bytes || false === $strong) {
94
                throw new RuntimeException('Unable to generate random string.');
95
            }
96
        } else {
97
            throw new RuntimeException('OpenSSL extension is required for PHP 5 users.');
98
        }
99
100
        return $bytes;
101
    }
102
103
    /**
104
     * Generate a "random" alpha-numeric string.
105
     *
106
     * Should not be considered sufficient for cryptography, etc.
107
     *
108
     * @param int $length
109
     *
110
     * @return string
111
     */
112
    public static function quickRandom($length = 16)
113
    {
114
        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
115
116
        return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
117
    }
118
119
    /**
120
     * Convert the given string to upper-case.
121
     *
122
     * @param string $value
123
     *
124
     * @return string
125
     */
126
    public static function upper($value)
127
    {
128
        return mb_strtoupper($value);
129
    }
130
131
    /**
132
     * Convert the given string to title case.
133
     *
134
     * @param string $value
135
     *
136
     * @return string
137
     */
138
    public static function title($value)
139
    {
140
        return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
141
    }
142
143
    /**
144
     * Convert a string to snake case.
145
     *
146
     * @param string $value
147
     * @param string $delimiter
148
     *
149
     * @return string
150
     */
151
    public static function snake($value, $delimiter = '_')
152
    {
153
        $key = $value.$delimiter;
154
155
        if (isset(static::$snakeCache[$key])) {
156
            return static::$snakeCache[$key];
157
        }
158
159
        if (!ctype_lower($value)) {
160
            $value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.$delimiter, $value));
161
        }
162
163
        return static::$snakeCache[$key] = trim($value, '_');
164
    }
165
166
    /**
167
     * Convert a value to studly caps case.
168
     *
169
     * @param string $value
170
     *
171
     * @return string
172
     */
173
    public static function studly($value)
174
    {
175
        $key = $value;
176
177
        if (isset(static::$studlyCache[$key])) {
178
            return static::$studlyCache[$key];
179
        }
180
181
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
182
183
        return static::$studlyCache[$key] = str_replace(' ', '', $value);
184
    }
185
186
    /**
187
     * Determine if a given string starts with a given substring.
188
     *
189
     * @param  string  $haystack
190
     * @param  string|array  $needles
191
     * @return bool
192
     */
193
    public static function startsWith($haystack, $needles)
194
    {
195
        foreach ((array) $needles as $needle) {
196
            if ($needle !== '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
197
                return true;
198
            }
199
        }
200
201
        return false;
202
    }
203
}
204