-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTabularDataStream.php
More file actions
83 lines (72 loc) · 2.32 KB
/
TabularDataStream.php
File metadata and controls
83 lines (72 loc) · 2.32 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
<?php
namespace frictionlessdata\datapackage\DataStreams;
use frictionlessdata\datapackage\Exceptions\DataStreamValidationException;
use frictionlessdata\datapackage\Exceptions\DataStreamOpenException;
use frictionlessdata\tableschema\DataSources\CsvDataSource;
use frictionlessdata\tableschema\Schema;
use frictionlessdata\tableschema\Table;
use frictionlessdata\tableschema\Exceptions\FieldValidationException;
use frictionlessdata\tableschema\Exceptions\DataSourceException;
class TabularDataStream extends BaseDataStream
{
public $table;
public $schema;
/**
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamOpenException
* @throws \Exception
*/
public function __construct($dataSource, $dataSourceOptions = null)
{
parent::__construct($dataSource, $dataSourceOptions);
$this->dataSourceOptions = (object) $this->dataSourceOptions;
$schema = $this->dataSourceOptions->schema;
$dialect = $this->dataSourceOptions->dialect;
if (empty($schema)) {
throw new \Exception('schema is required for tabular data stream');
} else {
try {
$this->schema = new Schema($schema);
$this->table = new Table($this->getDataSourceObject(), $this->schema, $dialect);
} catch (\Exception $e) {
throw new DataStreamOpenException('Failed to open tabular data source '.json_encode($dataSource).': '.json_encode($e->getMessage()));
}
}
}
protected function getDataSourceObject()
{
return new CsvDataSource($this->dataSource);
}
public function rewind():void
{
$this->table->rewind();
}
public function save($filename)
{
$this->table->save($filename);
}
/**
* @return array
*
* @throws DataStreamValidationException
*/
public function current():mixed
{
try {
return $this->table->current();
} catch (DataSourceException|FieldValidationException $e) {
throw new DataStreamValidationException($e->getMessage());
}
}
public function key():mixed
{
return $this->table->key();
}
public function next():void
{
$this->table->next();
}
public function valid(): bool
{
return $this->table->valid();
}
}