| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2019-2022 Graham Breach |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Lesser General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
/** |
| 19 |
* For more information, please contact <graham@goat1000.com> |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Goat1000\SVGGraph; |
| 23 |
|
| 24 |
/** |
| 25 |
* For iterating over structured data |
| 26 |
*/ |
| 27 |
class StructuredDataIterator implements \Iterator { |
| 28 |
|
| 29 |
private $data = 0; |
| 30 |
private $dataset = 0; |
| 31 |
private $position = 0; |
| 32 |
private $count = 0; |
| 33 |
private $structure = null; |
| 34 |
private $key_field = 0; |
| 35 |
private $dataset_fields = []; |
| 36 |
|
| 37 |
public function __construct(&$data, $dataset, $structure) |
| 38 |
{ |
| 39 |
$this->dataset = $dataset; |
| 40 |
$this->data =& $data; |
| 41 |
$this->count = count($data); |
| 42 |
$this->structure = $structure; |
| 43 |
|
| 44 |
$this->key_field = $structure['key']; |
| 45 |
$this->dataset_fields = $structure['value']; |
| 46 |
} |
| 47 |
|
| 48 |
#[\ReturnTypeWillChange] |
| 49 |
public function current() |
| 50 |
{ |
| 51 |
return $this->getItemByIndex($this->position); |
| 52 |
} |
| 53 |
|
| 54 |
#[\ReturnTypeWillChange] |
| 55 |
public function key() |
| 56 |
{ |
| 57 |
return $this->position; |
| 58 |
} |
| 59 |
|
| 60 |
#[\ReturnTypeWillChange] |
| 61 |
public function next() |
| 62 |
{ |
| 63 |
++$this->position; |
| 64 |
} |
| 65 |
|
| 66 |
#[\ReturnTypeWillChange] |
| 67 |
public function rewind() |
| 68 |
{ |
| 69 |
$this->position = 0; |
| 70 |
} |
| 71 |
|
| 72 |
#[\ReturnTypeWillChange] |
| 73 |
public function valid() |
| 74 |
{ |
| 75 |
return $this->position < $this->count; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Returns an item by index |
| 80 |
*/ |
| 81 |
public function getItemByIndex($index) |
| 82 |
{ |
| 83 |
if(isset($this->data[$index])) { |
| 84 |
$key = $this->key_field === null ? $index : null; |
| 85 |
return new StructuredDataItem($this->data[$index], |
| 86 |
$this->structure, $this->dataset, $key); |
| 87 |
} |
| 88 |
return null; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Returns an item by key |
| 93 |
*/ |
| 94 |
public function getItemByKey($key) |
| 95 |
{ |
| 96 |
if($this->key_field === null) { |
| 97 |
if(isset($this->data[$key])) |
| 98 |
return new StructuredDataItem($this->data[$key], $this->structure, |
| 99 |
$this->dataset, $key); |
| 100 |
return null; |
| 101 |
} |
| 102 |
|
| 103 |
foreach($this->data as $item) |
| 104 |
if(isset($item[$this->key_field]) && $item[$this->key_field] == $key) |
| 105 |
return new StructuredDataItem($item, $this->structure, |
| 106 |
$this->dataset, $key); |
| 107 |
return null; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|