| 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 |
* Data class for SVG path data |
| 26 |
*/ |
| 27 |
class PathData { |
| 28 |
|
| 29 |
private $parts = ''; |
| 30 |
private $last = ''; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructs a path from another PathData, elements or an array of elements |
| 34 |
*/ |
| 35 |
public function __construct() |
| 36 |
{ |
| 37 |
$segments = func_get_args(); |
| 38 |
$num = count($segments); |
| 39 |
if($num) { |
| 40 |
if($num > 1) { |
| 41 |
$this->add($segments); |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$this->add($segments[0]); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Adds segments to the path |
| 51 |
*/ |
| 52 |
public function add($e) |
| 53 |
{ |
| 54 |
$segments = func_get_args(); |
| 55 |
if(count($segments) < 1) |
| 56 |
throw new \Exception('No segments to add'); |
| 57 |
|
| 58 |
if(count($segments) == 1) { |
| 59 |
$e = $segments[0]; |
| 60 |
|
| 61 |
// add another PathData |
| 62 |
if(is_object($e) && get_class($e) === 'Goat1000\\SVGGraph\\PathData') { |
| 63 |
$this->parts .= ' ' . $e->parts; |
| 64 |
$this->last = $e->last; |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
if(is_array($e)) |
| 69 |
$segments = $e; |
| 70 |
} |
| 71 |
|
| 72 |
$this->addSegments($segments); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns true if the path has no segments |
| 77 |
*/ |
| 78 |
public function isEmpty() |
| 79 |
{ |
| 80 |
return $this->parts === ''; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Clears any existing segments |
| 85 |
*/ |
| 86 |
public function clear() |
| 87 |
{ |
| 88 |
$this->parts = ''; |
| 89 |
$this->last = ''; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Converts the path segments to a string |
| 94 |
*/ |
| 95 |
public function __toString() |
| 96 |
{ |
| 97 |
// already contains a string, this just reduces whitespace |
| 98 |
return preg_replace(['/ ([a-zA-Z])/', '/([a-zA-Z]) /'], '$1', $this->parts); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Adds an array of segments |
| 103 |
*/ |
| 104 |
private function addSegments($segments) |
| 105 |
{ |
| 106 |
$last = $this->last; |
| 107 |
$parts = ''; |
| 108 |
foreach($segments as $part) { |
| 109 |
|
| 110 |
if(is_object($part)) { |
| 111 |
if(get_class($part) === 'Goat1000\\SVGGraph\\PathData') |
| 112 |
throw new \InvalidArgumentException('PathData in segment list. Use separate add() calls.'); |
| 113 |
|
| 114 |
$parts .= ' ' . $part; |
| 115 |
continue; |
| 116 |
} |
| 117 |
|
| 118 |
if(!is_numeric($part)) { |
| 119 |
// skip duplicate path commands |
| 120 |
if($part == $last) |
| 121 |
continue; |
| 122 |
$last = $part; |
| 123 |
$parts .= $part; |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
$parts .= ' ' . new Number($part); |
| 128 |
} |
| 129 |
|
| 130 |
if($parts !== '') { |
| 131 |
$this->last = $last; |
| 132 |
$this->parts .= $parts; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|