Str::randomBytes()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 9
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 14
ccs 0
cts 0
cp 0
crap 30
rs 9.6111
1
<?php
2
3
namespace EasyIM\Kernel\Support;
4
5
use EasyIM\Kernel\Exceptions\RuntimeException;
6
7
/**
8
 * Class Str.
9
 */
10
class Str
11
{
12
    /**
13
     * The cache of snake-cased words.
14
     *
15
     * @var array
16
     */
17
    protected static $snakeCache = [];
18
19
    /**
20
     * The cache of camel-cased words.
21
     *
22
     * @var array
23
     */
24
    protected static $camelCache = [];
25
26
    /**
27
     * The cache of studly-cased words.
28
     *
29
     * @var array
30
     */
31
    protected static $studlyCache = [];
32
33
    /**
34
     * Convert a value to camel case.
35
     *
36
     * @param string $value
37
     *
38
     * @return string
39
     */
40 1
    public static function camel($value)
41
    {
42 1
        if (isset(static::$camelCache[$value])) {
43 1
            return static::$camelCache[$value];
44
        }
45
46 1
        return static::$camelCache[$value] = lcfirst(static::studly($value));
47
    }
48
49
    /**
50
     * Generate a more truly "random" alpha-numeric string.
51
     *
52
     * @param int $length
53
     *
54
     * @return string
55
     *
56
     * @throws \Exception
57
     */
58 1
    public static function random($length = 16)
59
    {
60 1
        $string = '';
61
62 1
        while (($len = strlen($string)) < $length) {
63 1
            $size = $length - $len;
64
65 1
            $bytes = static::randomBytes($size);
66
67 1
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
68
        }
69
70 1
        return $string;
71
    }
72
73
    /**
74
     * Generate a more truly "random" bytes.
75
     *
76
     * @param int $length
77
     *
78
     * @return string
79
     *
80
     * @throws RuntimeException
81
     *
82
     * @codeCoverageIgnore
83
     *
84
     * @throws \Exception
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
99
        return $bytes;
100
    }
101
102
    /**
103
     * Generate a "random" alpha-numeric string.
104
     *
105
     * Should not be considered sufficient for cryptography, etc.
106
     *
107
     * @param int $length
108
     *
109
     * @return string
110
     */
111 1
    public static function quickRandom($length = 16)
112
    {
113 1
        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
114
115 1
        return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
116
    }
117
118
    /**
119
     * Convert the given string to upper-case.
120
     *
121
     * @param string $value
122
     *
123
     * @return string
124
     */
125 1
    public static function upper($value)
126
    {
127 1
        return mb_strtoupper($value);
128
    }
129
130
    /**
131
     * Convert the given string to title case.
132
     *
133
     * @param string $value
134
     *
135
     * @return string
136
     */
137 1
    public static function title($value)
138
    {
139 1
        return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
140
    }
141
142
    /**
143
     * Convert a string to snake case.
144
     *
145
     * @param string $value
146
     * @param string $delimiter
147
     *
148
     * @return string
149
     */
150 3
    public static function snake($value, $delimiter = '_')
151
    {
152 3
        $key = $value.$delimiter;
153
154 3
        if (isset(static::$snakeCache[$key])) {
155 1
            return static::$snakeCache[$key];
156
        }
157
158 3
        if (!ctype_lower($value)) {
159 2
            $value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.$delimiter, $value));
160
        }
161
162 3
        return static::$snakeCache[$key] = trim($value, '_');
163
    }
164
165
    /**
166
     * Convert a value to studly caps case.
167
     *
168
     * @param string $value
169
     *
170
     * @return string
171
     */
172 3
    public static function studly($value)
173
    {
174 3
        $key = $value;
175
176 3
        if (isset(static::$studlyCache[$key])) {
177 2
            return static::$studlyCache[$key];
178
        }
179
180 3
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
181
182 3
        return static::$studlyCache[$key] = str_replace(' ', '', $value);
183
    }
184
}
185