PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.2.2
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.2.2
2.4.0 2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 All 67 releases
better-payment / vendor / league / csv / src / Statement.php

Statement.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.2.2, at vendor/league/csv/src/Statement.php

167 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * League.Csv (https://csv.thephpleague.com)
5 *
6 * (c) Ignace Nyamagana Butera <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 declare(strict_types=1);
13
14 namespace League\Csv;
15
16 use ArrayIterator;
17 use CallbackFilterIterator;
18 use Iterator;
19 use LimitIterator;
20 use function array_reduce;
21
22 /**
23 * Criteria to filter a {@link Reader} object.
24 */
25 class Statement
26 {
27 /** @var array<callable> Callables to filter the iterator. */
28 protected array $where = [];
29 /** @var array<callable> Callables to sort the iterator. */
30 protected array $order_by = [];
31 /** iterator Offset. */
32 protected int $offset = 0;
33 /** iterator maximum length. */
34 protected int $limit = -1;
35
36 /**
37 * @throws Exception
38 */
39 public static function create(callable $where = null, int $offset = 0, int $limit = -1): self
40 {
41 $stmt = new self();
42 if (null !== $where) {
43 $stmt = $stmt->where($where);
44 }
45
46 return $stmt->offset($offset)->limit($limit);
47 }
48
49 /**
50 * Set the Iterator filter method.
51 */
52 public function where(callable $where): self
53 {
54 $clone = clone $this;
55 $clone->where[] = $where;
56
57 return $clone;
58 }
59
60 /**
61 * Set an Iterator sorting callable function.
62 */
63 public function orderBy(callable $order_by): self
64 {
65 $clone = clone $this;
66 $clone->order_by[] = $order_by;
67
68 return $clone;
69 }
70
71 /**
72 * Set LimitIterator Offset.
73 *
74 * @throws Exception if the offset is lesser than 0
75 */
76 public function offset(int $offset): self
77 {
78 if (0 > $offset) {
79 throw InvalidArgument::dueToInvalidRecordOffset($offset, __METHOD__);
80 }
81
82 if ($offset === $this->offset) {
83 return $this;
84 }
85
86 $clone = clone $this;
87 $clone->offset = $offset;
88
89 return $clone;
90 }
91
92 /**
93 * Set LimitIterator Count.
94 *
95 * @throws Exception if the limit is lesser than -1
96 */
97 public function limit(int $limit): self
98 {
99 if (-1 > $limit) {
100 throw InvalidArgument::dueToInvalidLimit($limit, __METHOD__);
101 }
102
103 if ($limit === $this->limit) {
104 return $this;
105 }
106
107 $clone = clone $this;
108 $clone->limit = $limit;
109
110 return $clone;
111 }
112
113 /**
114 * Execute the prepared Statement on the {@link Reader} object.
115 *
116 * @param array<string> $header an optional header to use instead of the CSV document header
117 */
118 public function process(TabularDataReader $tabular_data, array $header = []): TabularDataReader
119 {
120 if ([] === $header) {
121 $header = $tabular_data->getHeader();
122 }
123
124 $iterator = $tabular_data->getRecords($header);
125 $iterator = array_reduce($this->where, [$this, 'filter'], $iterator);
126 $iterator = $this->buildOrderBy($iterator);
127
128 return new ResultSet(new LimitIterator($iterator, $this->offset, $this->limit), $header);
129 }
130
131 /**
132 * Filters elements of an Iterator using a callback function.
133 */
134 protected function filter(Iterator $iterator, callable $callable): CallbackFilterIterator
135 {
136 return new CallbackFilterIterator($iterator, $callable);
137 }
138
139 /**
140 * Sort the Iterator.
141 */
142 protected function buildOrderBy(Iterator $iterator): Iterator
143 {
144 if ([] === $this->order_by) {
145 return $iterator;
146 }
147
148 $compare = function (array $record_a, array $record_b): int {
149 foreach ($this->order_by as $callable) {
150 if (0 !== ($cmp = $callable($record_a, $record_b))) {
151 return $cmp;
152 }
153 }
154
155 return $cmp ?? 0;
156 };
157
158 $it = new ArrayIterator();
159 foreach ($iterator as $offset => $value) {
160 $it[$offset] = $value;
161 }
162 $it->uasort($compare);
163
164 return $it;
165 }
166 }
167