StringUtils::endsWith()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
4
namespace Dontdrinkandroot\Utils;
5
6
/**
7
 * @author Philip Washington Sorst <[email protected]>
8
 */
9
class StringUtils
10
{
11
    /**
12
     * Checks if a string starts with another string.
13
     *
14
     * @param string $haystack The string to search in.
15
     * @param string $needle The string to search.
16
     * @return bool
17
     */
18
    public static function startsWith(string $haystack, string $needle): bool
19
    {
20
        return $needle === "" || strpos($haystack, $needle) === 0;
21
    }
22
23
    /**
24
     * Checks if a string ends with another string.
25
     *
26
     * @param string $haystack The string to search in.
27
     * @param string $needle The string to search.
28
     * @return bool
29
     */
30
    public static function endsWith(string $haystack, string $needle): bool
31
    {
32
        return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
33
    }
34
35
    /**
36
     * Get the first character of a string.
37
     *
38
     * @param string $str The string to get the first character of.
39
     * @return string|false The first character or false if not found.
40
     */
41
    public static function getFirstChar($str)
42
    {
43
        $length = strlen($str);
44
        if ($length === 0) {
45
            return false;
46
        }
47
48
        return substr($str, 0, 1);
49
    }
50
51
    /**
52
     * Get the last character of a string.
53
     *
54
     * @param string false The string to get the last character of.
55
     * @return string|null The last character or false if not found.
56
     */
57
    public static function getLastChar($str)
58
    {
59
        $length = strlen($str);
60
        if ($length === 0) {
61
            return false;
62
        }
63
64
        return substr($str, $length - 1, 1);
65
    }
66
}
67