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

src/Repository/FixedTextFileIterator.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
use ByJG\AnyDataset\Enum\FixedTextDefinition;
6
use ByJG\AnyDataset\Exception\IteratorException;
7
8
class FixedTextFileIterator extends GenericIterator
9
{
10
11
    /**
12
     *
13
     * @var FixedTextDefinition[]
14
     */
15
    protected $_fields;
16
17
    /**
18
     * @var resource
19
     */
20
    protected $_handle;
21
22
    /**
23
     * @var int
24
     */
25
    protected $_current = 0;
26
27
    /**
28
     *
29
     * @param int $handle
30
     * @param FixedTextDefinition[] $fields
31
     */
32
    public function __construct($handle, $fields)
33
    {
34
        $this->_fields = $fields;
35
        $this->_handle = $handle;
0 ignored issues
show
Documentation Bug introduced by
It seems like $handle of type integer is incompatible with the declared type resource of property $_handle.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
        $this->_current = 0;
37
    }
38
39
    /**
40
     * @access public
41
     * @return int
42
     */
43
    public function count()
44
    {
45
        return -1;
46
    }
47
48
    /**
49
     * @access public
50
     * @return bool
51
     */
52
    public function hasNext()
53
    {
54 View Code Duplication
        if (!$this->_handle) {
55
            return false;
56
        } else {
57
            if (feof($this->_handle)) {
58
                fclose($this->_handle);
59
                return false;
60
            } else {
61
                return true;
62
            }
63
        }
64
    }
65
66
67
    /**
68
     * @return SingleRow|null
69
     * @throws IteratorException
70
     */
71
    public function moveNext()
72
    {
73
        if ($this->hasNext()) {
74
            $buffer = fgets($this->_handle, 4096);
75
76
            if ($buffer == "") {
77
                return new SingleRow();
78
            }
79
80
            $fields = $this->processBuffer($buffer, $this->_fields);
81
82
            if (is_null($fields)) {
83
                throw new IteratorException("Definition does not match");
84
            }
85
86
            $this->_current++;
87
            return new SingleRow($fields);
88
        } else {
89
            if ($this->_handle) {
90
                fclose($this->_handle);
91
            }
92
            return null;
93
        }
94
    }
95
96
    protected function processBuffer($buffer, $fieldDefinition)
97
    {
98
        $cntDef = count($fieldDefinition);
99
        $fields = [];
100
        for ($i = 0; $i < $cntDef; $i++) {
101
            $fieldDef = $fieldDefinition[$i];
102
103
            $fields[$fieldDef->fieldName] = substr($buffer, $fieldDef->startPos, $fieldDef->length);
104
            if (!empty($fieldDef->requiredValue) && (!preg_match("/^[" . $fieldDef->requiredValue . "]$/",
105
                    $fields[$fieldDef->fieldName]))) {
106
                throw new IteratorException("Expected the value '" . $fieldDef->requiredValue . "' and I got '" . $fields[$fieldDef->fieldName] . "'");
107
            } elseif (is_array($fieldDef->subTypes)) {
108
                $fields[$fieldDef->fieldName] = $this->processBuffer($fields[$fieldDef->fieldName], $fieldDef->subTypes);
109
            }
110
        }
111
112
        return $fields;
113
    }
114
115
    function key()
116
    {
117
        return $this->_current;
118
    }
119
}
120