Completed
Push — master ( e4e394...127b74 )
by f
01:37
created

S::findLastPositionForOneOfChars()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 9
nop 2
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
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
     */
12
    public static function setEncoding($encoding)
13
    {
14
        if (function_exists('mb_internal_encoding')) {
15
            mb_internal_encoding($encoding);
16
        } elseif (function_exists('iconv_set_encoding')) {
17
            iconv_set_encoding('internal_encoding', $encoding);
18
        } else {
19
            return false;
20
        }
21
    }
22
23
    /**
24
     * Calcules count of characters in string.
25
     */
26
    public static function length($string)
27
    {
28
        if (function_exists('mb_strlen')) {
29
            return mb_strlen($string);
30
        } elseif (function_exists('iconv_strlen')) {
31
            return iconv_strlen($string);
32
        } else {
33
            return false;
34
        }
35
    }
36
37
    /**
38
     * Slices string like python.
39
     */
40
    public static function slice($string, $start, $end = null)
41
    {
42
        if ($end != null) {
43
            $end -= $start;
44
        }
45
46
        if (function_exists('mb_substr')) {
47
            return $end === null ? mb_substr($string, $start) : mb_substr($string, $start, $end);
48
        } elseif (function_exists('iconv_substr')) {
49
            return $end === null ? iconv_substr($string, $start) : iconv_substr($string, $start, $end);
50
        } else {
51
            return false;
52
        }
53
    }
54
55
    /**
56
     * Lower case.
57
     */
58
    public static function lower($string)
59
    {
60
        if (function_exists('mb_strtolower')) {
61
            return mb_strtolower($string);
62
        } else {
63
            return false;
64
        }
65
    }
66
67
    /**
68
     * Upper case.
69
     */
70
    public static function upper($string)
71
    {
72
        if (function_exists('mb_strtoupper')) {
73
            return mb_strtoupper($string);
74
        } else {
75
            return false;
76
        }
77
    }
78
79
    /**
80
     * Name case. (ex: Thomas, Lewis)
81
     */
82
    public static function name($string)
83
    {
84
        if (function_exists('mb_strtoupper')) {
85
            return implode('-', array_map(function ($word) {
86
                return self::upper(self::slice($word, 0, 1)).self::lower(self::slice($word, 1));
87
            }, explode('-', $string)));
88
        } else {
89
            return false;
90
        }
91
    }
92
93
    /**
94
     * multiple substr_count().
95
     */
96
    public static function countChars($string, array $chars)
97
    {
98
        if (function_exists('mb_split')) {
99
            return count(mb_split('('.implode('|', $chars).')', $string)) - 1;
100
        } else {
101
            return false;
102
        }
103
    }
104
105
    public static function findLastPositionForOneOfChars($string, array $chars)
106
    {
107
        if (function_exists('mb_strrpos')) {
108
            $last_position = false;
109
            foreach ($chars as $char) {
110
                if (($pos = mb_strrpos($string, $char)) !== false) {
111
                    if ($pos > $last_position) {
112
                        $last_position = $pos;
113
                    }
114
                }
115
            }
116
            if ($last_position !== false) {
117
                return mb_substr($string, $last_position);
118
            }
119
            return false;
120
        } else {
121
            return false;
122
        }
123
    }
124
125
    public static function indexOf($string, $substring, $caseSensetive = false, $startOffset = 0)
126
    {
127
        if (function_exists('mb_stripos')) {
128
            return $caseSensetive ? mb_strpos($string, $substring, $startOffset) : mb_stripos($string, $substring, $startOffset);
129
        } else {
130
            return false;
131
        }
132
    }
133
}
134