Completed
Pull Request — master (#210)
by ignace nyamagana
02:52
created

Statement::buildOrderBy()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 2
nop 1
crap 4
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
     * CSV columns name
36
     *
37
     * @var array
38
     */
39
    protected $columns = [];
40
41
    /**
42
     * Callables to filter the iterator
43
     *
44
     * @var callable[]
45
     */
46
    protected $where = [];
47
48
    /**
49
     * Callables to sort the iterator
50
     *
51
     * @var callable[]
52
     */
53
    protected $order_by = [];
54
55
    /**
56
     * iterator Offset
57
     *
58
     * @var int
59
     */
60
    protected $offset = 0;
61
62
    /**
63
     * iterator maximum length
64
     *
65
     * @var int
66
     */
67
    protected $limit = -1;
68
69
    /**
70
     * Set and select the column to be used by the RecordSet object
71
     *
72
     * @param array $columns
73
     *
74
     * @return self
75
     */
76 18
    public function columns(array $columns): self
77
    {
78 18
        $columns = $this->filterColumnNames($columns);
79 16
        if ($columns === $this->columns) {
80 2
            return $this;
81
        }
82
83 14
        $clone = clone $this;
84 14
        $clone->columns = $columns;
85
86 14
        return $clone;
87
    }
88
89
    /**
90
     * Set the Iterator filter method
91
     *
92
     * @param callable $callable
93
     *
94
     * @return self
95
     */
96 2
    public function where(callable $callable): self
97
    {
98 2
        $clone = clone $this;
99 2
        $clone->where[] = $callable;
100
101 2
        return $clone;
102
    }
103
104
    /**
105
     * Set an Iterator sorting callable function
106
     *
107
     * @param callable $callable
108
     *
109
     * @return self
110
     */
111 2
    public function orderBy(callable $callable): self
112
    {
113 2
        $clone = clone $this;
114 2
        $clone->order_by[] = $callable;
115
116 2
        return $clone;
117
    }
118
119
    /**
120
     * Set LimitIterator Offset
121
     *
122
     * @param $offset
123
     *
124
     * @return self
125
     */
126 12
    public function offset(int $offset = 0): self
127
    {
128 12
        $offset = $this->filterInteger($offset, 0, 'the offset must be a positive integer or 0');
129 12
        if ($offset === $this->offset) {
130 2
            return $this;
131
        }
132
133 10
        $clone = clone $this;
134 10
        $clone->offset = $offset;
135
136 10
        return $clone;
137
    }
138
139
    /**
140
     * Set LimitIterator Count
141
     *
142
     * @param int $limit
143
     *
144
     * @return self
145
     */
146 16
    public function limit(int $limit = -1): self
147
    {
148 16
        $limit = $this->filterInteger($limit, -1, 'the limit must an integer greater or equals to -1');
149 14
        if ($limit === $this->limit) {
150 2
            return $this;
151
        }
152
153 12
        $clone = clone $this;
154 12
        $clone->limit = $limit;
155
156 12
        return $clone;
157
    }
158
159
    /**
160
     * Returns the inner CSV Document Iterator object
161
     *
162
     * @param Reader $reader
163
     *
164
     * @return RecordSet
165
     */
166 110
    public function process(Reader $reader): RecordSet
167
    {
168 110
        list($columns, $iterator) = $this->buildColumns($reader);
169 108
        $iterator = $this->buildWhere($iterator);
170 108
        $iterator = $this->buildOrderBy($iterator);
171
172 108
        return new RecordSet(new LimitIterator($iterator, $this->offset, $this->limit), $columns);
173
    }
174
175
    /**
176
     * Add the CSV column if present
177
     *
178
     * @param Reader $reader
179
     *
180
     * @return array
181
     */
182 110
    protected function buildColumns(Reader $reader): array
183
    {
184 110
        $header = $reader->getHeader();
185 108
        $iterator = $reader->getIterator();
186 108
        if (empty($this->columns)) {
187 94
            return [$header, $iterator];
188
        }
189
190 14
        $columns = $this->filterColumnAgainstCsvHeader($header);
191
        $combine = function (array $row) use ($columns): array {
192 10
            $record = [];
193 10
            foreach ($columns as $key => $alias) {
194 10
                $record[$alias] = $row[$key] ?? null;
195
            }
196
197 10
            return $record;
198 14
        };
199
200 14
        return [array_values($columns), new MapIterator($iterator, $combine)];
0 ignored issues
show
Compatibility introduced by
$iterator of type object<Traversable> is not a sub-type of object<Iterator>. It seems like you assume a child interface of the interface Traversable to be always present.

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.

Loading history...
201
    }
202
203
    /**
204
     * Format the column array
205
     *
206
     * @param array $columns
207
     *
208
     * @return array
209
     */
210 2
    private function formatColumns(array $columns): array
211
    {
212 2
        $res = [];
213 2
        foreach ($columns as $key => $alias) {
214 2
            $res[!is_string($key) ? $alias : $key] = $alias;
215
        }
216
217 2
        return $res;
218
    }
219
220
    /**
221
     * Validate the column against the processed CSV header
222
     *
223
     * @param string[] $headers Reader CSV header
224
     *
225
     * @throws Exception If a column is not found
226
     */
227 14
    protected function filterColumnAgainstCsvHeader(array $headers)
228
    {
229 14
        if (empty($headers)) {
230 12
            return $this->columns;
231
        }
232
233 2
        $columns = $this->formatColumns($this->columns);
234 2
        foreach ($columns as $key => $alias) {
235 2
            if (false === array_search($key, $headers, true)) {
236 2
                throw new Exception(sprintf('The following column `%s` does not exist in the CSV document', $key));
237
            }
238
        }
239
240 2
        return $columns;
241
    }
242
243
    /**
244
    * Filter the Iterator
245
    *
246
    * @param Iterator $iterator
247
    *
248
    * @return Iterator
249
    */
250 108
    protected function buildWhere(Iterator $iterator): Iterator
251
    {
252
        $reducer = function (Iterator $iterator, callable $callable): Iterator {
253 2
            return new CallbackFilterIterator($iterator, $callable);
254 108
        };
255
256 108
        return array_reduce($this->where, $reducer, $iterator);
257
    }
258
259
    /**
260
    * Sort the Iterator
261
    *
262
    * @param Iterator $iterator
263
    *
264
    * @return Iterator
265
    */
266 108
    protected function buildOrderBy(Iterator $iterator): Iterator
267
    {
268 108
        if (empty($this->order_by)) {
269 106
            return $iterator;
270
        }
271
272 2
        $obj = new ArrayIterator(iterator_to_array($iterator, true));
273 2
        $obj->uasort(function (array $record_a, array $record_b): int {
274 2
            $res = 0;
275 2
            foreach ($this->order_by as $compare) {
276 2
                if (0 !== ($res = $compare($record_a, $record_b))) {
277 2
                    break;
278
                }
279
            }
280
281 2
            return $res;
282 2
        });
283
284 2
        return $obj;
285
    }
286
}
287