1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* CSVelte: Slender, elegant CSV for PHP |
5
|
|
|
* Inspired by Python's CSV module and Frictionless Data and the W3C's CSV |
6
|
|
|
* standardization efforts, CSVelte was written in an effort to take all the |
7
|
|
|
* suck out of working with CSV. |
8
|
|
|
* |
9
|
|
|
* @version {version} |
10
|
|
|
* @copyright Copyright (c) 2016 Luke Visinoni <[email protected]> |
11
|
|
|
* @author Luke Visinoni <[email protected]> |
12
|
|
|
* @license https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT) |
13
|
|
|
*/ |
14
|
|
|
namespace CSVelte\Reader; |
15
|
|
|
|
16
|
|
|
use CSVelte\Reader as CsvReader; |
17
|
|
|
use FilterIterator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Filtered Reader Iterator. |
21
|
|
|
* |
22
|
|
|
* This class is not intended to be instantiated manually. It is returned by the |
23
|
|
|
* CSVelte\Reader class when filter() is called to iterate over the CSV file, |
24
|
|
|
* skipping all rows that don't pass the filter(s) tests. |
25
|
|
|
* |
26
|
|
|
* @package CSVelte |
27
|
|
|
* @subpackage Reader |
28
|
|
|
* |
29
|
|
|
* @since v0.1 |
30
|
|
|
* |
31
|
|
|
* @internal |
32
|
|
|
*/ |
33
|
|
|
class FilteredIterator extends FilterIterator |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* A list of callback functions. |
37
|
|
|
* |
38
|
|
|
* @var array of Callable objects/functions |
39
|
|
|
*/ |
40
|
|
|
protected $filters = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* FilteredIterator Constructor. |
44
|
|
|
* |
45
|
|
|
* Initializes the iterator using the CSV reader and its array of callback |
46
|
|
|
* filter functions/callables. |
47
|
|
|
* |
48
|
|
|
* @param \CSVelte\Reader $reader The CSV reader being iterated |
49
|
|
|
* @param array $filters The list of callbacks |
50
|
|
|
*/ |
51
|
3 |
|
public function __construct(CsvReader $reader, array $filters) |
52
|
|
|
{ |
53
|
3 |
|
$this->filters = $filters; |
54
|
3 |
|
parent::__construct($reader); |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Run filters against each row. |
59
|
|
|
* Loop through all of the callback functions, and if any of them fail, do |
60
|
|
|
* not include this row in the iteration. |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
* |
64
|
|
|
* @todo filter functions should accept current row, index, AND ref to reader |
65
|
|
|
*/ |
66
|
3 |
|
public function accept() |
67
|
|
|
{ |
68
|
3 |
|
$reader = $this->getInnerIterator(); |
69
|
3 |
|
foreach ($this->filters as $filter) { |
70
|
3 |
|
if (!$filter($reader->current())) { |
71
|
3 |
|
return false; |
72
|
|
|
} |
73
|
3 |
|
} |
74
|
|
|
|
75
|
3 |
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return this object as an array. |
80
|
|
|
* |
81
|
|
|
* @return array This object as an array |
82
|
|
|
*/ |
83
|
|
|
public function toArray() |
84
|
|
|
{ |
85
|
1 |
|
return array_map(function ($row) { |
86
|
1 |
|
return $row->toArray(); |
87
|
1 |
|
}, iterator_to_array($this)); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|