|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace midorikocak\tabletools; |
|
6
|
|
|
|
|
7
|
|
|
use League\Csv\Exception; |
|
8
|
|
|
use League\Csv\Reader; |
|
9
|
|
|
use League\Csv\Statement; |
|
10
|
|
|
|
|
11
|
|
|
use function count; |
|
12
|
|
|
use function iterator_to_array; |
|
13
|
|
|
use function strpos; |
|
14
|
|
|
|
|
15
|
|
|
class CsvTable implements TableInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private Reader $csv; |
|
18
|
|
|
private Statement $statement; |
|
19
|
|
|
private array $columns = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @throws Exception |
|
23
|
|
|
*/ |
|
24
|
6 |
|
public function __construct(string $filename) |
|
25
|
|
|
{ |
|
26
|
6 |
|
$this->csv = Reader::createFromPath($filename, 'r'); |
|
27
|
6 |
|
$this->csv->setHeaderOffset(0); |
|
28
|
6 |
|
$this->statement = new Statement(); |
|
29
|
6 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $order |
|
33
|
|
|
* @return $this |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function sort(string $key, $order = 'ASC'): self |
|
36
|
|
|
{ |
|
37
|
|
|
$this->statement->orderBy(function ($a, $b) use ($order) { |
|
38
|
|
|
return ($order === 'ASC' ? 1 : -1) * ($a <=> $b); |
|
39
|
1 |
|
}); |
|
40
|
|
|
|
|
41
|
1 |
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public function columns($keys): self |
|
45
|
|
|
{ |
|
46
|
1 |
|
$this->columns = $keys; |
|
47
|
1 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function filter(string $key, $value): self |
|
51
|
|
|
{ |
|
52
|
|
|
$this->statement->where(function ($item) use ($key, $value) { |
|
53
|
|
|
return $item[$key] === $value; |
|
54
|
1 |
|
}); |
|
55
|
|
|
|
|
56
|
1 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function search(string $key, $value): self |
|
60
|
|
|
{ |
|
61
|
|
|
$this->statement->where(function ($item) use ($key, $value) { |
|
62
|
|
|
return strpos($item[$key], $value) !== false; |
|
63
|
1 |
|
}); |
|
64
|
|
|
|
|
65
|
1 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
public function paginate(int $page = 0, int $pageSize = 10): self |
|
69
|
|
|
{ |
|
70
|
1 |
|
$offset = $page * $pageSize; |
|
71
|
1 |
|
$this->range($offset, $pageSize); |
|
72
|
1 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
public function range(int $offset, ?int $limit = null): self |
|
76
|
|
|
{ |
|
77
|
2 |
|
if ($limit && $offset) { |
|
|
|
|
|
|
78
|
1 |
|
$this->statement->limit($limit); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
if ($limit && $offset) { |
|
|
|
|
|
|
82
|
1 |
|
$this->statement->offset($offset); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
6 |
|
public function run(): array |
|
89
|
|
|
{ |
|
90
|
6 |
|
$records = iterator_to_array($this->statement->process($this->csv)->getRecords()); |
|
91
|
6 |
|
if (!empty($this->columns)) { |
|
92
|
1 |
|
$result = []; |
|
93
|
|
|
|
|
94
|
1 |
|
$recordCount = count($records); |
|
95
|
|
|
|
|
96
|
1 |
|
for ($i = 1; $i <= $recordCount; $i++) { |
|
97
|
1 |
|
foreach ($this->columns as $jValue) { |
|
98
|
1 |
|
$key = $jValue; |
|
99
|
1 |
|
$result[$i][$key] = $records[$i][$key]; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
return $result; |
|
104
|
|
|
} |
|
105
|
5 |
|
return $records; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: