| 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 attribute |
| 26 |
*/ |
| 27 |
class Attribute { |
| 28 |
public $name; |
| 29 |
public $value; |
| 30 |
public $encoding; |
| 31 |
public $units = ''; |
| 32 |
|
| 33 |
// these properties require units to work well |
| 34 |
private static $require_units = [ |
| 35 |
'baseline-shift' => 1, |
| 36 |
'font-size' => 1, |
| 37 |
'kerning' => 1, |
| 38 |
'letter-spacing' => 1, |
| 39 |
'stroke-dashoffset' => 1, |
| 40 |
'stroke-width' => 1, |
| 41 |
'word-spacing' => 1, |
| 42 |
]; |
| 43 |
|
| 44 |
public function __construct($name, $value, $encoding) |
| 45 |
{ |
| 46 |
$this->name = $name; |
| 47 |
if(isset(Attribute::$require_units[$name])) |
| 48 |
$this->units = 'px'; |
| 49 |
$this->value = $value; |
| 50 |
$this->encoding = $encoding; |
| 51 |
|
| 52 |
if(is_numeric($value)) |
| 53 |
$this->value = new Number($value, $this->units); |
| 54 |
} |
| 55 |
|
| 56 |
public function __toString() |
| 57 |
{ |
| 58 |
if($this->value === null) |
| 59 |
return ''; |
| 60 |
if(is_object($this->value)) |
| 61 |
return (string)$this->value; |
| 62 |
|
| 63 |
return htmlspecialchars($this->value, ENT_COMPAT, $this->encoding); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|