GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 966fe3...40d637 )
by t
04:58 queued 02:23
created

Strings   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 310
Duplicated Lines 0 %

Test Coverage

Coverage 11.11%

Importance

Changes 16
Bugs 1 Features 0
Metric Value
eloc 42
c 16
b 1
f 0
dl 0
loc 310
ccs 6
cts 54
cp 0.1111
rs 10
wmc 28

18 Methods

Rating   Name   Duplication   Size   Complexity  
A toNumber() 0 3 1
A isEndsWith() 0 3 2
A partBefore() 0 6 2
A map() 0 8 2
A byteLength() 0 3 1
A isStartsWith() 0 3 2
A isContains() 0 3 2
A length() 0 3 1
A random() 0 8 2
A toArray() 0 10 3
A toCamel() 0 5 1
A split() 0 3 1
A toUnderline() 0 3 1
A repeat() 0 4 2
A reverse() 0 3 1
A sub() 0 3 1
A partAfter() 0 6 2
A toTitle() 0 3 1
1
<?php
2
/**
3
 * Class Strings
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
10
namespace icy2003\php\ihelpers;
11
12
use icy2003\php\I;
13
14
/**
15
 * 字符串类
16
 *
17
 * @test icy2003\php_tests\ihelpers\StringsTest
18
 */
19
class Strings
20
{
21
22
    use StringsTrait;
23
24
    /**
25
     * 返回字符串的字节长(一个中文等于 3 字节哦~)
26
     *
27
     * @param string $string
28
     *
29
     * @return integer
30
     *
31
     * @tested
32
     */
33
    public static function byteLength($string)
34
    {
35
        return mb_strlen($string, '8bit');
36
    }
37
    /**
38
     * 返回字符个数(一个中文就是 1 个啦~)
39
     *
40
     * @param string $string
41
     *
42
     * @return integer
43
     *
44
     * @tested
45
     */
46
    public static function length($string)
47
    {
48
        return mb_strlen($string, 'UTF-8');
49
    }
50
51
    /**
52
     * 随机数种子(数字)
53
     */
54
    const STRINGS_RANDOM_NUMBER = '0123456789';
55
56
    /**
57
     * 随机数种子(小写字母)
58
     */
59
    const STRINGS_RANDOM_LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';
60
61
    /**
62
     * 随机数种子(大写字母)
63
     */
64
    const STRINGS_RANDOM_UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
65
66
    /**
67
     * 生成随机字符串
68
     *
69
     * @param integer $length 随机字符串的长度,默认 32
70
     * @param string $chars 字符列表,默认为0-9和大小写字母
71
     *
72
     * @return string
73
     *
74
     * @test
75
     */
76
    public static function random($length = 32, $chars = self::STRINGS_RANDOM_NUMBER . self::STRINGS_RANDOM_LOWERCASE . self::STRINGS_RANDOM_UPPERCASE)
77
    {
78
        $str = '';
79
        for ($i = 0; $i < $length; ++$i) {
80
            $str .= mb_substr($chars, mt_rand(0, self::length($chars) - 1), 1);
81
        }
82
83
        return $str;
84
    }
85
86
    /**
87
     * 小驼峰转化成下划线(如需要大写下划线,用 strtoupper 转化即可)
88
     *
89
     * @param string $string
90
     *
91
     * @return string
92
     *
93
     * @tested
94
     */
95
    public static function toUnderline($string)
96
    {
97
        return strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $string));
98
    }
99
100
    /**
101
     * 下划线转化为小驼峰(如需要大驼峰,用 ucfirst 转化即可)
102
     *
103
     * @param string $string
104
     *
105
     * @return string
106
     *
107
     * @tested
108
     */
109
    public static function toCamel($string)
110
    {
111
        return lcfirst(preg_replace_callback('/_+([a-z0-9_\x7f-\xff])/', function ($matches) {
112
            return ucfirst($matches[1]);
113
        }, strtolower($string)));
114
    }
115
116
    /**
117
     * 格式化成标题格式(每个单词首字母大写)
118
     *
119
     * @param string $string
120
     *
121
     * @return string
122
     *
123
     * @tested
124
     */
125
    public static function toTitle($string)
126
    {
127
        return mb_convert_case($string, MB_CASE_TITLE, 'UTF-8');
128
    }
129
130
    /**
131
     * 检查字符串是否以某字符串开头
132
     *
133
     * @param string $string
134
     * @param string $search 待搜索子字符串
135
     *
136
     * @return boolean
137
     *
138
     * @tested
139
     */
140
    public static function isStartsWith($string, $search)
141
    {
142
        return (string) $search !== "" && mb_strpos($string, $search) === 0;
143
    }
144
145
    /**
146
     * 检查字符串是否以某字符串结尾
147
     *
148
     * @param string $string
149
     * @param string $search 待搜索子字符串
150
     *
151
     * @return boolean
152
     *
153
     * @tested
154
     */
155
    public static function isEndsWith($string, $search)
156
    {
157
        return (string) $search !== "" && mb_substr($string, -static::length($search)) === $search;
158
    }
159
160
    /**
161
     * 检查字符串中是否包含某字符串
162
     *
163
     * @param string $string
164
     * @param string $search 待搜索子字符串
165
     * @param integer $pos 如果找到子串,则引用出子串的起始位置
166
     *
167
     * @return boolean
168
     *
169
     * @tested
170
     */
171
    public static function isContains($string, $search, &$pos = null)
172
    {
173
        return (string) $search !== "" && ($pos = mb_strpos($string, $search)) !== false;
174
    }
175
176
    /**
177
     * 在字符串里找子串的前部分
178
     *
179
     * @param string $string
180
     * @param string $search
181
     * @param integer $pos 如果找到子串,则引用出子串的起始位置
182
     *
183
     * @return string
184
     *
185
     * @tested
186
     */
187
    public static function partBefore($string, $search, &$pos = null)
188
    {
189
        if (self::isContains($string, $search, $pos)) {
190
            return mb_substr($string, 0, $pos);
191
        }
192
        return "";
193
    }
194
195
    /**
196
     * 在字符串里找子串的后部分
197
     *
198
     * @param string $string
199
     * @param string $search
200
     * @param integer $pos 如果找到子串,则引用出子串的起始位置
201
     *
202
     * @return string
203
     *
204
     * @tested
205
     */
206
    public static function partAfter($string, $search, &$pos = null)
207
    {
208
        if (self::isContains($string, $search, $pos)) {
209
            return mb_substr($string, $pos + self::length($search), self::length($string) - 1);
210
        }
211
        return "";
212
    }
213
214
    /**
215
     * 反转字符串,支持中文
216
     *
217
     * @param string $string
218
     *
219
     * @return string
220
     *
221
     * @tested
222
     */
223
    public static function reverse($string)
224
    {
225
        return implode('', array_reverse(self::split($string)));
226
    }
227
228
    /**
229
     * 把字符串打散为数组
230
     *
231
     * @param string $string
232
     *
233
     * @return array
234
     *
235
     * @tested
236
     */
237
    public static function split($string)
238
    {
239
        return preg_split('/(?<!^)(?!$)/u', $string);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_split('/(?<!^)(?!$)/u', $string) could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
240
    }
241
242
    /**
243
     * 拆分成数组
244
     *
245
     * @param array|string $mixed 数组或者字符串
246
     * @param string $delimiter 分隔符,默认英文逗号(,)
247
     * @param boolean $combine 是否合并相同元素,默认 false,即不合并
248
     *
249
     * @return array
250
     *
251
     * @tested
252
     */
253 3
    public static function toArray($mixed, $delimiter = ',', $combine = false)
254
    {
255 3
        if (is_array($mixed)) {
256 3
            $mixed = implode($delimiter, $mixed);
257
        }
258 3
        $array = explode($delimiter, $mixed);
259 3
        if (true === $combine) {
260
            $array = Arrays::toPart($array);
261
        }
262 3
        return $array;
263
    }
264
265
    /**
266
     * 返回字符串的子串
267
     *
268
     * @param string $string
269
     * @param integer $start 起始位置
270
     * @param integer|null $length 子串长度,默认为 null,即返回剩下的部分
271
     *
272
     * @return string
273
     *
274
     * @tested
275
     */
276
    public static function sub($string, $start, $length = null)
277
    {
278
        return mb_substr($string, $start, $length);
279
    }
280
281
    /**
282
     * 字符串转数字
283
     *
284
     * - 正则为 `/^\d\.*\d*[e|E]/` 的字符串会……,这是 PHP 特性!如果你不喜欢 PHP,右上角
285
     *
286
     * @param string $string
287
     *
288
     * @return double|integer
289
     *
290
     * @tested
291
     */
292
    public static function toNumber($string)
293
    {
294
        return $string + 0;
295
    }
296
297
    /**
298
     * 用回调将分隔符拆分出来的字符串执行后,用分隔符合并回去
299
     *
300
     * @param callback $callback 回调
301
     * @param string $string
302
     * @param string $delimiter 分隔符,默认英文逗号(,)
303
     *
304
     * @return string
305
     */
306
    public static function map($callback, $string, $delimiter = ',')
307
    {
308
        $arr = [];
309
        $parts = explode($delimiter, $string);
310
        foreach ($parts as $part) {
311
            $arr[] = I::trigger($callback, [$part]);
312
        }
313
        return implode($delimiter, $arr);
314
    }
315
316
    /**
317
     * 重复一个字符若干次
318
     *
319
     * @param string $char
320
     * @param integer $num
321
     * @param integer $maxLength 最大重复次数,默认不限制
322
     *
323
     * @return string
324
     */
325
    public static function repeat($char, $num, $maxLength = null)
326
    {
327
        $length = null === $maxLength ? $num : min($maxLength, $num);
328
        return str_repeat($char, $length);
329
    }
330
}
331