1 | <?php |
||||
2 | /** |
||||
3 | * Class ArraysOperator |
||||
4 | * |
||||
5 | * @link https://www.icy2003.com/ |
||||
6 | * @author icy2003 <[email protected]> |
||||
7 | * @copyright Copyright (c) 2020, icy2003 |
||||
8 | */ |
||||
9 | |||||
10 | namespace icy2003\php\ihelpers; |
||||
11 | |||||
12 | use icy2003\php\I; |
||||
13 | |||||
14 | /** |
||||
15 | * 数组操作类 |
||||
16 | * |
||||
17 | * 常见数组格式的拼装和处理 |
||||
18 | */ |
||||
19 | class ArraysOperator |
||||
20 | { |
||||
21 | |||||
22 | /** |
||||
23 | * 多个数组 |
||||
24 | * |
||||
25 | * @var array |
||||
26 | */ |
||||
27 | protected $_array = []; |
||||
28 | |||||
29 | /** |
||||
30 | * 筛选出来的多个数组 |
||||
31 | * |
||||
32 | * @var array |
||||
33 | */ |
||||
34 | protected $_rows = []; |
||||
35 | |||||
36 | /** |
||||
37 | * 初始化 |
||||
38 | * |
||||
39 | * @param array $array 可传入多个数组 |
||||
40 | */ |
||||
41 | public function __construct($array) |
||||
42 | { |
||||
43 | $this->_array = func_get_args(); |
||||
44 | } |
||||
45 | |||||
46 | /** |
||||
47 | * 遍历数组,回调值为 true 时统计次数,到达指定次数时退出遍历 |
||||
48 | * |
||||
49 | * @param callback|true $callback 回调函数,true 时遍历整个数组 |
||||
50 | * @param integer $times |
||||
51 | * |
||||
52 | * @return static |
||||
53 | */ |
||||
54 | public function foreachTimes($callback, $times) |
||||
55 | { |
||||
56 | $array = array_shift($this->_array); |
||||
57 | $rows = []; |
||||
58 | $counter = 0; |
||||
59 | foreach ($array as $key => $value) { |
||||
60 | $result = I::call($callback, [$value, $key]); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
61 | if (true === $result) { |
||||
62 | $counter++; |
||||
63 | $rows[$key] = $value; |
||||
64 | } |
||||
65 | if ($counter === $times) { |
||||
66 | break; |
||||
67 | } |
||||
68 | } |
||||
69 | $this->_rows[] = $rows; |
||||
70 | |||||
71 | return $this; |
||||
72 | } |
||||
73 | |||||
74 | /** |
||||
75 | * 回调值为 true 时停止遍历 |
||||
76 | * |
||||
77 | * @param callback|true $callback 回调函数,true 时遍历整个数组 |
||||
78 | * |
||||
79 | * @return static |
||||
80 | */ |
||||
81 | public function foreachOnce($callback) |
||||
82 | { |
||||
83 | return $this->foreachTimes($callback, 1); |
||||
84 | } |
||||
85 | |||||
86 | /** |
||||
87 | * 遍历数组,回调值为 true 并且再次遇到非 ture 值时统计次数,到达指定次数时退出遍历 |
||||
88 | * |
||||
89 | * @param callback|treu $callback 回调函数,true 时遍历整个数组 |
||||
0 ignored issues
–
show
The type
icy2003\php\ihelpers\treu was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
90 | * @param integer $times |
||||
91 | * |
||||
92 | * @return static |
||||
93 | */ |
||||
94 | public function foreachTimesUntil($callback, $times) |
||||
95 | { |
||||
96 | $array = array_shift($this->_array); |
||||
97 | $rows = []; |
||||
98 | $counter = 0; |
||||
99 | $active = false; |
||||
100 | foreach ($array as $key => $value) { |
||||
101 | $result = I::call($callback, [$value, $key]); |
||||
102 | if (true === $result) { |
||||
103 | if (false === $active) { |
||||
104 | $counter++; |
||||
105 | $active = true; |
||||
106 | } |
||||
107 | $rows[$key] = $value; |
||||
108 | } else { |
||||
109 | $active = false; |
||||
110 | if ($counter === $times) { |
||||
111 | break; |
||||
112 | } |
||||
113 | } |
||||
114 | } |
||||
115 | $this->_rows[] = $rows; |
||||
116 | |||||
117 | return $this; |
||||
118 | } |
||||
119 | |||||
120 | /** |
||||
121 | * 遍历数组,回调值为 true 并且再次遇到非 ture 值时退出遍历 |
||||
122 | * |
||||
123 | * @param callback|true $callback 回调函数,true 时遍历整个数组 |
||||
124 | * |
||||
125 | * @return static |
||||
126 | */ |
||||
127 | public function foreachOnceUntil($callback) |
||||
128 | { |
||||
129 | return $this->foreachTimesUntil($callback, 1); |
||||
0 ignored issues
–
show
It seems like
$callback can also be of type true ; however, parameter $callback of icy2003\php\ihelpers\Arr...or::foreachTimesUntil() does only seem to accept callable|icy2003\php\ihelpers\treu , 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
![]() |
|||||
130 | } |
||||
131 | |||||
132 | /** |
||||
133 | * 返回被筛选出来的数组 |
||||
134 | * |
||||
135 | * - 保留原键 |
||||
136 | * |
||||
137 | * @return array |
||||
138 | */ |
||||
139 | public function getRows() |
||||
140 | { |
||||
141 | return $this->_rows; |
||||
142 | } |
||||
143 | |||||
144 | /** |
||||
145 | * 清空被筛选的数组 |
||||
146 | * |
||||
147 | * @return void |
||||
148 | */ |
||||
149 | public function clear() |
||||
150 | { |
||||
151 | $this->_rows = []; |
||||
152 | } |
||||
153 | } |
||||
154 |