Str::studly()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Freyo\ApiGateway\Kernel\Support;
4
5
use Freyo\ApiGateway\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
    public static function camel($value)
41
    {
42
        if (isset(static::$camelCache[$value])) {
43
            return static::$camelCache[$value];
44
        }
45
46
        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 \RuntimeException
57
     */
58
    public static function random($length = 16)
59
    {
60
        $string = '';
61
62
        while (($len = strlen($string)) < $length) {
63
            $size = $length - $len;
64
65
            $bytes = static::randomBytes($size);
66
67
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
68
        }
69
70
        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
    public static function quickRandom($length = 16)
112
    {
113
        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
114
115
        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
    public static function upper($value)
126
    {
127
        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
    public static function title($value)
138
    {
139
        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
    public static function snake($value, $delimiter = '_')
151
    {
152
        $key = $value.$delimiter;
153
154
        if (isset(static::$snakeCache[$key])) {
155
            return static::$snakeCache[$key];
156
        }
157
158
        if (!ctype_lower($value)) {
159
            $value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.$delimiter, $value));
160
        }
161
162
        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
    public static function studly($value)
173
    {
174
        $key = $value;
175
176
        if (isset(static::$studlyCache[$key])) {
177
            return static::$studlyCache[$key];
178
        }
179
180
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
181
182
        return static::$studlyCache[$key] = str_replace(' ', '', $value);
183
    }
184
}
185