| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2018-2023 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 |
class Figures { |
| 25 |
|
| 26 |
private $graph; |
| 27 |
private $settings; |
| 28 |
private $loaded = false; |
| 29 |
private $figure_map = []; |
| 30 |
|
| 31 |
public function __construct(&$graph, &$settings) |
| 32 |
{ |
| 33 |
$this->graph =& $graph; |
| 34 |
$this->settings =& $settings; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Load figures from options list |
| 39 |
*/ |
| 40 |
public function load() |
| 41 |
{ |
| 42 |
if($this->loaded) |
| 43 |
return; |
| 44 |
$this->loaded = true; |
| 45 |
|
| 46 |
if(!isset($this->settings['figure'])) |
| 47 |
return; |
| 48 |
|
| 49 |
if(!is_array($this->settings['figure']) || |
| 50 |
!isset($this->settings['figure'][0])) |
| 51 |
throw new \Exception('Malformed figure option.'); |
| 52 |
|
| 53 |
if(!is_array($this->settings['figure'][0])) { |
| 54 |
$this->addFigure($this->settings['figure']); |
| 55 |
} else { |
| 56 |
foreach($this->settings['figure'] as $figure) { |
| 57 |
$this->addFigure($figure); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Adds a figure to the list |
| 64 |
*/ |
| 65 |
private function addFigure($figure_array) |
| 66 |
{ |
| 67 |
$name = array_shift($figure_array); |
| 68 |
if(isset($this->figure_map[$name])) |
| 69 |
throw new \Exception('Figure [' . $name . '] defined more than once.'); |
| 70 |
$content = ''; |
| 71 |
$shapes = $this->graph->getShapeList(); |
| 72 |
if(!is_array($figure_array[0])) { |
| 73 |
$shape = $shapes->getShape($figure_array); |
| 74 |
$content .= $shape->draw($this->graph); |
| 75 |
} else { |
| 76 |
foreach($figure_array as $s) { |
| 77 |
$shape = $shapes->getShape($s); |
| 78 |
$content .= $shape->draw($this->graph); |
| 79 |
} |
| 80 |
} |
| 81 |
$id = $this->graph->defs->defineSymbol($content); |
| 82 |
$this->figure_map[$name] = $id; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Returns a figure's symbol ID by name |
| 87 |
*/ |
| 88 |
public function getFigure($name) |
| 89 |
{ |
| 90 |
$this->load(); |
| 91 |
if(isset($this->figure_map[$name])) |
| 92 |
return $this->figure_map[$name]; |
| 93 |
return null; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|