Passed
Push — master ( b00b46...379d2a )
by Бабичев
01:38
created

Str::pos()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 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
     *
207
     * @return string
208
     */
209
    public static function first($string)
210
    {
211
        return static::sub($string, 0, 1);
212
    }
213
214
    /**
215
     * @param string $string
216
     *
217
     * @return string
218
     */
219
    public static function withoutFirst($string)
220
    {
221
        return static::sub($string, 1);
222
    }
223
224
    /**
225
     * @param string $string
226
     *
227
     * @return string
228
     */
229
    public static function last($string)
230
    {
231
        return static::sub($string, -1, 1);
232
    }
233
234
    /**
235
     * @param string $string
236
     *
237
     * @return string
238
     */
239
    public static function withoutLast($string)
240
    {
241
        return static::sub($string, 1, -1);
242
    }
243
244
    /**
245
     * @param string $string
246
     * @param int    $start
247
     * @param int    $length
248
     *
249
     * @return string
250
     */
251 3
    public static function sub($string, $start, $length = null)
252
    {
253 3
        return \mb_substr($string, $start, $length);
254
    }
255
256
    /**
257
     * @param string $string
258
     *
259
     * @return string
260
     */
261 3
    public static function upp($string)
262
    {
263 3
        return \mb_convert_case($string, MB_CASE_UPPER);
264
    }
265
266
    /**
267
     * @param string $string
268
     *
269
     * @return string
270
     */
271 4
    public static function low($string)
272
    {
273 4
        return \mb_convert_case($string, MB_CASE_LOWER);
274
    }
275
276
    /**
277
     * @param string $string
278
     *
279
     * @return string
280
     */
281 1
    public static function capitalize($string)
282
    {
283 1
        return \mb_convert_case($string, MB_CASE_TITLE);
284
    }
285
286
    /**
287
     * @param $string string
288
     * @param $char   string
289
     *
290
     * @return string
291
     */
292 1
    public static function snakeCase($string, $char = '_')
293
    {
294 1
        $data = \preg_replace('~([a-zа-яё])([A-ZА-ЯЁ])~u', '\\1' . $char . '\\2', $string);
295
296 1
        return static::low($data);
297
    }
298
299
    /**
300
     * @param array $input
301
     *
302
     * @return string
303
     */
304 2
    final protected static function _upperCase(array $input)
305
    {
306 2
        return static::upp(\end($input));
307
    }
308
309
    /**
310
     * @param string $string
311
     * @param string $char
312
     * @param bool   $lcFirst
313
     *
314
     * @return mixed
315
     */
316 2
    public static function camelCase($string, $char = '_', $lcFirst = false)
317
    {
318 2
        $data = \preg_replace_callback(
319 2
            '~' . $char . '([a-zа-яё])~u',
320 2
            [static::class, '_upperCase'],
321 2
            $string
322
        );
323
324 2
        if ($lcFirst)
325
        {
326 1
            return static::lcFirst($data);
327
        }
328
329 2
        return $data;
330
    }
331
332
    /**
333
     * @param string $string
334
     * @param bool   $translit
335
     * @param int    $case
336
     *
337
     * @return string
338
     */
339 1
    public static function friendlyUrl($string, $translit = true, $case = self::SNAKE_CASE)
340
    {
341 1
        $data = static::low($string);
342
343 1
        if ($translit)
344
        {
345 1
            $data = static::translit($data);
346
        }
347
348 1
        $data = \preg_replace('~[^a-zа-яё0-9]+~u', '-', $data);
349
350 1
        if ($case !== self::SNAKE_CASE)
351
        {
352 1
            $data = static::camelCase($data, '-');
353
        }
354
355 1
        $data = \preg_replace('~-{2,}~', '-', $data);
356
357 1
        return \trim($data, '-');
358
    }
359
360
    /**
361
     * @param string $string
362
     *
363
     * @return string mixed
364
     */
365 1
    public static function toNumber($string)
366
    {
367 1
        return \preg_replace('/\D/', '', $string);
368
    }
369
370
    /**
371
     * @param string $string
372
     * @param string $needle
373
     * @param int    $offset
374
     *
375
     * @return int
376
     */
377 1
    public static function pos($string, $needle, $offset = null)
378
    {
379 1
        return \mb_strpos($string, $needle, $offset);
380
    }
381
382
    /**
383
     * @param string $string
384
     * @param int    $multiplier
385
     *
386
     * @return string
387
     */
388 1
    public static function repeat($string, $multiplier)
389
    {
390 1
        return \str_repeat($string, $multiplier);
391
    }
392
393
    /**
394
     * @param string $string
395
     *
396
     * @return string
397
     */
398 1
    public static function shuffle($string)
399
    {
400 1
        return static::rand($string, static::len($string));
401
    }
402
403
    /**
404
     * @param string $string
405
     *
406
     * @return int
407
     */
408 2
    public static function len($string)
409
    {
410 2
        return \mb_strlen($string);
411
    }
412
413
    protected static $trans = [
414
        'á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A',
415
        'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A',
416
        'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C',
417
        'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C',
418
        'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh',
419
        'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E',
420
        'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E',
421
        'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G',
422
        'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H',
423
        'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I',
424
        'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I',
425
        'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L',
426
        'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N',
427
        'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O',
428
        'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O',
429
        'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE',
430
        'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R',
431
        'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S',
432
        'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't',
433
        'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u',
434
        'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u',
435
        'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u',
436
        'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w',
437
        'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y',
438
        'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z',
439
        'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'A',
440
        'б' => 'b', 'Б' => 'B', 'в' => 'v', 'В' => 'V', 'г' => 'g', 'Г' => 'G', 'д' => 'd', 'Д' => 'D',
441
        'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'Zh', 'з' => 'z', 'З' => 'Z',
442
        'и' => 'i', 'И' => 'I', 'й' => 'j', 'Й' => 'J', 'к' => 'k', 'К' => 'K', 'л' => 'l', 'Л' => 'L',
443
        'м' => 'm', 'М' => 'M', 'н' => 'n', 'Н' => 'N', 'о' => 'o', 'О' => 'O', 'п' => 'p', 'П' => 'P',
444
        'р' => 'r', 'Р' => 'R', 'с' => 's', 'С' => 'S', 'т' => 't', 'Т' => 'T', 'у' => 'u', 'У' => 'U',
445
        'ф' => 'f', 'Ф' => 'F', 'х' => 'h', 'Х' => 'H', 'ц' => 'c', 'Ц' => 'C', 'ч' => 'ch', 'Ч' => 'Ch',
446
        'ш' => 'sh', 'Ш' => 'Sh', 'щ' => 'sch', 'Щ' => 'Sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'Y',
447
        'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'E', 'ю' => 'ju', 'Ю' => 'Ju', 'я' => 'ja', 'Я' => 'Ja'
448
    ];
449
450
}
451