| 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 |
* Class for structured data items |
| 26 |
*/ |
| 27 |
class StructuredDataItem extends DataItem { |
| 28 |
|
| 29 |
private $item; |
| 30 |
private $dataset = 0; |
| 31 |
private $key_field = 0; |
| 32 |
private $dataset_fields = []; |
| 33 |
private $structure; |
| 34 |
public $key = 0; |
| 35 |
public $value = null; |
| 36 |
|
| 37 |
public function __construct($item, &$structure, $dataset, $key = null) |
| 38 |
{ |
| 39 |
$this->item = $item; |
| 40 |
$this->key_field = $structure['key']; |
| 41 |
$this->dataset_fields = $structure['value']; |
| 42 |
$this->key = $this->key_field === null ? $key : $item[$this->key_field]; |
| 43 |
if(isset($this->dataset_fields[$dataset]) && |
| 44 |
isset($item[$this->dataset_fields[$dataset]])) |
| 45 |
$this->value = $item[$this->dataset_fields[$dataset]]; |
| 46 |
|
| 47 |
$this->dataset = $dataset; |
| 48 |
$this->structure = &$structure; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Constructs a new data item with a different dataset |
| 53 |
*/ |
| 54 |
public function newFrom($dataset) |
| 55 |
{ |
| 56 |
return new StructuredDataItem($this->item, $this->structure, |
| 57 |
$dataset, $this->key); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Getter for data fields |
| 62 |
*/ |
| 63 |
public function __get($field) |
| 64 |
{ |
| 65 |
return $this->data($field); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Tests if a field is set |
| 70 |
*/ |
| 71 |
public function __isset($field) |
| 72 |
{ |
| 73 |
if(!isset($this->structure[$field])) |
| 74 |
return false; |
| 75 |
$item_field = $this->structure[$field]; |
| 76 |
if(is_array($item_field)) { |
| 77 |
if(!isset($item_field[$this->dataset])) |
| 78 |
return false; |
| 79 |
$item_field = $item_field[$this->dataset]; |
| 80 |
} |
| 81 |
|
| 82 |
return isset($this->item[$item_field]); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Returns some extra data from item |
| 87 |
*/ |
| 88 |
public function data($field) |
| 89 |
{ |
| 90 |
if(!isset($this->structure[$field])) |
| 91 |
return null; |
| 92 |
$item_field = $this->structure[$field]; |
| 93 |
if(is_array($item_field)) { |
| 94 |
if(!isset($item_field[$this->dataset])) |
| 95 |
return null; |
| 96 |
$item_field = $item_field[$this->dataset]; |
| 97 |
} |
| 98 |
|
| 99 |
return isset($this->item[$item_field]) ? $this->item[$item_field] : null; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Check if extra data field exists |
| 104 |
*/ |
| 105 |
public function rawDataExists($field) |
| 106 |
{ |
| 107 |
return isset($this->item[$field]); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Returns a value from the item without translating structure |
| 112 |
*/ |
| 113 |
public function rawData($field) |
| 114 |
{ |
| 115 |
return isset($this->item[$field]) ? $this->item[$field] : null; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|