Completed
Push — master ( b8ee44...86e435 )
by Jim
02:27
created

Str::endsWith()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/15/2018
6
 * Time: 11:18 AM
7
 */
8
9
namespace TimSDK\Support;
10
11
class Str
12
{
13
    /**
14
     * The cache of snake-cased words.
15
     *
16
     * @var array
17
     */
18
    protected static $snakeCache = [];
19
20
    /**
21
     * Determine if a given string starts with a given substring.
22
     *
23
     * @param  string  $haystack
24
     * @param  string|array  $needles
25
     * @return bool
26
     */
27
    public static function startsWith($haystack, $needles)
28
    {
29
        foreach ((array) $needles as $needle) {
30
            if ($needle !== '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
31
                return true;
32
            }
33
        }
34
        return false;
35
    }
36
37
    /**
38
     * Determine if a given string ends with a given substring.
39
     *
40
     * @param  string  $haystack
41
     * @param  string|array  $needles
42
     * @return bool
43
     */
44
    public static function endsWith($haystack, $needles)
45
    {
46
        foreach ((array) $needles as $needle) {
47
            if (substr($haystack, -strlen($needle)) === (string) $needle) {
48
                return true;
49
            }
50
        }
51
        return false;
52
    }
53
54
    /**
55
     * Convert a string to snake case.
56
     *
57
     * @param  string  $value
58
     * @param  string  $delimiter
59
     * @return string
60
     */
61
    public static function snake($value, $delimiter = '_')
62
    {
63
        $key = $value;
64
65
        if (isset(static::$snakeCache[$key][$delimiter])) {
66
            return static::$snakeCache[$key][$delimiter];
67
        }
68
69
        if (! ctype_lower($value)) {
70
            $value = preg_replace('/\s+/u', '', ucwords($value));
71
72
            $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
73
        }
74
75
        return static::$snakeCache[$key][$delimiter] = $value;
76
    }
77
78
    /**
79
     * Convert the given string to lower-case.
80
     *
81
     * @param  string  $value
82
     * @return string
83
     */
84
    public static function lower($value)
85
    {
86
        return mb_strtolower($value, 'UTF-8');
87
    }
88
89
    /**
90
     * Convert the given string to upper-case.
91
     *
92
     * @param  string  $value
93
     * @return string
94
     */
95
    public static function upper($value)
96
    {
97
        return mb_strtoupper($value, 'UTF-8');
98
    }
99
}
100