forked from carlosleopoldo/PatronesDisenoPHP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRowSet.php
100 lines (89 loc) · 1.94 KB
/
RowSet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace DesignPatterns\Iterator;
/**
* Class RowSet
*/
class RowSet implements \Iterator
{
/**
* @var
*/
protected $currentRow;
/**
* @var string
*/
protected $file;
/**
* @var int
*/
protected $lineNumber;
/**
* @param string $file
*/
public function __construct($file)
{
$this->file = $file;
}
/**
* composite pattern: run through all rows and process them
*
* @return void
*/
public function process()
{
// this actually calls rewind(), { next(), valid(), key() and current() :}
/**
* THE key feature of the Iterator Pattern is to provide a *public contract*
* to iterate on a collection without knowing how items are handled inside
* the collection. It is not just an easy way to use "foreach"
*
* One cannot see the point of iterator pattern if you iterate on $this.
* This example is unclear and mixed with some Composite pattern ideas.
*/
foreach ($this as $line => $row) {
$row->process();
}
}
/**
* {@inheritdoc}
*/
public function rewind()
{
// seek to first line from $this->file
}
/**
* {@inheritdoc}
*/
public function next()
{
// read the next line from $this->file
if (!$eof) {
$data = ''; // get the line
$this->currentRow = new Row($data);
} else {
$this->currentRow = null;
}
}
/**
* {@inheritdoc}
*/
public function current()
{
return $this->currentRow;
}
/**
* {@inheritdoc}
*/
public function valid()
{
return null !== $this->currentRow;
}
/**
* {@inheritdoc}
*/
public function key()
{
// you would want to increment this in next() or whatsoever
return $this->lineNumber;
}
}