-
Notifications
You must be signed in to change notification settings - Fork 10
/
DefaultDataStream.php
74 lines (62 loc) · 1.76 KB
/
DefaultDataStream.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
<?php
namespace frictionlessdata\datapackage\DataStreams;
use frictionlessdata\datapackage\Exceptions\DataStreamOpenException;
/**
* streams the raw data without processing - used for default data package resources.
*/
class DefaultDataStream extends BaseDataStream
{
public $fopenResource;
/**
* @param $dataSource
*
* @param null $dataSourceOptions
*
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamOpenException
*/
public function __construct($dataSource, $dataSourceOptions = null)
{
parent::__construct($dataSource, $dataSourceOptions);
try {
$this->fopenResource = fopen($this->dataSource, 'r');
} catch (\Exception $e) {
throw new DataStreamOpenException('Failed to open data source '.json_encode($this->dataSource).': '.json_encode($e->getMessage()));
}
}
public function __destruct()
{
fclose($this->fopenResource);
}
public function rewind()
{
if ($this->currentLineNumber == 0) {
// starting iterations
$this->currentLineNumber = 1;
} else {
throw new \Exception('DataStream does not support rewinding a stream, sorry');
}
}
public function save($filename)
{
$target = fopen($filename, 'w');
stream_copy_to_stream($this->fopenResource, $target);
fclose($target);
}
public function current()
{
return fgets($this->fopenResource);
}
public function key()
{
return $this->currentLineNumber;
}
public function next()
{
++$this->currentLineNumber;
}
public function valid()
{
return !feof($this->fopenResource);
}
protected $currentLineNumber = 0;
}