Passed
Push — master ( c7f67f...b7b336 )
by Бабичев
01:53
created

Str   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 404
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 34
lcom 2
cbo 3
dl 0
loc 404
ccs 93
cts 93
cp 1
rs 9.2
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A split() 0 4 1
A shorten() 0 16 2
A ucFirst() 0 7 1
A lcFirst() 0 7 1
A random() 0 21 4
A rand() 0 14 2
A uniqid() 0 4 1
B fileSize() 0 24 5
A translit() 0 6 1
A sub() 0 4 1
A upp() 0 4 1
A low() 0 4 1
A capitalize() 0 4 1
A snakeCase() 0 6 1
A _upperCase() 0 4 1
A camelCase() 0 15 2
A friendlyUrl() 0 20 3
A toNumber() 0 4 1
A pos() 0 4 1
A repeat() 0 4 1
A shuffle() 0 4 1
A len() 0 4 1
1
<?php
2
3
namespace Bavix\Helpers;
4
5
use Bavix\Exceptions;
6
7
class Str
8
{
9
10
    const SNAKE_CASE = 1;
11
    const CAMEL_CASE = 2;
12
13
    const DIGITS        = '0123456789';
14
    const ALPHABET_LOW  = 'abcdefghijklmnopqrstuvwxyz';
15
    const ALPHABET_HIGH = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
16
    const ALPHABET      = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
17
18
    const RAND_ALPHA_LOW  = 1;
19
    const RAND_ALPHA_HIGH = 2;
20
    const RAND_ALPHA      = 4;
21
    const RAND_DIGITS     = 8;
22
    const RAND_ALL        = 16;
23
24
    /**
25
     * @var array
26
     */
27
    protected static $dictionary = [
28
        4 => self::ALPHABET . self::DIGITS,
29
        3 => self::DIGITS,
30
        2 => self::ALPHABET,
31
        1 => self::ALPHABET_HIGH,
32
        0 => self::ALPHABET_LOW,
33
    ];
34
35
    /**
36
     * @param string $str
37
     * @param int    $length
38
     *
39
     * @return array
40
     */
41 1
    public static function split($str, $length = 1)
42
    {
43 1
        return \str_split($str, $length);
44
    }
45
46
    /**
47
     * Shortens text to length and keeps integrity of words
48
     *
49
     * @param  string  $str
50
     * @param  integer $length
51
     * @param  string  $end
52
     *
53
     * @return string
54
     */
55 1
    public static function shorten($str, $length = 100, $end = '&#8230;')
56
    {
57 1
        $data = $str;
58
59 1
        if (strlen($data) > $length)
60
        {
61 1
            $data = trim($data);
62 1
            $data = \substr($data, 0, $length);
63 1
            $rev  = \strrev($data);
64 1
            $pos  = \strpos($rev, ' ');
65 1
            $data = \substr($data, 0, -$pos);
66 1
            $data .= trim($end);
67
        }
68
69 1
        return $data;
70
    }
71
72
    /**
73
     * @param string $string
74
     *
75
     * @return string
76
     */
77 1
    public static function ucFirst($string)
78
    {
79 1
        $ucFirst = static::sub($string, 0, 1);
80 1
        $ucFirst = static::upp($ucFirst);
81
82 1
        return $ucFirst . static::sub($string, 1);
83
    }
84
85
    /**
86
     * @param string $string
87
     *
88
     * @return string
89
     */
90 2
    public static function lcFirst($string)
91
    {
92 2
        $lcFirst = static::sub($string, 0, 1);
93 2
        $lcFirst = static::low($lcFirst);
94
95 2
        return $lcFirst . static::sub($string, 1);
96
    }
97
98
    /**
99
     * Return random string
100
     *
101
     * @param  int $length
102
     * @param  int $type
103
     *
104
     * @return string
105
     *
106
     * @throws \InvalidArgumentException
107
     */
108 3
    public static function random($length = 32, $type = self::RAND_ALL)
109
    {
110 3
        $string = '';
111
        // todo: make to helper?
112 3
        foreach (static::$dictionary as $pos => $item)
113
        {
114 3
            $key = (1 << $pos);
115 3
            if ($type >= $key)
116
            {
117 2
                $string .= $item;
118 2
                $type   -= $key;
119
            }
120
        }
121
122 3
        if (empty($string))
123
        {
124 1
            throw new Exceptions\Blank("Invalid random string type [{$type}].");
125
        }
126
127 2
        return static::rand($string, $length);
128
    }
129
130
    /**
131
     * @param string $chars
132
     * @param int    $length
133
     *
134
     * @return string
135
     */
136 2
    protected static function rand($chars, $length)
137
    {
138 2
        $string = '';
139 2
        $max    = static::len($chars) - 1;
140 2
        $i      = 0;
141
142 2
        while ($i < $length)
143
        {
144 2
            $string .= $chars[Num::randomInt(0, $max)];
145 2
            $i++;
146
        }
147
148 2
        return $string;
149
    }
150
151
    /**
152
     * @return string
153
     */
154 1
    public static function uniqid()
155
    {
156 1
        return \uniqid(Num::randomInt(), true);
157
    }
158
159
    /**
160
     * @param int $size
161
     * @param int $decimals
162
     *
163
     * @return string
164
     */
165 2
    public static function fileSize($size, $decimals = 2)
166
    {
167
        $units = [
168 2
            'B', 'KB', 'MB',
169
            'GB', 'TB', 'PB',
170
            'EB', 'ZB', 'YB'
171
        ];
172
173 2
        if ($size < 0)
174
        {
175 1
            throw new Exceptions\Invalid('Undefined size!');
176
        }
177
178 1
        $log     = \log($size, 1024);
179 1
        $power   = \min(\floor($log), \count($units));
180 1
        $postfix = $units[$power > 0 ? $power : 0];
181
182 1
        while ($power > 0 && $power--)
183
        {
184 1
            $size /= 1024;
185
        }
186
187 1
        return Num::format($size, $decimals) . ' ' . $postfix;
188
    }
189
190
    /**
191
     * transliteration cyr->lat
192
     *
193
     * @param $string
194
     *
195
     * @return string
196
     */
197 2
    public static function translit($string)
198
    {
199 2
        $string = \strtr($string, static::$trans);
200
201 2
        return \iconv(\mb_internal_encoding(), 'ASCII//TRANSLIT', $string);
202
    }
203
204
    /**
205
     * @param string $string
206
     * @param int    $start
207
     * @param int    $length
208
     *
209
     * @return string
210
     */
211 3
    public static function sub($string, $start, $length = null)
212
    {
213 3
        return \mb_substr($string, $start, $length);
214
    }
215
216
    /**
217
     * @param string $string
218
     *
219
     * @return string
220
     */
221 3
    public static function upp($string)
222
    {
223 3
        return \mb_convert_case($string, MB_CASE_UPPER);
224
    }
225
226
    /**
227
     * @param string $string
228
     *
229
     * @return string
230
     */
231 4
    public static function low($string)
232
    {
233 4
        return \mb_convert_case($string, MB_CASE_LOWER);
234
    }
235
236
    /**
237
     * @param string $string
238
     *
239
     * @return string
240
     */
241 1
    public static function capitalize($string)
242
    {
243 1
        return \mb_convert_case($string, MB_CASE_TITLE);
244
    }
245
246
    /**
247
     * @param $string string
248
     * @param $char   string
249
     *
250
     * @return string
251
     */
252 1
    public static function snakeCase($string, $char = '_')
253
    {
254 1
        $data = \preg_replace('~([a-zа-яё])([A-ZА-ЯЁ])~u', '\\1' . $char . '\\2', $string);
255
256 1
        return static::low($data);
257
    }
258
259
    /**
260
     * @param array $input
261
     *
262
     * @return string
263
     */
264 2
    final protected static function _upperCase(array $input)
265
    {
266 2
        return static::upp(\end($input));
267
    }
268
269
    /**
270
     * @param string $string
271
     * @param string $char
272
     * @param bool   $lcFirst
273
     *
274
     * @return mixed
275
     */
276 2
    public static function camelCase($string, $char = '_', $lcFirst = false)
277
    {
278 2
        $data = \preg_replace_callback(
279 2
            '~' . $char . '([a-zа-яё])~u',
280 2
            [static::class, '_upperCase'],
281 2
            $string
282
        );
283
284 2
        if ($lcFirst)
285
        {
286 1
            return static::lcFirst($data);
287
        }
288
289 2
        return $data;
290
    }
291
292
    /**
293
     * @param string $string
294
     * @param bool   $translit
295
     * @param int    $case
296
     *
297
     * @return string
298
     */
299 1
    public static function friendlyUrl($string, $translit = true, $case = self::SNAKE_CASE)
300
    {
301 1
        $data = static::low($string);
302
303 1
        if ($translit)
304
        {
305 1
            $data = static::translit($data);
306
        }
307
308 1
        $data = \preg_replace('~[^a-zа-яё0-9]+~u', '-', $data);
309
310 1
        if ($case !== self::SNAKE_CASE)
311
        {
312 1
            $data = static::camelCase($data, '-');
313
        }
314
315 1
        $data = \preg_replace('~-{2,}~', '-', $data);
316
317 1
        return \trim($data, '-');
318
    }
319
320
    /**
321
     * @param string $string
322
     *
323
     * @return string mixed
324
     */
325 1
    public static function toNumber($string)
326
    {
327 1
        return \preg_replace('/\D/', '', $string);
328
    }
329
330
    /**
331
     * @param string $string
332
     * @param string $needle
333
     * @param int    $offset
334
     *
335
     * @return int
336
     */
337 1
    public static function pos($string, $needle, $offset = null)
338
    {
339 1
        return \mb_strpos($string, $needle, $offset);
340
    }
341
342
    /**
343
     * @param string $string
344
     * @param int    $multiplier
345
     *
346
     * @return string
347
     */
348 1
    public static function repeat($string, $multiplier)
349
    {
350 1
        return \str_repeat($string, $multiplier);
351
    }
352
353
    /**
354
     * @param string $string
355
     *
356
     * @return string
357
     */
358 1
    public static function shuffle($string)
359
    {
360 1
        return static::rand($string, static::len($string));
361
    }
362
363
    /**
364
     * @param string $string
365
     *
366
     * @return int
367
     */
368 2
    public static function len($string)
369
    {
370 2
        return \mb_strlen($string);
371
    }
372
373
    protected static $trans = [
374
        'á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A',
375
        'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A',
376
        'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C',
377
        'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C',
378
        'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh',
379
        'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E',
380
        'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E',
381
        'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G',
382
        'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H',
383
        'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I',
384
        'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I',
385
        'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L',
386
        'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N',
387
        'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O',
388
        'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O',
389
        'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE',
390
        'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R',
391
        'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S',
392
        'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't',
393
        'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u',
394
        'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u',
395
        'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u',
396
        'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w',
397
        'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y',
398
        'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z',
399
        'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'A',
400
        'б' => 'b', 'Б' => 'B', 'в' => 'v', 'В' => 'V', 'г' => 'g', 'Г' => 'G', 'д' => 'd', 'Д' => 'D',
401
        'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'Zh', 'з' => 'z', 'З' => 'Z',
402
        'и' => 'i', 'И' => 'I', 'й' => 'j', 'Й' => 'J', 'к' => 'k', 'К' => 'K', 'л' => 'l', 'Л' => 'L',
403
        'м' => 'm', 'М' => 'M', 'н' => 'n', 'Н' => 'N', 'о' => 'o', 'О' => 'O', 'п' => 'p', 'П' => 'P',
404
        'р' => 'r', 'Р' => 'R', 'с' => 's', 'С' => 'S', 'т' => 't', 'Т' => 'T', 'у' => 'u', 'У' => 'U',
405
        'ф' => 'f', 'Ф' => 'F', 'х' => 'h', 'Х' => 'H', 'ц' => 'c', 'Ц' => 'C', 'ч' => 'ch', 'Ч' => 'Ch',
406
        'ш' => 'sh', 'Ш' => 'Sh', 'щ' => 'sch', 'Щ' => 'Sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'Y',
407
        'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'E', 'ю' => 'ju', 'Ю' => 'Ju', 'я' => 'ja', 'Я' => 'Ja'
408
    ];
409
410
}
411