Completed
Pull Request — master (#3)
by Joao
06:17 queued 02:28
created

FixedTextFileIterator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 65.45%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 133
ccs 36
cts 55
cp 0.6545
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 32 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
    /**
98
     * @param $buffer
99
     * @param $fieldDefinition
100
     * @return array
101
     * @throws \ByJG\AnyDataset\Exception\IteratorException
102
     */
103 2
    protected function processBuffer($buffer, $fieldDefinition)
104
    {
105 2
        $cntDef = count($fieldDefinition);
106 2
        $fields = [];
107 2
        for ($i = 0; $i < $cntDef; $i++) {
108 2
            $fieldDef = $fieldDefinition[$i];
109
110 2
            $fields[$fieldDef->fieldName] = substr($buffer, $fieldDef->startPos, $fieldDef->length);
111 2
            if (!empty($fieldDef->requiredValue)
112 2
                && (
113
                    !preg_match("/^[" . $fieldDef->requiredValue . "]$/", $fields[$fieldDef->fieldName])
114
                )
115 2
            ) {
116
                throw new IteratorException(
117
                    "Expected the value '"
118
                    . $fieldDef->requiredValue
119
                    . "' and I got '"
120
                    . $fields[$fieldDef->fieldName]
121
                    . "'"
122
                );
123
            }
124
125 2
            if (is_array($fieldDef->subTypes)) {
126 1
                $fields[$fieldDef->fieldName] = $this->processBuffer(
127 1
                    $fields[$fieldDef->fieldName],
128 1
                    $fieldDef->subTypes
129 1
                );
130 1
            }
131 2
        }
132
133 2
        return $fields;
134
    }
135
136
    public function key()
137
    {
138
        return $this->current;
139
    }
140
}
141