Passed
Branch 1.0 (db08ca)
by Vladimir
31:42
created

Str::lower()   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
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Helpers;
6
7
class Str
8
{
9
    /**
10
     * Determine if a given string ends with a given substring.
11
     *
12
     * @param string       $haystack
13
     * @param string|array $needles
14
     *
15
     * @return bool
16
     */
17 1
    public static function endsWith(string $haystack, $needles): bool
18
    {
19 1
        foreach ((array) $needles as $needle) {
20 1
            if (substr($haystack, -strlen($needle)) === (string) $needle) {
21 1
                return true;
22
            }
23
        }
24
25 1
        return false;
26
    }
27
28
    /**
29
     * Generate a more truly "random" alpha-numeric string.
30
     *
31
     * @param int $length
32
     *
33
     * @return string
34
     */
35 2
    public static function random(int $length = 16): string
36
    {
37 2
        $string = '';
38 2
        while (($len = strlen($string)) < $length) {
39 2
            $size = $length - $len;
40 2
            $bytes = random_bytes($size);
41 2
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
42
        }
43
44 2
        return $string;
45
    }
46
47
    /**
48
     * Convert the given string to lower-case.
49
     *
50
     * @param  string $value
51
     *
52
     * @return string
53
     */
54 3
    public static function lower($value)
55
    {
56 3
        return mb_strtolower($value, 'UTF-8');
57
    }
58
}
59