|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the League.csv library |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT |
|
6
|
|
|
* @link https://github.com/thephpleague/csv/ |
|
7
|
|
|
* @version 9.0.0 |
|
8
|
|
|
* @package League.csv |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
declare(strict_types=1); |
|
14
|
|
|
|
|
15
|
|
|
namespace League\Csv; |
|
16
|
|
|
|
|
17
|
|
|
use ArrayIterator; |
|
18
|
|
|
use CallbackFilterIterator; |
|
19
|
|
|
use Iterator; |
|
20
|
|
|
use LimitIterator; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* A trait to manage filtering a CSV |
|
24
|
|
|
* |
|
25
|
|
|
* @package League.csv |
|
26
|
|
|
* @since 9.0.0 |
|
27
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
class Statement |
|
31
|
|
|
{ |
|
32
|
|
|
use ValidatorTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Callables to filter the iterator |
|
36
|
|
|
* |
|
37
|
|
|
* @var callable[] |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $where = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Callables to sort the iterator |
|
43
|
|
|
* |
|
44
|
|
|
* @var callable[] |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $order_by = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* iterator Offset |
|
50
|
|
|
* |
|
51
|
|
|
* @var int |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $offset = 0; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* iterator maximum length |
|
57
|
|
|
* |
|
58
|
|
|
* @var int |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $limit = -1; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* CSV headers |
|
64
|
|
|
* |
|
65
|
|
|
* @var string[] |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $header = []; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set LimitIterator Offset |
|
71
|
|
|
* |
|
72
|
|
|
* @param $offset |
|
73
|
|
|
* |
|
74
|
|
|
* @return self |
|
75
|
|
|
*/ |
|
76
|
12 |
|
public function offset(int $offset = 0): self |
|
77
|
|
|
{ |
|
78
|
12 |
|
$offset = $this->filterInteger($offset, 0, 'the offset must be a positive integer or 0'); |
|
79
|
12 |
|
if ($offset === $this->offset) { |
|
80
|
2 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
10 |
|
$clone = clone $this; |
|
84
|
10 |
|
$clone->offset = $offset; |
|
85
|
|
|
|
|
86
|
10 |
|
return $clone; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Set LimitIterator Count |
|
91
|
|
|
* |
|
92
|
|
|
* @param int $limit |
|
93
|
|
|
* |
|
94
|
|
|
* @return self |
|
95
|
|
|
*/ |
|
96
|
16 |
|
public function limit(int $limit = -1): self |
|
97
|
|
|
{ |
|
98
|
16 |
|
$limit = $this->filterInteger($limit, -1, 'the limit must an integer greater or equals to -1'); |
|
99
|
14 |
|
if ($limit === $this->limit) { |
|
100
|
2 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
12 |
|
$clone = clone $this; |
|
104
|
12 |
|
$clone->limit = $limit; |
|
105
|
|
|
|
|
106
|
12 |
|
return $clone; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Set an Iterator sorting callable function |
|
111
|
|
|
* |
|
112
|
|
|
* @param callable $callable |
|
113
|
|
|
* |
|
114
|
|
|
* @return self |
|
115
|
|
|
*/ |
|
116
|
2 |
|
public function orderBy(callable $callable): self |
|
117
|
|
|
{ |
|
118
|
2 |
|
$clone = clone $this; |
|
119
|
2 |
|
$clone->order_by[] = $callable; |
|
120
|
|
|
|
|
121
|
2 |
|
return $clone; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Set the Iterator filter method |
|
126
|
|
|
* |
|
127
|
|
|
* @param callable $callable |
|
128
|
|
|
* |
|
129
|
|
|
* @return self |
|
130
|
|
|
*/ |
|
131
|
2 |
|
public function where(callable $callable): self |
|
132
|
|
|
{ |
|
133
|
2 |
|
$clone = clone $this; |
|
134
|
2 |
|
$clone->where[] = $callable; |
|
135
|
|
|
|
|
136
|
2 |
|
return $clone; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Set the headers to be used by the RecordSet object |
|
141
|
|
|
* |
|
142
|
|
|
* @param string[] $header |
|
143
|
|
|
* |
|
144
|
|
|
* @return self |
|
145
|
|
|
*/ |
|
146
|
18 |
|
public function header(array $header): self |
|
147
|
|
|
{ |
|
148
|
18 |
|
$header = $this->filterHeader($header); |
|
149
|
16 |
|
if ($header === $this->header) { |
|
150
|
2 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
14 |
|
$clone = clone $this; |
|
154
|
14 |
|
$clone->header = $header; |
|
155
|
|
|
|
|
156
|
14 |
|
return $clone; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Returns the inner CSV Document Iterator object |
|
161
|
|
|
* |
|
162
|
|
|
* @return RecordSet |
|
163
|
|
|
*/ |
|
164
|
108 |
|
public function process(Reader $reader): RecordSet |
|
165
|
|
|
{ |
|
166
|
108 |
|
$header = $this->header; |
|
167
|
108 |
|
if (empty($header)) { |
|
168
|
94 |
|
$header = $reader->getHeader(); |
|
169
|
|
|
} |
|
170
|
106 |
|
$iterator = $this->combineHeader($reader->getIterator()); |
|
|
|
|
|
|
171
|
106 |
|
$iterator = $this->filterRecords($iterator); |
|
172
|
106 |
|
$iterator = $this->orderRecords($iterator); |
|
173
|
|
|
|
|
174
|
106 |
|
return new RecordSet(new LimitIterator($iterator, $this->offset, $this->limit), $header); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Add the CSV header if present |
|
179
|
|
|
* |
|
180
|
|
|
* @param Iterator $iterator |
|
181
|
|
|
* |
|
182
|
|
|
* @return Iterator |
|
183
|
|
|
*/ |
|
184
|
106 |
|
protected function combineHeader(Iterator $iterator): Iterator |
|
185
|
|
|
{ |
|
186
|
106 |
|
if (empty($this->header)) { |
|
187
|
92 |
|
return $iterator; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
14 |
|
$header_count = count($this->header); |
|
191
|
|
|
$combine = function (array $row) use ($header_count) { |
|
192
|
10 |
|
if ($header_count != count($row)) { |
|
193
|
4 |
|
$row = array_slice(array_pad($row, $header_count, null), 0, $header_count); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
10 |
|
return array_combine($this->header, $row); |
|
197
|
14 |
|
}; |
|
198
|
|
|
|
|
199
|
14 |
|
return new MapIterator($iterator, $combine); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Filter the Iterator |
|
204
|
|
|
* |
|
205
|
|
|
* @param Iterator $iterator |
|
206
|
|
|
* |
|
207
|
|
|
* @return Iterator |
|
208
|
|
|
*/ |
|
209
|
106 |
|
protected function filterRecords(Iterator $iterator): Iterator |
|
210
|
|
|
{ |
|
211
|
|
|
$reducer = function ($iterator, $callable) { |
|
212
|
2 |
|
return new CallbackFilterIterator($iterator, $callable); |
|
213
|
106 |
|
}; |
|
214
|
|
|
|
|
215
|
106 |
|
return array_reduce($this->where, $reducer, $iterator); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Sort the Iterator |
|
220
|
|
|
* |
|
221
|
|
|
* @param Iterator $iterator |
|
222
|
|
|
* |
|
223
|
|
|
* @return Iterator |
|
224
|
|
|
*/ |
|
225
|
106 |
|
protected function orderRecords(Iterator $iterator): Iterator |
|
226
|
|
|
{ |
|
227
|
106 |
|
if (empty($this->order_by)) { |
|
228
|
104 |
|
return $iterator; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
2 |
|
$obj = new ArrayIterator(iterator_to_array($iterator)); |
|
232
|
2 |
|
$obj->uasort(function ($row_a, $row_b) { |
|
233
|
2 |
|
$res = 0; |
|
234
|
2 |
|
foreach ($this->order_by as $compare) { |
|
235
|
2 |
|
if (0 !== ($res = $compare($row_a, $row_b))) { |
|
236
|
2 |
|
break; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
2 |
|
return $res; |
|
241
|
2 |
|
}); |
|
242
|
|
|
|
|
243
|
2 |
|
return $obj; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.