-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathDataIterator.php
98 lines (86 loc) · 2.22 KB
/
DataIterator.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
<?php
/**
* Copyright (C) 2013-2022 Graham Breach
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* For more information, please contact <graham@goat1000.com>
*/
namespace Goat1000\SVGGraph;
/**
* Class to iterate over standard data
*/
class DataIterator implements \Iterator {
private $data = 0;
private $dataset = 0;
private $position = 0;
private $count = 0;
public function __construct(&$data, $dataset)
{
$this->dataset = $dataset;
$this->data =& $data;
$this->count = count($data[$dataset]);
}
/**
* Iterator methods
*/
#[\ReturnTypeWillChange]
public function current()
{
return $this->getItemByIndex($this->position);
}
#[\ReturnTypeWillChange]
public function key()
{
return $this->position;
}
#[\ReturnTypeWillChange]
public function next()
{
++$this->position;
next($this->data[$this->dataset]);
}
#[\ReturnTypeWillChange]
public function rewind()
{
$this->position = 0;
reset($this->data[$this->dataset]);
}
#[\ReturnTypeWillChange]
public function valid()
{
return $this->position < $this->count;
}
/**
* Returns an item by index
*/
public function getItemByIndex($index)
{
$slice = array_slice($this->data[$this->dataset], $index, 1, true);
// use foreach to get key and value
foreach($slice as $k => $v)
return new DataItem($k, $v);
return null;
}
/**
* Returns an item by its key
*/
public function getItemByKey($key)
{
if(isset($this->data[$this->dataset][$key]))
return new DataItem($key, $this->data[$this->dataset][$key]);
return null;
}
}