|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Dataset; |
|
4
|
|
|
|
|
5
|
|
|
class SocketIterator extends GenericIterator |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
private $colsep = null; |
|
9
|
|
|
private $rowsep = null; |
|
10
|
|
|
private $fields = null; //Array |
|
11
|
|
|
private $handle = null; |
|
12
|
|
|
private $rows = null; |
|
13
|
|
|
private $current = 0; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* @param resource $handle |
|
18
|
|
|
* @param array $fieldnames |
|
19
|
|
|
* @param string $rowsep |
|
20
|
|
|
* @param string $colsep |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct($handle, $fieldnames, $rowsep, $colsep) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->rowsep = $rowsep; |
|
25
|
|
|
$this->colsep = $colsep; |
|
26
|
|
|
$this->fields = $fieldnames; |
|
27
|
|
|
$this->handle = $handle; |
|
28
|
|
|
|
|
29
|
|
|
$header = true; |
|
30
|
|
|
while (!feof($this->handle) && $header) { |
|
31
|
|
|
$x = fgets($this->handle); |
|
32
|
|
|
$header = (trim($x) != ""); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$linha = ""; |
|
36
|
|
|
while (!feof($this->handle)) { |
|
37
|
|
|
$x = fgets($this->handle, 4096); |
|
38
|
|
|
if ((trim($x) != "") && (strpos($x, $this->colsep) > 0)) { |
|
39
|
|
|
$linha .= $x; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->rows = array(); |
|
44
|
|
|
$rowsaux = preg_split("/" . $this->rowsep . "/", $linha); |
|
45
|
|
|
sort($rowsaux); |
|
46
|
|
|
foreach ($rowsaux as $value) { |
|
47
|
|
|
$colsaux = preg_split("/" . $this->colsep . "/", $value); |
|
48
|
|
|
if (count($colsaux) == count($fieldnames)) { |
|
49
|
|
|
$this->rows[] = $value; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
fclose($this->handle); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function count() |
|
57
|
|
|
{ |
|
58
|
|
|
return count($this->rows); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @access public |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
|
|
public function hasNext() |
|
66
|
|
|
{ |
|
67
|
|
|
if ($this->current < $this->count()) { |
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @access public |
|
76
|
|
|
* @return Row |
|
77
|
|
|
*/ |
|
78
|
|
|
public function moveNext() |
|
79
|
|
|
{ |
|
80
|
|
|
$cols = preg_split("/" . $this->colsep . "/", $this->rows[$this->current]); |
|
81
|
|
|
$this->current++; |
|
82
|
|
|
|
|
83
|
|
|
$sr = new Row(); |
|
84
|
|
|
$cntFields = count($this->fields); |
|
85
|
|
|
for ($i = 0; $i < $cntFields; $i++) { |
|
86
|
|
|
$sr->addField(strtolower($this->fields[$i]), $cols[$i]); |
|
87
|
|
|
} |
|
88
|
|
|
return $sr; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function key() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->current; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|