-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spreadsheet.php
187 lines (155 loc) · 5.01 KB
/
Spreadsheet.php
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
<?php
declare(strict_types=1);
/*
* This file is part of the xezilaires project.
*
* (c) sigwin.hr
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Xezilaires\Bridge\Spout;
use OpenSpout\Common\Exception\IOException;
use OpenSpout\Common\Exception\UnsupportedTypeException;
use OpenSpout\Reader\Exception\ReaderNotOpenedException;
use OpenSpout\Reader\ReaderInterface;
use OpenSpout\Reader\SheetInterface;
use OpenSpout\Reader\XLSX\Reader;
use Xezilaires\Exception\SpreadsheetException;
use Xezilaires\Iterator;
use Xezilaires\Spreadsheet as SpreadsheetInterface;
final class Spreadsheet implements SpreadsheetInterface
{
/**
* @var array<int, string>
*/
private static array $indexCache = [];
private \SplFileObject $file;
/**
* @psalm-suppress PropertyNotSetInConstructor
*/
private ReaderInterface $reader;
/**
* @psalm-suppress PropertyNotSetInConstructor
*
* @psalm-var Iterator&RowIterator
*/
private Iterator $iterator;
public function __construct(\SplFileObject $file)
{
$this->file = $file;
}
public static function fromFile(\SplFileObject $file): SpreadsheetInterface
{
return new self($file);
}
/**
* {@inheritdoc}
*/
public function createIterator(int $startRowIndex): void
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
if (isset($this->iterator)) {
throw SpreadsheetException::iteratorAlreadyCreated();
}
$sheet = $this->getActiveWorksheet();
$iterator = $sheet->getRowIterator();
$this->iterator = new RowIterator($iterator, $startRowIndex);
}
public function getIterator(): Iterator
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
if (isset($this->iterator) === false) {
throw SpreadsheetException::noIterator();
}
return $this->iterator;
}
/**
* {@inheritdoc}
*/
public function getRow(int $rowIndex): array
{
$iterator = $this->getIterator();
$seekRow = $iterator->key();
$iterator->seek($rowIndex);
$row = $this->getCurrentRow();
$iterator->seek($seekRow);
return $row;
}
/**
* {@inheritdoc}
*/
public function getCurrentRow(): array
{
/** @var \ArrayObject $rowArrayObject */
$rowArrayObject = $this->getIterator()->current();
$row = [];
/**
* @var int $columnIndex
* @var null|float|int|string $columnValue
*/
foreach ($rowArrayObject as $columnIndex => $columnValue) {
$columnName = self::stringFromColumnIndex($columnIndex + 1);
$row[$columnName] = $columnValue !== '' ? $columnValue : null;
}
return $row;
}
/**
* {@inheritdoc}
*/
public function getHighestRow(): int
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
if (isset($this->iterator) === false) {
throw SpreadsheetException::noIterator();
}
return $this->iterator->getHighestRow();
}
/**
* @author https://github.com/phpoffice/PhpSpreadsheet
*/
private static function stringFromColumnIndex(int $columnIndex): string
{
if (isset(self::$indexCache[$columnIndex]) === false) {
$indexValue = $columnIndex;
$base26 = '';
do {
$normalizedIndexValue = $indexValue % 26;
$characterValue = $normalizedIndexValue > 0 ? $normalizedIndexValue : 26;
$indexValue = ($indexValue - $characterValue) / 26;
$base26 = \chr($characterValue + 64).$base26;
} while ($indexValue > 0);
self::$indexCache[$columnIndex] = $base26;
}
return self::$indexCache[$columnIndex];
}
private function getReader(): ReaderInterface
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
if (isset($this->reader) === false) {
$path = $this->file->getRealPath();
if ($path === false) {
throw SpreadsheetException::noSpreadsheetFound();
}
try {
$this->reader = new Reader();
$this->reader->open($path);
} catch (UnsupportedTypeException|IOException $exception) {
throw SpreadsheetException::invalidSpreadsheet($exception);
}
}
return $this->reader;
}
private function getActiveWorksheet(): SheetInterface
{
try {
/** @var SheetInterface $sheet */
foreach ($this->getReader()->getSheetIterator() as $sheet) {
return $sheet;
}
} catch (ReaderNotOpenedException $exception) {
throw SpreadsheetException::failedFetchingActiveWorksheet($exception);
}
throw SpreadsheetException::failedFetchingActiveWorksheet();
}
}