| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2019 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 SVG transforms |
| 26 |
*/ |
| 27 |
class Transform { |
| 28 |
private $transforms = []; |
| 29 |
|
| 30 |
/** |
| 31 |
* Output for SVG values |
| 32 |
*/ |
| 33 |
public function __toString() |
| 34 |
{ |
| 35 |
$str = ''; |
| 36 |
foreach($this->transforms as $xform) { |
| 37 |
$str .= $xform[0] . '('; |
| 38 |
$str .= implode(' ', $xform[1]); |
| 39 |
$str .= ')'; |
| 40 |
} |
| 41 |
return $str; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Adds another transform to this one |
| 46 |
*/ |
| 47 |
public function add($xform) |
| 48 |
{ |
| 49 |
if(!is_object($xform) || get_class($xform) !== 'Goat1000\\SVGGraph\\Transform') |
| 50 |
throw new \InvalidArgumentException('Argument is not a Transform'); |
| 51 |
|
| 52 |
$this->transforms = array_merge($this->transforms, $xform->transforms); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Translate by $x, $y |
| 57 |
*/ |
| 58 |
public function translate($x, $y) |
| 59 |
{ |
| 60 |
$this->transforms[] = ['translate', [new Number($x), new Number($y)]]; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Scale by $x, or by $x and $y |
| 65 |
*/ |
| 66 |
public function scale($x, $y = null) |
| 67 |
{ |
| 68 |
$args = [new Number($x)]; |
| 69 |
if($y !== null) |
| 70 |
$args[] = new Number($y); |
| 71 |
$this->transforms[] = ['scale', $args]; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Rotate by $a degrees, around point $x, $y |
| 76 |
*/ |
| 77 |
public function rotate($a, $x = null, $y = null) |
| 78 |
{ |
| 79 |
$args = [new Number($a)]; |
| 80 |
if($x !== null && $y !== null) { |
| 81 |
$args[] = new Number($x); |
| 82 |
$args[] = new Number($y); |
| 83 |
} |
| 84 |
$this->transforms[] = ['rotate', $args]; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Skew by $a degrees along X-axis |
| 89 |
*/ |
| 90 |
public function skewX($a) |
| 91 |
{ |
| 92 |
$this->transforms[] = ['skewX', [new Number($a)]]; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Skew by $a degrees along Y-axis |
| 97 |
*/ |
| 98 |
public function skewY($a) |
| 99 |
{ |
| 100 |
$this->transforms[] = ['skewY', [new Number($a)]]; |
| 101 |
} |
| 102 |
} |
| 103 |
|