Completed
Push — master ( 1857e6...d96bec )
by Joao
03:02
created

src/Repository/TextFileIterator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace ByJG\AnyDataset\Repository;
4
5
class TextFileIterator extends GenericIterator
6
{
7
8
    protected $_fields;
9
    protected $_fieldexpression;
10
    protected $_handle;
11
    protected $_current = 0;
12
    protected $_currentBuffer = "";
13
14
    /**
15
     * @access public
16
     * @return IteratorInterface
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
17
     */
18
    public function __construct($handle, $fields, $fieldexpression)
19
    {
20
        $this->_fields = $fields;
21
        $this->_fieldexpression = $fieldexpression;
22
        $this->_handle = $handle;
23
24
        $this->readNextLine();
25
    }
26
27
    protected function readNextLine()
28
    {
29
        if ($this->hasNext()) {
30
            $buffer = fgets($this->_handle, 4096);
31
            $this->_currentBuffer = false;
32
33
            if (($buffer !== false) && (trim($buffer) != "")) {
34
                $this->_current++;
35
                $this->_currentBuffer = $buffer;
36
            } else $this->readNextLine();
37
        }
38
    }
39
40
    /**
41
     * @access public
42
     * @return int
43
     */
44
    public function count()
45
    {
46
        return -1;
47
    }
48
49
    /**
50
     * @access public
51
     * @return bool
52
     */
53
    public function hasNext()
54
    {
55
        if ($this->_currentBuffer !== false) {
56
            return true;
57
        } elseif (!$this->_handle) {
58
            return false;
59 View Code Duplication
        } else {
60
            if (feof($this->_handle)) {
61
                fclose($this->_handle);
62
                $this->_handle = null;
63
                return false;
64
            } else {
65
                return true;
66
            }
67
        }
68
    }
69
70
    /**
71
     * @access public
72
     * @return SingleRow
73
     */
74
    public function moveNext()
75
    {
76
        if ($this->hasNext()) {
77
            $cols = preg_split($this->_fieldexpression, $this->_currentBuffer, -1, PREG_SPLIT_DELIM_CAPTURE);
78
79
            $sr = new SingleRow();
80
81
            for ($i = 0; ($i < sizeof($this->_fields)) && ($i < sizeof($cols)); $i++) {
82
                $column = $cols[$i];
83
84
                if (($i >= sizeof($this->_fields) - 1) || ($i >= sizeof($cols) - 1)) {
85
                    $column = preg_replace("/(\r?\n?)$/", "", $column);
86
                }
87
88
                $sr->addField(strtolower($this->_fields[$i]), $column);
89
            }
90
91
            $this->readNextLine();
92
            return $sr;
93
        } else {
94
            if ($this->_handle) {
95
                fclose($this->_handle);
96
            }
97
            return null;
98
        }
99
    }
100
101
    function key()
102
    {
103
        return $this->_current;
104
    }
105
}
106