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 ( 8a4525...04b2e6 )
by t
05:00 queued 40s
created

Numbers::toUpperChineseCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 17
ccs 0
cts 17
cp 0
crap 2
rs 9.7666
1
<?php
2
/**
3
 * Class Numbers
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
namespace icy2003\php\ihelpers;
10
11
use icy2003\php\C;
12
use icy2003\php\I;
13
14
/**
15
 * 数字处理
16
 */
17
class Numbers
18
{
19
    /**
20
     * 比较两个任意精度的数字
21
     *
22
     * @param string $number1 数字1
23
     * @param string $number2 数字2
24
     * @param integer $scale 精确度,默认精确到小数点后 2 位
25
     *
26
     * @return boolean
27
     */
28
    public static function isEquals($number1, $number2, $scale = 2)
29
    {
30
        return 0 === bccomp($number1, $number2, $scale);
31
    }
32
33
    /**
34
     * 加载一些奇怪的常量
35
     *
36
     * 我知道这些符号不好打,所以从注释里复制喽
37
     *
38
     * @constant double π 圆周率
39
     * @constant double e 自然常数
40
     * @constant double γ 欧拉常数
41
     * @constant double φ 黄金比
42
     * @constant double c 光速
43
     *
44
     * @return void
45
     */
46
    public static function load()
47
    {
48
        I::def('π', M_PI);
49
        I::def('e', M_E);
50
        I::def('γ', M_EULER);
51
        I::def('φ', (sqrt(5) - 1) / 2);
52
        I::def('c', 2.99792458e8);
53
    }
54
55
    /**
56
     * 获取数字在指定长度的位置
57
     *
58
     * @param integer $number
59
     * @param integer $length
60
     *
61
     * @return integer
62
     */
63
    public static function position($number, $length)
64
    {
65
        while ($number < 0) {
66
            $number += $length;
67
        }
68
        return $number % $length;
69
    }
70
71
    /**
72
     * 转成字节数
73
     *
74
     * - 支持数字和单位之间有空格
75
     *
76
     * @param string $size 例如:10m、10M、10Tb、10kB 等
77
     *
78
     * @return integer
79
     */
80
    public static function toBytes($size)
81
    {
82
        $callback = function ($matches) {
83
            $sizeMap = [
84
                '' => 0,
85
                'b' => 0, // 为了简化正则
86
                'k' => 1,
87
                'm' => 2,
88
                'g' => 3,
89
                't' => 4,
90
                'p' => 5,
91
            ];
92
93
            return $matches[1] * pow(1024, $sizeMap[strtolower($matches[2])]);
94
        };
95
96
        return preg_replace_callback('/(\d*)\s*([a-z]?)b?/i', $callback, $size, 1);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_replace_call...', $callback, $size, 1) returns the type string which is incompatible with the documented return type integer.
Loading history...
97
    }
98
99
    /**
100
     * 字节数尽可能转成 k、m、g 等形式
101
     *
102
     * - 支持小单位转大单位
103
     *
104
     * @param integer $bytes
105
     *
106
     * @return string
107
     */
108
    public static function fromBytes($bytes)
109
    {
110
        $bytes = self::toBytes($bytes);
111
        $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
112
        return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), 2) . ' ' . $unit[$i];
113
    }
114
115
    /**
116
     * 比较两个值(如:10m、10M、10Tb、10kB 等)的大小
117
     *
118
     * - 0:相等,-1:左小于右,1:右小于左
119
     *
120
     * @param string $size1
121
     * @param string $size2
122
     *
123
     * @return integer
124
     */
125
    public static function compareSize($size1, $size2)
126
    {
127
        $bytes1 = self::toBytes($size1);
128
        $bytes2 = self::toBytes($size2);
129
        return ($v = $bytes1 - $bytes2) == 0 ? 0 : intval(($v) / abs($v));
130
    }
131
132
    /**
133
     * 将某进制的字符串转化成另一进制的字符串
134
     *
135
     * @see http://php.net/manual/zh/function.base-convert.php
136
     *
137
     * @param string $numberInput 待转换的字符串
138
     * @param string $fromBaseInput 起始进制的规则
139
     * @param string $toBaseInput 结束进制的规则
140
     *
141
     * @return string
142
     */
