-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathTableReader.php
More file actions
390 lines (322 loc) · 9.43 KB
/
TableReader.php
File metadata and controls
390 lines (322 loc) · 9.43 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php declare(strict_types=1);
namespace XBase;
use XBase\Column\ColumnInterface;
use XBase\Column\XBaseColumn;
use XBase\DataConverter\Encoder\EncoderInterface;
use XBase\DataConverter\Encoder\IconvEncoder;
use XBase\Enum\Codepage;
use XBase\Enum\TableType;
use XBase\Exception\TableException;
use XBase\Header\Header;
use XBase\Header\Reader\HeaderReaderFactory;
use XBase\Memo\MemoFactory;
use XBase\Memo\MemoInterface;
use XBase\Record\RecordFactory;
use XBase\Record\RecordInterface;
use XBase\Stream\Stream;
use XBase\Table\Table;
use XBase\Table\TableAwareTrait;
/**
* @author Alexander Strizhak <gam6itko@gmail.com>
*/
class TableReader
{
use TableAwareTrait;
/** @var int Current record position. */
protected $recordPos = -1;
/**
* @var int
* @deprecated
* @todo remove in next version
*/
protected $deleteCount = 0;
/**
* @var RecordInterface|null
* @todo unnecessary. get rid of this in next version
*/
protected $record;
/** @var EncoderInterface */
protected $encoder;
/**
* @var Table
*/
protected $table;
/**
* Table constructor.
*
* @param array $options Array of options:<br>
* encoding - convert text data from<br>
* columns - available columns<br>
* encoder - encoder class name, default: IconvEncoder::class<br>
*
* @throws \Exception
*/
public function __construct(string $filepath, array $options = [])
{
$this->table = new Table();
$this->table->filepath = $filepath;
$this->encoder = isset($options['encoder']) && $options['encoder'] instanceof EncoderInterface ?
$options['encoder'] :
new IconvEncoder();
$this->table->options = $this->resolveOptions($options);
$this->open();
$this->readHeader();
$this->openMemo();
}
protected function resolveOptions(array $options): array
{
return array_merge([
'columns' => [],
'encoding' => null,
'editMode' => null,
'ignoreDeleted' => true,
], $options);
}
protected function open(): void
{
if (!file_exists($this->getFilepath())) {
throw new \Exception(sprintf('File %s cannot be found', $this->getFilepath()));
}
if ($this->table->stream) {
$this->table->stream->close();
}
$this->table->stream = Stream::createFromFile($this->getFilepath());
}
protected function readHeader(): void
{
$this->table->header = HeaderReaderFactory::create($this->getFilepath(), $this->table->options)->read();
$this->getStream()->seek($this->table->header->length);
$this->recordPos = -1;
$this->deleteCount = 0;
}
protected function openMemo(): void
{
if (TableType::hasMemo($this->getVersion())) {
$this->table->memo = MemoFactory::create($this->table, $this->encoder);
}
}
protected function getHeader(): Header
{
return $this->table->header;
}
protected function getMemo(): ?MemoInterface
{
return $this->table->memo;
}
public function close(): void
{
$this->getStream()->close();
if ($memo = $this->getMemo()) {
$memo->close();
}
}
public function nextRecord(): ?RecordInterface
{
if (!$this->isOpen()) {
$this->open();
}
if ($this->record) {
$this->record->destroy();
$this->record = null;
}
$valid = false;
do {
if (($this->recordPos + 1) >= $this->getHeader()->recordCount) {
return null;
}
$this->recordPos++;
$this->record = RecordFactory::create($this->table, $this->encoder, $this->recordPos, $this->getStream()
->read($this->getHeader()->recordByteLength));
if ($this->record->isDeleted()) {
$this->deleteCount++;
if($this->table->options['ignoreDeleted'] === false) {
$valid = true;
}
} else {
$valid = true;
}
} while (!$valid);
return $this->record;
}
/**
* Get record by row index.
*
* @param int $position Zero based position
*/
public function pickRecord(int $position): ?RecordInterface
{
if ($position >= $this->getHeader()->recordCount) {
throw new TableException("Row with index {$position} does not exists");
}
$curPos = $this->getStream()->tell();
$seekPos = $this->getHeader()->length + $position * $this->getHeader()->recordByteLength;
if (0 !== $this->getStream()->seek($seekPos)) {
throw new TableException("Failed to pick row at position {$position}");
}
$record = RecordFactory::create($this->table, $this->encoder, $position, $this->getStream()
->read($this->getHeader()->recordByteLength));
// revert pointer
$this->getStream()->seek($curPos);
return $record;
}
public function previousRecord(): ?RecordInterface
{
if (!$this->isOpen()) {
$this->open();
}
if ($this->record) {
$this->record->destroy();
$this->record = null;
}
$valid = false;
do {
if (($this->recordPos - 1) < 0) {
return null;
}
$this->recordPos--;
$this->getStream()->seek($this->getHeader()->length + ($this->recordPos * $this->getHeader()->recordByteLength));
$this->record = RecordFactory::create(
$this->table,
$this->encoder,
$this->recordPos,
$this->getStream()->read($this->getRecordByteLength())
);
if ($this->record->isDeleted()) {
$this->deleteCount++;
if($this->table->options['ignoreDeleted'] === false) {
$valid = true;
}
} else {
$valid = true;
}
} while (!$valid);
return $this->record;
}
public function moveTo(int $index): ?RecordInterface
{
$this->recordPos = $index;
if ($index < 0) {
return null;
}
$this->getStream()->seek($this->getHeader()->length + ($index * $this->getHeader()->recordByteLength));
$this->record = RecordFactory::create($this->table, $this->encoder, $this->recordPos, $this->getStream()
->read($this->getHeader()->recordByteLength));
return $this->record;
}
/**
* @return ColumnInterface
*/
public function getColumn(string $name)
{
foreach ($this->getHeader()->columns as $column) {
if ($column->name === $name) {
return new XBaseColumn($column);
}
}
throw new \Exception("Column $name not found");
}
public function getRecord(): ?RecordInterface
{
return $this->record;
}
public function getCodepage(): int
{
return $this->getHeader()->languageCode;
}
/**
* @return ColumnInterface[]
*/
public function getColumns(): array
{
$columns = [];
foreach ($this->getHeader()->columns as $column) {
assert(!empty($column->name));
$columns[$column->name] = new XBaseColumn($column);
}
return $columns;
}
public function getColumnCount(): int
{
return count($this->getHeader()->columns);
}
/**
* @return int
*/
public function getRecordCount()
{
return $this->getHeader()->recordCount;
}
/**
* @return int
*/
public function getRecordPos()
{
return $this->recordPos;
}
public function getRecordByteLength()
{
return $this->getHeader()->recordByteLength;
}
/**
* @return string
*/
public function getFilepath()
{
return $this->table->filepath;
}
public function getVersion(): int
{
return $this->getHeader()->version;
}
/**
* @see Codepage
*/
public function getLanguageCode(): int
{
return $this->getHeader()->languageCode;
}
/**
* @return int
* @deprecated this method is pointless
*/
public function getDeleteCount()
{
@trigger_error('Method `getDeleteCount` is useless and will be deleted soon. Do not use it!');
return $this->deleteCount;
}
public function getConvertFrom(): ?string
{
return $this->table->options['encoding'];
}
/**
* @return bool
*/
protected function isOpen()
{
return $this->getStream() ? true : false;
}
public function isFoxpro(): bool
{
return TableType::isFoxpro($this->getHeader()->version);
}
public function getModifyDate()
{
return $this->getHeader()->modifyDate;
}
public function isInTransaction(): bool
{
return $this->getHeader()->inTransaction;
}
public function isEncrypted(): bool
{
return $this->getHeader()->encrypted;
}
public function getMdxFlag(): string
{
return chr($this->getHeader()->mdxFlag);
}
public function getHeaderLength(): int
{
return $this->getHeader()->length;
}
}