-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTable.php
More file actions
277 lines (236 loc) · 6.77 KB
/
Table.php
File metadata and controls
277 lines (236 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
declare(strict_types=1);
namespace HiFolks\DataType;
use Countable;
use Iterator;
/**
* @implements Iterator<int|string, Arr>
*/
final class Table implements Countable, Iterator
{
/**
* @var array<int|string, Arr> $rows
*/
private array $rows = [];
/**
* @param array<int|string, array<int|string, mixed>|Arr> $array
*/
public static function make(array $array): self
{
$table = new self();
foreach ($array as $value) {
$table->append($value);
}
return $table;
}
/**
* @return Arr[]
*/
public function rows(): array
{
return $this->rows;
}
/**
* @return array<int|string, array<int|string, mixed>>
*/
public function toArray(): array
{
$result = [];
foreach ($this->rows as $key => $row) {
$result[$key] = $row->arr();
}
return $result;
}
/**
* @param array<int|string, mixed>|Arr $arr
* @return $this
*/
public function append(array|Arr $arr): self
{
$this->rows[] = $this->getArr($arr);
return $this;
}
public function first(): ?Arr
{
$array = array_reverse($this->rows);
return array_pop($array);
}
public function last(): ?Arr
{
$reference = $this->rows;
return array_pop($reference);
}
public function getFromFirst(int|string $field): mixed
{
return $this->first()?->get($field);
}
public function getFromLast(int|string $field): mixed
{
return $this->last()?->get($field);
}
public function select(int|string ...$columns): self
{
$table = self::make([]);
foreach ($this->rows as $row) {
$newRow = [];
foreach ($columns as $column) {
$value = $row->get($column);
$newRow[$column] = $value;
}
$table->append(Arr::make($newRow));
}
return $table;
}
/**
* It returns a new Table instance with data, excluding the attributes listed in
* $columns
*/
public function except(int|string ...$columns): self
{
$table = self::make([]);
foreach ($this->rows as $row) {
$newRow = $row;
foreach ($columns as $column) {
$newRow->unset($column);
}
$table->append($newRow);
}
return $table;
}
public function where(string|int $field, mixed $operator = null, mixed $value = null): self
{
if (func_num_args() === 1) {
$value = true;
$operator = '==';
}
if (func_num_args() === 2) {
$value = $operator;
$operator = '===';
}
$function = match ($operator) {
'==' => fn ($element): bool => $element->get($field) == $value,
'>' => fn ($element): bool => $element->get($field) > $value,
'<' => fn ($element): bool => $element->get($field) < $value,
'>=' => fn ($element): bool => $element->get($field) >= $value,
'<=' => fn ($element): bool => $element->get($field) <= $value,
'!=' => fn ($element): bool => $element->get($field) != $value,
'!==' => fn ($element): bool => $element->get($field) !== $value,
default => fn ($element): bool => $element->get($field) === $value
};
$filteredArray = array_filter($this->rows, $function);
return self::make($filteredArray);
}
public function orderBy(string|int $field, string $order = 'desc'): self
{
$array = $this->rows;
if ($order !== 'asc') {
$closure = static fn (Arr $item1, Arr $item2): int => $item2->get($field) <=> $item1->get($field);
} else {
$closure = static fn ($item1, $item2): int => $item1->get($field) <=> $item2->get($field);
}
usort($array, $closure);
return self::make($array);
}
/**
* @return $this
*/
public function calc(string|int $destinationField, callable $function): self
{
foreach ($this->rows as $key => $value) {
$this->rows[$key][$destinationField] = $function($value);
}
return $this;
}
/**
* This will only return the values for the first time it a unique row within that group
*/
public function groupBy(string|int $field): Table
{
$result = [];
foreach ($this->rows as $value) {
$property = $value->get($field);
$property = $this->castVariableForStrval($property);
if ($property === null) {
continue;
}
if ($property === false) {
continue;
}
$key = is_bool($property) || is_float($property) ? strval($property) : $property;
if (array_key_exists(strval($key), $result)) {
continue;
}
$result[$key] = $value;
}
return self::make(array_values($result));
}
/**
* strval sometimes crashes when passed a mixed value, this fixes that problem.
*/
private function castVariableForStrval(mixed $property): bool|float|int|string|null
{
return match(gettype($property)) {
'boolean' => $property,
'double' => $property,
'integer' => $property,
'string' => $property,
default => null,
};
}
/**
* Transform allows you to run a function over an entire table on a specific row
*/
public function transform(
string|int $field,
callable $function,
): self {
$array = $this->rows;
foreach ($array as $row) {
/** @var array<int, mixed> $keys */
$keys = $row->keys();
if (in_array($field, $keys, true)) {
$result = $function($row->get($field));
$row->set($field, $result);
}
}
return self::make($array);
}
public function count(): int
{
return count($this->rows);
}
/**
* @param array<int|string, mixed>|Arr $value
*/
private function getArr(array|Arr $value): Arr
{
if (!$value instanceof Arr) {
return Arr::make($value);
}
return $value;
}
public function current(): Arr|false
{
return current($this->rows);
}
public function next(): void
{
next($this->rows);
}
public function prev(): void
{
prev($this->rows);
}
public function key(): string|int|null
{
return key($this->rows);
}
public function valid(): bool
{
return ! is_null($this->key());
}
public function rewind(): void
{
reset($this->rows);
}
}