• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taobig / php-helper / 10330426697

10 Aug 2024 08:58AM UTC coverage: 91.718% (-0.1%) from 91.853%
10330426697

push

github

taobig
remove @deprecated methods & constants:
1. taobig\helpers\composer\Version::PACKAGE_PROD(use Version::PACKAGE instead)
2. taobig\helpers\StringHelper::leftPadding() (use StringHelper::prepend() instead)
3. taobig\helpers\ArrayHelper::groupBy() (use groupByColumn() instead)
4. taobig\helpers\ArrayHelper::removeEmptyElement() (use removeEmpty() instead)
5. taobig\helpers\ArrayHelper::removeSpecifiedElement() (use removeSpecified() instead)
6. taobig\helpers\lock\Lock::name() (use getName() instead)
7. taobig\helpers\RandomHelper::getRandomEnglishCharacters() (use str() instead)

443 of 483 relevant lines covered (91.72%)

1.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.83
/src/ArrayHelper.php
1
<?php
2

3
namespace taobig\helpers;
4

5
class ArrayHelper
6
{
7

8
    /**
9
     * @param array $arr
10
     * @param int $size split by $size
11
     * @param bool $preserveKeys
12
     * @return array
13
     */
14
    public static function split(array $arr, int $size, bool $preserveKeys = false): array
15
    {
16
        $multiArr = [];
1✔
17
        if ($size <= 0) {
1✔
18
            return $multiArr;
1✔
19
        }
20
//
21
//        if ($size >= count($arr)) {
22
//            $multiArr[] = $arr;
23
//        } else {
24
//            $index = 0;
25
//            $length = 0;
26
//            foreach ($arr as $row) {
27
//                ++$length;
28
//                $multiArr[$index][] = $row;
29
//                if ($length >= $size) {
30
//                    ++$index;
31
//                    $length = 0;
32
//                }
33
//            }
34
//        }
35
//        return $multiArr;
36
        $multiArr = array_chunk($arr, $size, $preserveKeys);
1✔
37
        return $multiArr;
1✔
38
    }
39

40
    public static function groupByColumn(array $arr, $columnKey, bool $skipIllegalKeys = false): array
41
    {
42
        $multiArr = [];
3✔
43
        foreach ($arr as $row) {
3✔
44
            $val = $row[$columnKey];
3✔
45
            if (is_int($val) || is_string($val)) {
3✔
46
                $multiArr[$val][] = $row;
3✔
47
            } else {
48
                if (!$skipIllegalKeys) {
3✔
49
                    throw new \ValueError("Illegal offset type");
2✔
50
                }
51
            }
52
        }
53
        return $multiArr;
1✔
54
    }
55

56
    /**
57
     * convert all keys from underscore-style to camelcase-style.
58
     * 如果转换后的key已经存在,则使用原始值
59
     * @param array $arr
60
     * @param bool $reserveOriginValue
61
     * @return array
62
     */
63
    public static function underscore2camelcase(array $arr, bool $reserveOriginValue = true): array
64
    {
65
        $result = [];
1✔
66
        if (is_array($arr)) {
1✔
67
            foreach ($arr as $key => $val) {
1✔
68
                $camelcaseKey = StringHelper::snakeCase2CamelCase($key);
1✔
69
                if (isset($result[$camelcaseKey])) {
1✔
70
                    if ($reserveOriginValue) {
1✔
71
                        continue;
1✔
72
                    } else {
73
                        $result[$camelcaseKey] = $val;
1✔
74
                    }
75
                }
76
                if (!is_array($val)) {
1✔
77
                    $result[$camelcaseKey] = $val;
1✔
78
                } else {
79
                    $result[$camelcaseKey] = self::underscore2camelcase($val);
1✔
80
                }
81
            }
82
        }
83
        return $result;
1✔
84
    }
85

86
    public static function removeEmpty(array $arr, bool $preserveKeys = true): array
87
    {
88
        if ($preserveKeys) {
1✔
89
            return array_filter($arr);
1✔
90
        } else {
91
            return array_values(array_filter($arr));
1✔
92
        }
93
    }
94

95
    public static function removeSpecified(array $arr, $specifiedElement = null, bool $strictType = false, bool $preserveKeys = true): array
96
    {
97
        $result = array_filter($arr, function ($v) use ($specifiedElement, $strictType) {
1✔
98
            if ($strictType) {
1✔
99
                if ($v === $specifiedElement) {
1✔
100
                    return false;
1✔
101
                }
102
            } else {
103
                if ($v == $specifiedElement) {
1✔
104
                    return false;
1✔
105
                }
106
            }
107
            return true;
1✔
108
        });
1✔
109
        if (!$preserveKeys) {
1✔
110
            return array_values($result);
1✔
111
        }
112
        return $result;
1✔
113
    }
114

115
    /**
116
     * 判断数组是关联数组(associative array)
117
     * @param array $arr
118
     * @return bool
119
     */
120
    public static function isAssocArray(array $arr): bool
121
    {
122
        if (empty($arr)) {
1✔
123
            return true;
1✔
124
        }
125
        return array_keys($arr) !== range(0, count($arr) - 1);
1✔
126
    }
127

128
    /**
129
     * 判断数组是索引数组(indexed array)
130
     * 1. 包含有合法整型值的字符串会被转换为整型。例如键名 "8" 实际会被储存为 8。但是 "08" 则不会强制转换,因为其不是一个合法的十进制数值。
131
     * 2. 浮点数也会被转换为整型,意味着其小数部分会被舍去。例如键名 8.7 实际会被储存为 8。
132
     * 3. 布尔值也会被转换成整型。即键名 true 实际会被储存为 1 而键名 false 会被储存为 0。
133
     * 4. Null 会被转换为空字符串,即键名 null 实际会被储存为 ""。
134
     * 5. 数组和对象不能被用为键名。坚持这么做会导致警告:Illegal offset type。
135
     * 即 $arr[1] === $arr['1'] === $arr[1.5] === $arr[true] === $arr[01]; 但是$arr['01']实际存储的key是字符串'01'。
136
     * @param array $arr
137
     * @return bool
138
     * @deprecated
139
     * @see array_is_list Since PHP 8.1
140
     */
141
    public static function isIndexedArray(array $arr): bool
142
    {
143
        if (PHP_VERSION_ID >= 80100) {
1✔
144
            return array_is_list($arr);//Since PHP 8.1
1✔
145
        }
146
        if (empty($arr)) {
×
147
            return true;
×
148
        }
149
        return array_keys($arr) === range(0, count($arr) - 1);
×
150
    }
151

152
    /**
153
     * convert object 2 array, will ignore resources and \__PHP_Incomplete_Class
154
     * @see https://www.php.net/manual/en/function.gettype.php
155
     * @see https://www.php.net/manual/en/function.is-resource.php
156
     * @see https://www.php.net/manual/en/function.is-object.php
157
     * 不能用json_decode(json_encode($obj), true),因为json_encode不支持resource
158
     * @param object $obj
159
     * @return array
160
     */
161
    public static function object2Array($obj)
162
    {
163
        $arr = (array)$obj;
1✔
164
        foreach ($arr as $k => $v) {
1✔
165
            //is_resource() is not a strict type-checking method: it will return FALSE if var is a resource variable that has been closed.
166
            if (in_array(gettype($v), ['resource', 'resource (closed)', 'unknown type'])) {
1✔
167
                unset($arr[$k]);
1✔
168
                continue;
1✔
169
            }
170
            if ($v instanceof \__PHP_Incomplete_Class) {
1✔
171
                unset($arr[$k]);
1✔
172
                continue;
1✔
173
            }
174
            //7.2.0        is_object() now returns TRUE for unserialized objects without a class definition (class of __PHP_Incomplete_Class). Previously FALSE was returned.
175
            // >= 7.2.0  gettype($v) => 'object'   and  is_object($v) => true
176
            // < 7.2.0   gettype($v) => 'object'   and  is_object($v) => false
177
            if (gettype($v) === 'object' || gettype($v) === 'array') {
1✔
178
                $arr[$k] = self::object2Array($v);
1✔
179
            }
180
        }
181
        return $arr;
1✔
182
    }
183

184
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc