1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Datapp\TableauExport; |
6
|
|
|
|
7
|
|
|
use Datapp\TableauExport\Sorter\SorterInterface; |
8
|
|
|
use Datapp\TableauExport\Sorter\NullSorter; |
9
|
|
|
use Datapp\TableauExport\CrossTableException; |
10
|
|
|
use Generator; |
11
|
|
|
|
12
|
|
|
class CrossTable |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
const MEASURE_NAMES = 'Measure Names'; |
16
|
|
|
const MEASURE_VALUES = 'Measure Values'; |
17
|
|
|
|
18
|
|
|
private array $cache = []; |
|
|
|
|
19
|
|
|
private SorterInterface $sorter; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param SorterInterface $sorter |
23
|
|
|
*/ |
24
|
5 |
|
public function __construct(SorterInterface $sorter = null) |
25
|
|
|
{ |
26
|
5 |
|
$this->sorter = $sorter ?? new NullSorter(); |
27
|
5 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $fileName |
31
|
|
|
* @return Generator<int, array <string, string>, mixed, void> |
32
|
|
|
* @throws CrossTableException |
33
|
|
|
*/ |
34
|
5 |
|
public function fromFile(string $fileName): Generator |
35
|
|
|
{ |
36
|
5 |
|
$lineGenerator = $this->readLineByLine($fileName); |
37
|
5 |
|
if (!$lineGenerator->valid()) { |
38
|
|
|
throw CrossTableException::errorParsingCsvLine(); |
39
|
|
|
} |
40
|
|
|
/** @var array <int, string> */ |
41
|
4 |
|
$headers = $lineGenerator->current(); |
42
|
4 |
|
$idColums = $this->getIdColums($headers, [self::MEASURE_NAMES, self::MEASURE_VALUES]); |
43
|
4 |
|
$lineGenerator->next(); |
44
|
|
|
|
45
|
4 |
|
while ($lineGenerator->valid()) { |
46
|
|
|
/** @var array <int, string> */ |
47
|
4 |
|
$line = $lineGenerator->current(); |
48
|
4 |
|
$data = array_combine($headers, $line); |
49
|
4 |
|
if ($data === false) { |
50
|
1 |
|
throw CrossTableException::errorParsingCsvLine(); |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
if (!$this->hasSameValuesInIdColumsAsRowBefore($data, $idColums, $this->cache)) { |
54
|
3 |
|
if (count($this->cache) > 0) { |
55
|
3 |
|
yield $this->sorter->sort($this->cache); |
56
|
|
|
} |
57
|
3 |
|
$this->cache = $data; |
58
|
3 |
|
unset($this->cache[self::MEASURE_NAMES]); |
59
|
3 |
|
unset($this->cache[self::MEASURE_VALUES]); |
60
|
|
|
} |
61
|
3 |
|
$this->cache[(string) $data[self::MEASURE_NAMES]] = (string) $data[self::MEASURE_VALUES]; |
62
|
3 |
|
$lineGenerator->next(); |
63
|
|
|
} |
64
|
|
|
// don't forget last line |
65
|
3 |
|
yield $this->sorter->sort($this->cache); |
66
|
3 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $fileName |
70
|
|
|
* @return \Generator<int, array <int, string>, mixed, void> |
71
|
|
|
* @throws CrossTableException |
72
|
|
|
*/ |
73
|
5 |
|
private function readLineByLine(string $fileName): \Generator |
74
|
|
|
{ |
75
|
5 |
|
if (!is_readable($fileName)) { |
76
|
1 |
|
throw CrossTableException::fileNotFoundOrNotReadable($fileName); |
77
|
|
|
} |
78
|
4 |
|
$fileHandle = fopen($fileName, 'r'); |
79
|
4 |
|
if ($fileHandle === false) { |
80
|
|
|
// @codeCoverageIgnoreStart |
81
|
|
|
throw CrossTableException::fileNotFoundOrNotReadable($fileName); |
82
|
|
|
// @codeCoverageIgnoreEnd |
83
|
|
|
} |
84
|
4 |
|
while ($row = fgetcsv($fileHandle, 0, ';')) { |
85
|
4 |
|
yield $row; |
86
|
|
|
} |
87
|
3 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array<int, string> $headers |
91
|
|
|
* @param array<int, string> $measureColums |
92
|
|
|
* @return array<int, string> |
93
|
|
|
* @throws CrossTableException |
94
|
|
|
*/ |
95
|
4 |
|
private function getIdColums(array $headers, array $measureColums): array |
96
|
|
|
{ |
97
|
4 |
|
$idColums = array_diff($headers, $measureColums); |
98
|
4 |
|
if (count($idColums) + 2 !== count($headers)) { |
99
|
|
|
throw CrossTableException::errorParsingCsvLine(); |
100
|
|
|
} |
101
|
4 |
|
return $idColums; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array<string, string> $row |
106
|
|
|
* @param array<int, string> $idCols |
107
|
|
|
* @param array<string, string> $cache |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
3 |
|
private function hasSameValuesInIdColumsAsRowBefore(array $row, array $idCols, array $cache): bool |
111
|
|
|
{ |
112
|
|
|
// first line? |
113
|
3 |
|
if (count($cache) === 0) { |
114
|
3 |
|
return false; |
115
|
|
|
} |
116
|
|
|
// compare values in the id columns |
117
|
|
|
/** @var string $id */ |
118
|
3 |
|
foreach ($idCols as $id) { |
119
|
3 |
|
if ($cache[$id] !== $row[$id]) { |
120
|
3 |
|
return false; |
121
|
|
|
} |
122
|
|
|
} |
123
|
3 |
|
return true; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|