143
    public static function baseConvert($numberInput, $fromBaseInput, $toBaseInput)
144
    {
145
        if ($fromBaseInput == $toBaseInput) {
146
            return $numberInput;
147
        }
148
149
        $fromBase = Strings::split($fromBaseInput);
150
        $toBase = Strings::split($toBaseInput);
151
        $number = Strings::split($numberInput);
152
        $fromLen = Strings::length($fromBaseInput);
153
        $toLen = Strings::length($toBaseInput);
154
        $numberLen = Strings::length($numberInput);
155
        $retval = '';
156
        if ($toBaseInput == '0123456789') {
157
            $retval = 0;
158
            for ($i = 1; $i <= $numberLen; $i++) {
159
                $retval = bcadd($retval, bcmul(array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i)));
0 ignored issues
show
Bug introduced by
It seems like array_search($number[$i - 1], $fromBase) can also be of type false; however, parameter $left_operand of bcmul() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
                $retval = bcadd($retval, bcmul(/** @scrutinizer ignore-type */ array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i)));
Loading history...
160
            }
161
162
            return $retval;
163
        }
164
        if ($fromBaseInput != '0123456789') {
165
            $base10 = self::baseConvert($numberInput, $fromBaseInput, '0123456789');
166
        } else {
167
            $base10 = $numberInput;
168
        }
169
170
        if ($base10 < Strings::length($toBaseInput)) {
171
            return $toBase[$base10];
172
        }
173
174
        while ($base10 != '0') {
175
            $retval = $toBase[bcmod($base10, $toLen)] . $retval;
176
            $base10 = bcdiv($base10, $toLen, 0);
177
        }
178
        return $retval;
179
    }
180
181
    /**
182
     * 进制转换
183
     *
184
     * - 支持 2、8、10、16 进制之间转换
185
     *
186
     * @param string $number
187
     * @param integer $fromBase 2、8、10、16之间互转
188
     * @param integer $toBase 2、8、10、16之间互转
189
     *
190
     * @return string
191
     */
192
    public static function base($number, $fromBase, $toBase)
193
    {
194
        C::assertTrue(in_array($fromBase, [2, 8, 10, 16]), '原始进制必须为 2、8、10、16 之一');
195
        C::assertTrue(in_array($toBase, [2, 8, 10, 16]), '目标进制必须为 2、8、10、16 之一');
196
        $chars = '0123456789ABCDEF';
197
        return self::baseConvert($number, Strings::sub($chars, 0, $fromBase), Strings::sub($chars, 0, $toBase));
198
    }
199
200
    /**
201
     * 中文大写数字转成小写数字
202
     *
203
     * @param string $number
204
     *
205
     * @return string
206
     */
207
    public static function toLowerChineseCase($number)
208
    {
209
        return Strings::replace($number, [
210
            '零' => '〇',
211
            '壹' => '一',
212
            '贰' => '二',
213
            '叁' => '三',
214
            '肆' => '四',
215
            '伍' => '五',
216
            '陆' => '六',
217
            '柒' => '七',
218
            '捌' => '八',
219
            '玖' => '九',
220
            '拾' => '十',
221
            '佰' => '百',
222
            '仟' => '千',
223
            '圆' => '元',
224
        ]);
225
    }
226
227
    /**
228
     * 中文小写数字转成大写数字
229
     *
230
     * @param string $number
231
     *
232
     * @return string
233
     */
234
    public static function toUpperChineseCase($number)
235
    {
236
        return Strings::replace($number, [
237
            '〇' => '零',
238
            '一' => '壹',
239
            '二' => '贰',
240
            '三' => '叁',
241
            '四' => '肆',
242
            '五' => '伍',
243
            '六' => '陆',
244
            '七' => '柒',
245
            '八' => '捌',
246
            '九' => '玖',
247
            '十' => '拾',
248
            '百' => '佰',
249
            '千' => '仟',
250
            '元' => '圆',
251
        ]);
252
    }
253
}
254