Completed
Push — master ( b5bb9d...38f7c0 )
by f
02:02
created

src/S.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace morphos;
3
4
/**
5
 * Multibyte string helper
6
 */
7
class S
8
{
9
    /**
10
     * Sets encoding for using in morphos/* functions.
11
     * @param string $encoding
12
     * @return bool
13
     */
14
    public static function setEncoding($encoding)
15
    {
16
        if (function_exists('mb_internal_encoding')) {
17
            mb_internal_encoding($encoding);
18
        } elseif (function_exists('iconv_set_encoding')) {
19
            iconv_set_encoding('internal_encoding', $encoding);
20
        } else {
21
            return false;
22
        }
23
    }
24
25
    /**
26
     * Calculates count of characters in string.
27
     * @param $string
28
     * @return bool|int
29
     */
30 38
    public static function length($string)
31
    {
32 38
        if (function_exists('mb_strlen')) {
33 38
            return mb_strlen($string);
34
        }
35
36
        if (function_exists('iconv_strlen')) {
37
            return iconv_strlen($string);
38
        }
39
40
        return false;
41
    }
42
43
    /**
44
     * Slices string like python.
45
     * @param string $string
46
     * @param int $start
47
     * @param int|null $end
48
     * @return bool|string
49
     */
50 1416
    public static function slice($string, $start, $end = null)
51
    {
52 1416
        if ($end != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $end of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
53 595
            $end -= $start;
54
        }
55
56 1416
        if (function_exists('mb_substr')) {
57 1416
            return $end === null ? mb_substr($string, $start) : mb_substr($string, $start, $end);
58
        }
59
60
        if (function_exists('iconv_substr')) {
61
            return $end === null ? iconv_substr($string, $start) : iconv_substr($string, $start, $end);
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * Lower case.
69
     * @param $string
70
     * @return bool|string
71
     */
72 1669
    public static function lower($string)
73
    {
74 1669
        if (function_exists('mb_strtolower')) {
75 1669
            return mb_strtolower($string);
76
        }
77
78
        return false;
79
    }
80
81
    /**
82
     * Upper case.
83
     * @param $string
84
     * @return bool|string
85
     */
86 208
    public static function upper($string)
87
    {
88 208
        if (function_exists('mb_strtoupper')) {
89 208
            return mb_strtoupper($string);
90
        }
91
92
        return false;
93
    }
94
95
    /**
96
     * Name case. (ex: Thomas, Lewis)
97
     * @param $string
98
     * @return bool|string
99
     */
100 206
    public static function name($string)
101
    {
102 206
        if (function_exists('mb_strtoupper')) {
103 206
            return implode('-', array_map(function ($word) {
104 206
                return self::upper(self::slice($word, 0, 1)).self::lower(self::slice($word, 1));
105 206
            }, explode('-', $string)));
106
        }
107
108
        return false;
109
    }
110
111
    /**
112
     * multiple substr_count().
113
     * @param $string
114
     * @param array $chars
115
     * @return bool|int
116
     */
117
    public static function countChars($string, array $chars)
118
    {
119
        if (function_exists('mb_split')) {
120
            return count(mb_split('('.implode('|', $chars).')', $string)) - 1;
121
        }
122
123
        return false;
124
    }
125
126
    /**
127
     * @param string $string
128
     * @param string $char
129
     * @return bool|string
130
     */
131 9
    public static function findFirstPosition($string, $char)
132
    {
133 9
        if (function_exists('mb_strpos')) {
134 9
            return mb_strpos($string, $char, 0);
135
        }
136
137
        return strpos($string, $char);
138
    }
139
140
    /**
141
     * @param string $string
142
     * @param string $char
143
     * @return bool|string
144
     */
145 6
    public static function findLastPosition($string, $char)
146
    {
147 6
        if (function_exists('mb_strrpos')) {
148 6
            return mb_strrpos($string, $char, 0);
149
        }
150
151
        return strrpos($string, $char);
152
    }
153
154
    /**
155
     * @param $string
156
     * @param array $chars
157
     * @return bool|string
158
     */
159 46
    public static function findLastPositionForOneOfChars($string, array $chars)
160
    {
161 46
        if (function_exists('mb_strrpos')) {
162 46
            $last_position = false;
163 46
            foreach ($chars as $char) {
164 46
                if (($pos = mb_strrpos($string, $char)) !== false) {
165 46
                    if ($pos > $last_position) {
166 46
                        $last_position = $pos;
167
                    }
168
                }
169
            }
170 46
            if ($last_position !== false) {
171 46
                return mb_substr($string, $last_position);
172
            }
173
            return false;
174
        }
175
176
        return false;
177
    }
178
179
    /**
180
     * @param $string
181
     * @param $substring
182
     * @param bool $caseSensetive
183
     * @param int $startOffset
184
     * @return string|false
185
     */
186 78
    public static function indexOf($string, $substring, $caseSensetive = false, $startOffset = 0)
187
    {
188 78
        if (function_exists('mb_stripos')) {
189 78
            return $caseSensetive ? mb_strpos($string, $substring, $startOffset) : mb_stripos($string, $substring, $startOffset);
190
        }
191
192
        return false;
193
    }
194
}
195