| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2013-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 |
* Class for standard data |
| 26 |
*/ |
| 27 |
class Data implements \Countable, \ArrayAccess, \Iterator { |
| 28 |
|
| 29 |
private $datasets = 0; |
| 30 |
private $data; |
| 31 |
private $assoc = null; |
| 32 |
private $datetime = null; |
| 33 |
private $min_value = []; |
| 34 |
private $max_value = []; |
| 35 |
private $min_key = []; |
| 36 |
private $max_key = []; |
| 37 |
public $error = null; |
| 38 |
|
| 39 |
public function __construct(&$data, $force_assoc, $datetime_keys) |
| 40 |
{ |
| 41 |
if(empty($data[0])) { |
| 42 |
$this->error = 'No data'; |
| 43 |
return; |
| 44 |
} |
| 45 |
$this->data = $data; |
| 46 |
$this->datasets = count($data); |
| 47 |
if($force_assoc) |
| 48 |
$this->assoc = true; |
| 49 |
if($datetime_keys) { |
| 50 |
if($this->rekey('Goat1000\\SVGGraph\\Graph::dateConvert')) { |
| 51 |
$this->datetime = true; |
| 52 |
$this->assoc = false; |
| 53 |
return; |
| 54 |
} |
| 55 |
$this->error = 'Too many date/time conversion errors'; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Implement Iterator interface to prevent iteration... |
| 61 |
*/ |
| 62 |
private function notIterator() |
| 63 |
{ |
| 64 |
throw new \Exception('Cannot iterate ' . __CLASS__); |
| 65 |
} |
| 66 |
#[\ReturnTypeWillChange] |
| 67 |
public function current() { $this->notIterator(); } |
| 68 |
#[\ReturnTypeWillChange] |
| 69 |
public function key() { $this->notIterator(); } |
| 70 |
#[\ReturnTypeWillChange] |
| 71 |
public function next() { $this->notIterator(); } |
| 72 |
#[\ReturnTypeWillChange] |
| 73 |
public function rewind() { $this->notIterator(); } |
| 74 |
#[\ReturnTypeWillChange] |
| 75 |
public function valid() { $this->notIterator(); } |
| 76 |
|
| 77 |
/** |
| 78 |
* ArrayAccess methods |
| 79 |
*/ |
| 80 |
#[\ReturnTypeWillChange] |
| 81 |
public function offsetExists($offset) |
| 82 |
{ |
| 83 |
return array_key_exists($offset, $this->data); |
| 84 |
} |
| 85 |
|
| 86 |
#[\ReturnTypeWillChange] |
| 87 |
public function offsetGet($offset) |
| 88 |
{ |
| 89 |
return new DataIterator($this->data, $offset); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Don't allow writing to the data |
| 94 |
*/ |
| 95 |
#[\ReturnTypeWillChange] |
| 96 |
public function offsetSet($offset, $value) |
| 97 |
{ |
| 98 |
throw new \Exception('Read-only'); |
| 99 |
} |
| 100 |
#[\ReturnTypeWillChange] |
| 101 |
public function offsetUnset($offset) |
| 102 |
{ |
| 103 |
throw new \Exception('Read-only'); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Countable method |
| 108 |
*/ |
| 109 |
#[\ReturnTypeWillChange] |
| 110 |
public function count() |
| 111 |
{ |
| 112 |
return $this->datasets; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns minimum data value for a dataset |
| 117 |
*/ |
| 118 |
public function getMinValue($dataset = 0) |
| 119 |
{ |
| 120 |
if(!isset($this->min_value[$dataset])) { |
| 121 |
$this->min_value[$dataset] = null; |
| 122 |
if(count($this->data[$dataset])) |
| 123 |
$this->min_value[$dataset] = Graph::min($this->data[$dataset]); |
| 124 |
} |
| 125 |
return $this->min_value[$dataset]; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Returns maximum data value for a dataset |
| 130 |
*/ |
| 131 |
public function getMaxValue($dataset = 0) |
| 132 |
{ |
| 133 |
if(!isset($this->max_value[$dataset])) { |
| 134 |
$this->max_value[$dataset] = null; |
| 135 |
if(count($this->data[$dataset])) |
| 136 |
$this->max_value[$dataset] = max($this->data[$dataset]); |
| 137 |
} |
| 138 |
return $this->max_value[$dataset]; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns the minimum key value |
| 143 |
*/ |
| 144 |
public function getMinKey($dataset = 0) |
| 145 |
{ |
| 146 |
if(!isset($this->min_key[$dataset])) { |
| 147 |
$this->min_key[$dataset] = null; |
| 148 |
if(count($this->data[$dataset])) { |
| 149 |
$this->min_key[$dataset] = $this->associativeKeys() ? 0 : |
| 150 |
min(array_keys($this->data[$dataset])); |
| 151 |
} |
| 152 |
} |
| 153 |
return $this->min_key[$dataset]; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Returns the maximum key value |
| 158 |
*/ |
| 159 |
public function getMaxKey($dataset = 0) |
| 160 |
{ |
| 161 |
if(!isset($this->max_key[$dataset])) { |
| 162 |
$this->max_key[$dataset] = null; |
| 163 |
if(count($this->data[$dataset])) { |
| 164 |
$this->max_key[$dataset] = $this->associativeKeys() ? |
| 165 |
count($this->data[$dataset]) - 1 : |
| 166 |
max(array_keys($this->data[$dataset])); |
| 167 |
} |
| 168 |
} |
| 169 |
return $this->max_key[$dataset]; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Returns the key at a given index |
| 174 |
*/ |
| 175 |
public function getKey($index, $dataset = 0) |
| 176 |
{ |
| 177 |
if(!$this->associativeKeys()) |
| 178 |
return $index; |
| 179 |
|
| 180 |
// round index to nearest integer, or PHP will floor() it |
| 181 |
$index = (int)round($index); |
| 182 |
if($index >= 0) { |
| 183 |
$slice = array_slice($this->data[$dataset], $index, 1, true); |
| 184 |
// use foreach to get key and value |
| 185 |
foreach($slice as $k => $v) |
| 186 |
return $k; |
| 187 |
} |
| 188 |
return null; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Returns TRUE if the keys are associative |
| 193 |
*/ |
| 194 |
public function associativeKeys() |
| 195 |
{ |
| 196 |
if($this->assoc !== null) |
| 197 |
return $this->assoc; |
| 198 |
|
| 199 |
foreach(array_keys($this->data[0]) as $k) |
| 200 |
if(!is_integer($k)) |
| 201 |
return ($this->assoc = true); |
| 202 |
return ($this->assoc = false); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Returns the number of data items |
| 207 |
*/ |
| 208 |
public function itemsCount($dataset = 0) |
| 209 |
{ |
| 210 |
if($dataset < 0) |
| 211 |
$dataset = 0; |
| 212 |
return count($this->data[$dataset]); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Returns the min and max sum values |
| 217 |
*/ |
| 218 |
public function getMinMaxSumValues($start = 0, $end = null) |
| 219 |
{ |
| 220 |
if($start != 0 || ($end !== null && $end != 0)) |
| 221 |
throw new \Exception('Dataset not found'); |
| 222 |
|
| 223 |
// structured data is used for multi-data, so just |
| 224 |
// return the min and max |
| 225 |
return [$this->getMinValue(), $this->getMaxValue()]; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Returns the min/max sum values for an array of datasets |
| 230 |
*/ |
| 231 |
public function getMinMaxSumValuesFor($datasets) |
| 232 |
{ |
| 233 |
// Data class can't handle multiple datasets |
| 234 |
if(count($datasets) > 1) |
| 235 |
throw new \InvalidArgumentException('Multiple datasets not supported'); |
| 236 |
|
| 237 |
$d = array_pop($datasets); |
| 238 |
if($d < 0 || $d >= $this->datasets) |
| 239 |
throw new \Exception('Dataset not found'); |
| 240 |
|
| 241 |
return [$this->getMinValue($d), $this->getMaxValue($d)]; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Returns TRUE if the item exists, setting the $value |
| 246 |
*/ |
| 247 |
public function getData($index, $name, &$value) |
| 248 |
{ |
| 249 |
// base class doesn't support this, so always return false |
| 250 |
return false; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Doesn't return a structured data item |
| 255 |
*/ |
| 256 |
public function getItem($index, $dataset = 0) |
| 257 |
{ |
| 258 |
return null; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Transforms the keys using a callback function |
| 263 |
*/ |
| 264 |
public function rekey($callback) |
| 265 |
{ |
| 266 |
$new_data = []; |
| 267 |
$count = $invalid = 0; |
| 268 |
for($d = 0; $d < $this->datasets; ++$d) { |
| 269 |
$new_data[$d] = []; |
| 270 |
foreach($this->data[$d] as $key => $value) { |
| 271 |
$new_key = call_user_func($callback, $key); |
| 272 |
|
| 273 |
// if the callback returns null, skip the value |
| 274 |
if($new_key === null) { |
| 275 |
++$invalid; |
| 276 |
continue; |
| 277 |
} |
| 278 |
|
| 279 |
$new_data[$d][$new_key] = $value; |
| 280 |
} |
| 281 |
++$count; |
| 282 |
} |
| 283 |
// if too many invalid, probably a format error |
| 284 |
if($count && $invalid / $count > 0.05) |
| 285 |
return false; |
| 286 |
$this->data = $new_data; |
| 287 |
// forget previous min/max |
| 288 |
$this->min_key = []; |
| 289 |
$this->max_key = []; |
| 290 |
return true; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|