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 \ByJG\AnyDataset\Exception\IteratorException |
71
|
|
|
* @throws \ByJG\Serializer\Exception\InvalidArgumentException |
72
|
|
|
*/ |
73
|
2 |
|
public function moveNext() |
74
|
|
|
{ |
75
|
2 |
|
if ($this->hasNext()) { |
76
|
2 |
|
$buffer = fgets($this->handle, 4096); |
77
|
|
|
|
78
|
2 |
|
if ($buffer == "") { |
79
|
|
|
return new Row(); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
$fields = $this->processBuffer($buffer, $this->fields); |
83
|
|
|
|
84
|
2 |
|
if (is_null($fields)) { |
85
|
|
|
throw new IteratorException("Definition does not match"); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
$this->current++; |
89
|
2 |
|
return new Row($fields); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($this->handle) { |
93
|
|
|
fclose($this->handle); |
94
|
|
|
} |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $buffer |
100
|
|
|
* @param $fieldDefinition |
101
|
|
|
* @return array |
102
|
|
|
* @throws \ByJG\AnyDataset\Exception\IteratorException |
103
|
|
|
*/ |
104
|
2 |
|
protected function processBuffer($buffer, $fieldDefinition) |
105
|
|
|
{ |
106
|
2 |
|
$cntDef = count($fieldDefinition); |
107
|
2 |
|
$fields = []; |
108
|
2 |
|
for ($i = 0; $i < $cntDef; $i++) { |
109
|
2 |
|
$fieldDef = $fieldDefinition[$i]; |
110
|
|
|
|
111
|
2 |
|
$fields[$fieldDef->fieldName] = substr($buffer, $fieldDef->startPos, $fieldDef->length); |
112
|
2 |
|
if (!empty($fieldDef->requiredValue) |
113
|
|
|
&& ( |
114
|
2 |
|
!preg_match("/^[" . $fieldDef->requiredValue . "]$/", $fields[$fieldDef->fieldName]) |
115
|
|
|
) |
116
|
|
|
) { |
117
|
|
|
throw new IteratorException( |
118
|
|
|
"Expected the value '" |
119
|
|
|
. $fieldDef->requiredValue |
120
|
|
|
. "' and I got '" |
121
|
|
|
. $fields[$fieldDef->fieldName] |
122
|
|
|
. "'" |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
if (is_array($fieldDef->subTypes)) { |
127
|
1 |
|
$fields[$fieldDef->fieldName] = $this->processBuffer( |
128
|
1 |
|
$fields[$fieldDef->fieldName], |
129
|
1 |
|
$fieldDef->subTypes |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
return $fields; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function key() |
138
|
|
|
{ |
139
|
|
|
return $this->current; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|