Passed
Branch 1.0 (f7255b)
by Vladimir
06:00
created

Str::contains()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 3
nop 2
crap 4
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 contains a given substring.
11
     *
12
     * @param  string       $haystack
13
     * @param  string|array $needles
14
     *
15
     * @return bool
16
     */
17 3
    public static function contains(string $haystack, $needles)
18
    {
19 3
        foreach ((array) $needles as $needle) {
20 3
            if ($needle !== '' && mb_strpos($haystack, $needle) !== false) {
21 3
                return true;
22
            }
23
        }
24
25 2
        return false;
26
    }
27
28
    /**
29
     * Determine if a given string ends with a given substring.
30
     *
31
     * @param string       $haystack
32
     * @param string|array $needles
33
     *
34
     * @return bool
35
     */
36 1
    public static function endsWith(string $haystack, $needles): bool
37
    {
38 1
        foreach ((array) $needles as $needle) {
39 1
            if (substr($haystack, -strlen($needle)) === (string) $needle) {
40 1
                return true;
41
            }
42
        }
43
44 1
        return false;
45
    }
46
47
    /**
48
     * Generate a more truly "random" alpha-numeric string.
49
     *
50
     * @param int $length
51
     *
52
     * @return string
53
     */
54 2
    public static function random(int $length = 16): string
55
    {
56 2
        $string = '';
57 2
        while (($len = strlen($string)) < $length) {
58 2
            $size = $length - $len;
59 2
            $bytes = random_bytes($size);
60 2
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
61
        }
62
63 2
        return $string;
64
    }
65
66
    /**
67
     * Convert the given string to lower-case.
68
     *
69
     * @param  string $value
70
     *
71
     * @return string
72
     */
73 3
    public static function lower($value)
74
    {
75 3
        return mb_strtolower($value, 'UTF-8');
76
    }
77
}
78