|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace frictionlessdata\datapackage\DataStreams; |
|
4
|
|
|
|
|
5
|
|
|
use frictionlessdata\datapackage\Exceptions\DataStreamValidationException; |
|
6
|
|
|
use frictionlessdata\datapackage\Exceptions\DataStreamOpenException; |
|
7
|
|
|
use frictionlessdata\tableschema\DataSources\CsvDataSource; |
|
8
|
|
|
use frictionlessdata\tableschema\Schema; |
|
9
|
|
|
use frictionlessdata\tableschema\Table; |
|
10
|
|
|
use frictionlessdata\tableschema\Exceptions\FieldValidationException; |
|
11
|
|
|
use frictionlessdata\tableschema\Exceptions\DataSourceException; |
|
12
|
|
|
|
|
13
|
|
|
class TabularDataStream extends BaseDataStream |
|
14
|
|
|
{ |
|
15
|
|
|
public $table; |
|
16
|
|
|
public $schema; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct($dataSource, $dataSourceOptions = null) |
|
19
|
|
|
{ |
|
20
|
|
|
parent::__construct($dataSource, $dataSourceOptions); |
|
21
|
|
|
$schema = $this->dataSourceOptions; |
|
22
|
|
|
if (empty($schema)) { |
|
23
|
|
|
throw new \Exception('schema is required for tabular data stream'); |
|
24
|
|
|
} else { |
|
25
|
|
|
try { |
|
26
|
|
|
$this->schema = new Schema($schema); |
|
27
|
|
|
$this->table = new Table($this->getDataSourceObject(), $this->schema); |
|
28
|
|
|
} catch (\Exception $e) { |
|
29
|
|
|
throw new DataStreamOpenException('Failed to open tabular data source '.json_encode($dataSource).': '.json_encode($e->getMessage())); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function getDataSourceObject() |
|
35
|
|
|
{ |
|
36
|
|
|
return new CsvDataSource($this->dataSource); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function rewind() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->table->rewind(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return array |
|
46
|
|
|
* |
|
47
|
|
|
* @throws DataStreamValidationException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function current() |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
|
|
return $this->table->current(); |
|
53
|
|
|
} catch (DataSourceException $e) { |
|
54
|
|
|
throw new DataStreamValidationException($e->getMessage()); |
|
55
|
|
|
} catch (FieldValidationException $e) { |
|
56
|
|
|
throw new DataStreamValidationException($e->getMessage()); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function key() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->table->key(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function next() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->table->next(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function valid() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->table->valid(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|