Completed
Pull Request — master (#2)
by Joao
05:22
created

FixedTextFileIterator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 63.46%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 124
ccs 33
cts 52
cp 0.6346
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A count() 0 4 1
A hasNext() 0 14 3
B moveNext() 0 24 5
B processBuffer() 0 29 5
A key() 0 4 1
1
<?php
2
3
namespace ByJG\AnyDataset\Dataset;
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 resource $handle
30
     * @param FixedTextDefinition[] $fields
31
     */
32 2
    public function __construct($handle, $fields)
33
    {
34 2
        $this->fields = $fields;
35 2
        $this->handle = $handle;
36 2
        $this->current = 0;
37 2
    }
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 2
    public function hasNext()
53
    {
54 2
        if (!$this->handle) {
55
            return false;
56
        }
57
58 2
        if (feof($this->handle)) {
59 2
            fclose($this->handle);
60
61 2
            return false;
62
        }
63
64 2
        return true;
65
    }
66
67
68
    /**
69
     * @return Row|null
70
     * @throws IteratorException
71
     */
72 2
    public function moveNext()
73
    {
74 2
        if ($this->hasNext()) {
75 2
            $buffer = fgets($this->handle, 4096);
76
77 2
            if ($buffer == "") {
78
                return new Row();
79
            }
80
81 2
            $fields = $this->processBuffer($buffer, $this->fields);
82
83 2
            if (is_null($fields)) {
84
                throw new IteratorException("Definition does not match");
85
            }
86
87 2
            $this->current++;
88 2
            return new Row($fields);
89
        }
90
91
        if ($this->handle) {
92
            fclose($this->handle);
93
        }
94
        return null;
95
    }
96
97 2
    protected function processBuffer($buffer, $fieldDefinition)
98
    {
99 2
        $cntDef = count($fieldDefinition);
100 2
        $fields = [];
101 2
        for ($i = 0; $i < $cntDef; $i++) {
102 2
            $fieldDef = $fieldDefinition[$i];
103
104 2
            $fields[$fieldDef->fieldName] = substr($buffer, $fieldDef->startPos, $fieldDef->length);
105 2
            if (!empty($fieldDef->requiredValue)
106 2
                && (
107
                    !preg_match("/^[" . $fieldDef->requiredValue . "]$/", $fields[$fieldDef->fieldName])
108
                )
109 2
            ) {
110
                throw new IteratorException(
111
                    "Expected the value '"
112
                    . $fieldDef->requiredValue
113
                    . "' and I got '"
114
                    . $fields[$fieldDef->fieldName]
115
                    . "'"
116
                );
117
            }
118
119 2
            if (is_array($fieldDef->subTypes)) {
120 1
                $fields[$fieldDef->fieldName] = $this->processBuffer($fields[$fieldDef->fieldName], $fieldDef->subTypes);
121 1
            }
122 2
        }
123
124 2
        return $fields;
125
    }
126
127
    public function key()
128
    {
129
        return $this->current;
130
    }
131
}
132