1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Wowstack\Dbc; |
||
6 | |||
7 | /** |
||
8 | * Implements iteration over the records of a DBC file. |
||
9 | */ |
||
10 | class DBCIterator implements \Iterator |
||
11 | { |
||
12 | /** |
||
13 | * @var DBC |
||
14 | */ |
||
15 | protected $dbcFile = null; |
||
16 | |||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | protected $position = 0; |
||
21 | |||
22 | /** |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
23 | * Creates a new DBC iterator. |
||
24 | * |
||
25 | * @var DBC $dbcFile A valid DBC file to be used |
||
26 | */ |
||
27 | 2 | public function __construct(DBC $dbcFile) |
|
28 | { |
||
29 | 2 | $this->dbcFile = $dbcFile; |
|
30 | 2 | } |
|
31 | |||
32 | /** |
||
33 | * Reset iterator to the first record. |
||
34 | */ |
||
35 | 1 | public function rewind() |
|
36 | { |
||
37 | 1 | $this->position = 0; |
|
38 | 1 | } |
|
39 | |||
40 | /** |
||
41 | * Returns the current record. |
||
42 | * |
||
43 | * @return DBCRecord |
||
44 | * |
||
45 | * @throws DBCException |
||
46 | */ |
||
47 | 1 | public function current() |
|
48 | { |
||
49 | 1 | return $this->dbcFile->getRecord($this->position); |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * Returns the current record index. |
||
54 | * |
||
55 | * @return int |
||
56 | */ |
||
57 | 1 | public function key(): int |
|
58 | { |
||
59 | 1 | return $this->position; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Advances the record index by one. |
||
64 | */ |
||
65 | 1 | public function next() |
|
66 | { |
||
67 | 1 | ++$this->position; |
|
68 | 1 | } |
|
69 | |||
70 | /** |
||
71 | * Moves the record index back by one. |
||
72 | */ |
||
73 | 1 | public function prev() |
|
74 | { |
||
75 | 1 | --$this->position; |
|
76 | 1 | } |
|
77 | |||
78 | /** |
||
79 | * Moves record position to given position. |
||
80 | * |
||
81 | * @param int $position |
||
82 | */ |
||
83 | 1 | public function seek(int $position) |
|
84 | { |
||
85 | 1 | $this->position = $position; |
|
86 | 1 | } |
|
87 | |||
88 | /** |
||
89 | * Returns information if a given record index exists. |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | 1 | public function valid(): bool |
|
94 | { |
||
95 | 1 | return $this->dbcFile->hasRecord($this->position); |
|
96 | } |
||
97 | } |
||
98 |