-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ZipIterator.php
174 lines (156 loc) · 4.23 KB
/
ZipIterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.5
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Collection\Iterator;
use Cake\Collection\Collection;
use Cake\Collection\CollectionInterface;
use Cake\Collection\CollectionTrait;
use MultipleIterator;
/**
* Creates an iterator that returns elements grouped in pairs
*
* ### Example
*
* ```
* $iterator = new ZipIterator([[1, 2], [3, 4]]);
* $iterator->toList(); // Returns [[1, 3], [2, 4]]
* ```
*
* You can also chose a custom function to zip the elements together, such
* as doing a sum by index:
*
* ### Example
*
* ```
* $iterator = new ZipIterator([[1, 2], [3, 4]], function ($a, $b) {
* return $a + $b;
* });
* $iterator->toList(); // Returns [4, 6]
* ```
*/
class ZipIterator implements CollectionInterface
{
use CollectionTrait;
/**
* @var \MultipleIterator
*/
protected MultipleIterator $multipleIterator;
/**
* The function to use for zipping items together
*
* @var callable|null
*/
protected $_callback;
/**
* Contains the original iterator objects that were attached
*
* @var array
*/
protected array $_iterators = [];
/**
* Creates the iterator to merge together the values by for all the passed
* iterators by their corresponding index.
*
* @param array $sets The list of array or iterators to be zipped.
* @param callable|null $callable The function to use for zipping the elements of each iterator.
*/
public function __construct(array $sets, ?callable $callable = null)
{
$this->multipleIterator = new MultipleIterator(
MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC
);
$this->_callback = $callable;
foreach ($sets as $set) {
$iterator = (new Collection($set))->unwrap();
$this->_iterators[] = $iterator;
$this->multipleIterator->attachIterator($iterator);
}
}
/**
* Returns the value resulting out of zipping all the elements for all the
* iterators with the same positional index.
*
* @return mixed
*/
public function current(): mixed
{
$current = $this->multipleIterator->current();
if ($this->_callback) {
return call_user_func_array($this->_callback, $current);
}
return $current;
}
/**
* Implements Iterator::key().
*
* @return mixed
*/
public function key(): mixed
{
return $this->multipleIterator->key();
}
/**
* Implements Iterator::next().
*
* @return void
*/
public function next(): void
{
$this->multipleIterator->next();
}
/**
* Implements Iterator::rewind().
*
* @return void
*/
public function rewind(): void
{
$this->multipleIterator->rewind();
}
/**
* Implements Iterator::valid().
*
* @return bool
*/
public function valid(): bool
{
return $this->multipleIterator->valid();
}
/**
* Magic method used for serializing the iterator instance.
*
* @return array
*/
public function __serialize(): array
{
return $this->_iterators;
}
/**
* Magic method used to rebuild the iterator instance.
*
* @param array $data Data array.
* @return void
*/
public function __unserialize(array $data): void
{
$this->multipleIterator = new MultipleIterator(
MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC
);
$this->_iterators = $data;
foreach ($this->_iterators as $it) {
$this->multipleIterator->attachIterator($it);
}
}
}