1 | <?php |
||
25 | class Statement |
||
26 | { |
||
27 | /** |
||
28 | * Callables to filter the iterator. |
||
29 | * |
||
30 | * @var callable[] |
||
31 | */ |
||
32 | protected $where = []; |
||
33 | |||
34 | /** |
||
35 | * Callables to sort the iterator. |
||
36 | * |
||
37 | * @var callable[] |
||
38 | */ |
||
39 | protected $order_by = []; |
||
40 | |||
41 | /** |
||
42 | * iterator Offset. |
||
43 | * |
||
44 | * @var int |
||
45 | */ |
||
46 | protected $offset = 0; |
||
47 | |||
48 | /** |
||
49 | * iterator maximum length. |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $limit = -1; |
||
54 | |||
55 | /** |
||
56 | * Named Constructor to ease Statement instantiation. |
||
57 | * |
||
58 | * @throws Exception |
||
59 | */ |
||
60 | 27 | public static function create(callable $where = null, int $offset = 0, int $limit = -1): self |
|
69 | |||
70 | /** |
||
71 | * Set the Iterator filter method. |
||
72 | */ |
||
73 | 3 | public function where(callable $where): self |
|
80 | |||
81 | /** |
||
82 | * Set an Iterator sorting callable function. |
||
83 | */ |
||
84 | 6 | public function orderBy(callable $order_by): self |
|
91 | |||
92 | /** |
||
93 | * Set LimitIterator Offset. |
||
94 | * |
||
95 | * @throws Exception if the offset is lesser than 0 |
||
96 | */ |
||
97 | 39 | public function offset(int $offset): self |
|
112 | |||
113 | /** |
||
114 | * Set LimitIterator Count. |
||
115 | * |
||
116 | * @throws Exception if the limit is lesser than -1 |
||
117 | */ |
||
118 | 39 | public function limit(int $limit): self |
|
133 | |||
134 | /** |
||
135 | * Execute the prepared Statement on the {@link Reader} object. |
||
136 | * |
||
137 | * @param string[] $header an optional header to use instead of the CSV document header |
||
138 | */ |
||
139 | 30 | public function process(TabularDataReader $tabular_data, array $header = []): TabularDataReader |
|
140 | { |
||
141 | 30 | if ([] === $header) { |
|
142 | 30 | $header = $tabular_data->getHeader(); |
|
143 | } |
||
144 | |||
145 | 30 | $iterator = $tabular_data->getRecords($header); |
|
146 | 30 | $iterator = array_reduce($this->where, [$this, 'filter'], $iterator); |
|
147 | 30 | $iterator = $this->buildOrderBy($iterator); |
|
148 | |||
149 | 30 | return new ResultSet(new LimitIterator($iterator, $this->offset, $this->limit), $header); |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * Filters elements of an Iterator using a callback function. |
||
154 | */ |
||
155 | 6 | protected function filter(Iterator $iterator, callable $callable): CallbackFilterIterator |
|
159 | |||
160 | /** |
||
161 | * Sort the Iterator. |
||
162 | */ |
||
163 | 21 | protected function buildOrderBy(Iterator $iterator): Iterator |
|
187 | } |
||
188 |