Tools::iteratorGet()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 9.2222
cc 6
nc 6
nop 3
crap 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: yiranzai
5
 * Date: 19-3-22
6
 * Time: 上午10:59
7
 */
8
9
namespace Yiranzai\Tools;
10
11
use ArrayAccess;
12
use Exception;
13
14
class Tools
15
{
16
    /**
17
     * 转化内存信息
18
     * @param      $bytes
19
     * @param bool $binaryPrefix
20
     * @return string
21
     */
22 3
    public static function getNiceFileSize(int $bytes, bool $binaryPrefix = true): string
23
    {
24 3
        if ($binaryPrefix) {
25 3
            $unit = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
26 3
            if ($bytes === 0) {
27 3
                return '0 ' . $unit[0];
28
            }
29 3
            return @round($bytes / (1024 ** ($i = floor(log($bytes, 1024)))), 2) . ' ' . ($unit[(int)$i] ?? 'B');
30
        }
31
32 3
        $unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
33 3
        if ($bytes === 0) {
34 3
            return '0 ' . $unit[0];
35
        }
36 3
        return @round($bytes / (1000 ** ($i = floor(log($bytes, 1000)))), 2) . ' ' . ($unit[(int)$i] ?? 'B');
37
    }
38
39
    /**
40
     * 调用对象的方法
41
     * @param object $object
42
     * @param string $func
43
     * @param array  $params
44
     * @param null   $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
45
     * @return mixed|null
46
     */
47 3
    public static function callFunc($object, $func, array $params = [], $default = null)
48
    {
49 3
        if (is_object($object) && method_exists($object, $func)) {
50 3
            return call_user_func_array([$object, $func], $params);
51
        }
52 3
        return $default;
53
    }
54
55
    /**
56
     * 获取一个对象或者一个数组的属性
57
     *
58
     * @param            $iterator
59
     * @param mixed      $key
60
     * @param mixed|null $default
61
     * @return mixed
62
     * @throws Exception
63
     */
64 12
    public static function iteratorGet($iterator, $key, $default = null)
65
    {
66 12
        if (empty($iterator)) {
67 6
            if ($default instanceof Exception) {
68 3
                throw $default;
69
            }
70
71 3
            return $default;
72
        }
73
74 9
        if (is_object($iterator)) {
75 6
            if (!method_exists($iterator, $key)) {
76 6
                if ($iterator instanceof ArrayAccess) {
77 3
                    return self::arrGet($iterator, $key, $default);
78
                }
79 6
                return self::objectGet($iterator, $key, $default);
80
            }
81
            //  对象获取
82 3
            return self::objectGet($iterator, $key, $default);
83
        }
84
85
        //  数组获取
86 6
        return self::arrGet($iterator, $key, $default);
87
    }
88
89
    /**
90
     * 获取数组中的某个元素
91
     * @param array|mixed $arr     数组
92
     * @param mixed       $key     下标
93
     * @param null|mixed  $default 默认值
94
     * @return mixed|null   如果存在指定元素则返回元素,否则返回默认值
95
     * @throws Exception
96
     */
97 15
    public static function arrGet($arr, $key, $default = null)
98
    {
99 15
        $isDefault = false;
100
101 15
        if (empty($arr) || (empty($key) && 0 !== $key)) {
102 3
            $isDefault = true;
103 15
        } elseif (!isset($arr[$key])) {
104 12
            $isDefault = true;
105
        }
106
107 15
        if ($isDefault) {
108 12
            if ($default instanceof Exception) {
109 6
                throw $default;
110
            }
111 6
            return $default;
112
        }
113
114 9
        return $arr[$key];
115
    }
116
117
    /**
118
     * 获取对象中的某个元素
119
     * @param object     $json    json对象
120
     * @param string     $key     下标
121
     * @param null|mixed $default 默认值
122
     * @return mixed|null 如果存在指定元素则返回该元素,否则返回默认值
123
     * @throws Exception
124
     */
125 12
    public static function objectGet($json, $key, $default = null)
126
    {
127 12
        $isDefault = false;
128
129 12
        if (empty($json) || empty($key)) {
130 3
            $isDefault = true;
131 12
        } elseif (!isset($json->{$key})) {
132 12
            $isDefault = true;
133
        }
134
135 12
        if ($isDefault) {
136 12
            if ($default instanceof Exception) {
137 6
                throw $default;
138
            }
139
140 6
            return $default;
141
        }
142
143 6
        return $json->{$key};
144
    }
145
}
146