Completed
Branch master (7d7b3f)
by Ori
01:38
created

DefaultDataStream::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace frictionlessdata\datapackage\DataStreams;
4
5
use frictionlessdata\datapackage\Exceptions\DataStreamOpenException;
6
7
/**
8
 * streams the raw data without processing - used for default data package resources.
9
 */
10
class DefaultDataStream extends BaseDataStream
11
{
12
    public $fopenResource;
13
14
    /**
15
     * @param string $dataSource
16
     *
17
     * @throws DataStreamOpenException
18
     */
19
    public function __construct($dataSource, $dataSourceOptions = null)
20
    {
21
        parent::__construct($dataSource, $dataSourceOptions);
22
        try {
23
            $this->fopenResource = fopen($this->dataSource, 'r');
24
        } catch (\Exception $e) {
25
            throw new DataStreamOpenException('Failed to open data source '.json_encode($this->dataSource).': '.json_encode($e->getMessage()));
26
        }
27
    }
28
29
    public function __destruct()
30
    {
31
        fclose($this->fopenResource);
32
    }
33
34
    public function rewind()
35
    {
36
        if ($this->currentLineNumber == 0) {
37
            // starting iterations
38
            $this->currentLineNumber = 1;
39
        } else {
40
            throw new \Exception('DataStream does not support rewinding a stream, sorry');
41
        }
42
    }
43
44
    public function current()
45
    {
46
        return fgets($this->fopenResource);
47
    }
48
49
    public function key()
50
    {
51
        return $this->currentLineNumber;
52
    }
53
54
    public function next()
55
    {
56
        ++$this->currentLineNumber;
57
    }
58
59
    public function valid()
60
    {
61
        return !feof($this->fopenResource);
62
    }
63
64
    protected $currentLineNumber = 0;
65
}
66