| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2019-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 |
/** |
| 25 |
* Class for outputting numbers |
| 26 |
*/ |
| 27 |
class Number { |
| 28 |
public $value = 0; |
| 29 |
public $units = ''; |
| 30 |
public $units_before = ''; |
| 31 |
public $precision = 0; |
| 32 |
private $as_string = ''; |
| 33 |
private static $default_precision = 5; |
| 34 |
private static $decimal_separator = '.'; |
| 35 |
private static $thousands_separator = ','; |
| 36 |
|
| 37 |
public function __construct($value, $units = '', $units_before = '') |
| 38 |
{ |
| 39 |
if(is_object($value) && get_class($value) === 'Goat1000\\SVGGraph\\Number') { |
| 40 |
$this->value = $value->value; |
| 41 |
$this->units = $value->units; |
| 42 |
$this->units_before = $value->units_before; |
| 43 |
$this->as_string = $value->as_string; |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
if(!is_numeric($value)) |
| 48 |
throw new \Exception($value . ' is not a number'); |
| 49 |
$this->value = $value; |
| 50 |
$this->units = $units; |
| 51 |
$this->units_before = $units_before; |
| 52 |
$this->as_string = ''; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Output for SVG values |
| 57 |
*/ |
| 58 |
public function __toString() |
| 59 |
{ |
| 60 |
if($this->as_string !== '') |
| 61 |
return $this->as_string; |
| 62 |
|
| 63 |
$value = $this->value; |
| 64 |
if($value == 0) { |
| 65 |
$value = '0'; |
| 66 |
} elseif($value == 1) { |
| 67 |
$value = '1'; |
| 68 |
} elseif(is_int($value) || $value >= 1000 || $value <= -1000) { |
| 69 |
$value = sprintf('%d', $value); |
| 70 |
} else { |
| 71 |
if($this->precision) |
| 72 |
$value = sprintf('%.' . $this->precision . 'F', $value); |
| 73 |
else |
| 74 |
$value = sprintf('%.2F', $value); |
| 75 |
$value = rtrim($value, '0'); |
| 76 |
$value = rtrim($value, '.'); |
| 77 |
} |
| 78 |
$this->as_string = $value . $this->units; |
| 79 |
return $this->as_string; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Sets the formatted string options |
| 84 |
*/ |
| 85 |
public static function setup($precision, $decimal, $thousands) |
| 86 |
{ |
| 87 |
if($decimal === $thousands) |
| 88 |
throw new \LogicException('Decimal and thousands separators using same value. Please use different settings for "thousands" and "decimal".'); |
| 89 |
|
| 90 |
Number::$default_precision = $precision; |
| 91 |
Number::$decimal_separator = $decimal; |
| 92 |
Number::$thousands_separator = $thousands; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Formatted output |
| 97 |
*/ |
| 98 |
public function format($decimals = null, $precision = null) |
| 99 |
{ |
| 100 |
$n = $this->value; |
| 101 |
$d = ($decimals === null ? 0 : $decimals); |
| 102 |
|
| 103 |
if(!is_int($n)) { |
| 104 |
if($precision === null) |
| 105 |
$precision = Number::$default_precision; |
| 106 |
|
| 107 |
// if there are too many zeroes before other digits, round to 0 |
| 108 |
$e = floor(log(abs($n), 10)); |
| 109 |
if(-$e > $precision) |
| 110 |
$n = 0; |
| 111 |
|
| 112 |
// subtract number of digits before decimal point from precision |
| 113 |
// for precision-based decimals |
| 114 |
if($decimals === null) |
| 115 |
$d = $precision - ($e > 0 ? $e : 0); |
| 116 |
} |
| 117 |
$s = number_format($n, $d, Number::$decimal_separator, |
| 118 |
Number::$thousands_separator); |
| 119 |
|
| 120 |
if($decimals === null && $d && |
| 121 |
strpos($s, Number::$decimal_separator) !== false) { |
| 122 |
list($a, $b) = explode(Number::$decimal_separator, $s); |
| 123 |
$b1 = rtrim($b, '0'); |
| 124 |
$s = $b1 != '' ? $a . Number::$decimal_separator . $b1 : $a; |
| 125 |
} |
| 126 |
return $this->units_before . $s . $this->units; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Converts a string with units to a value in SVG user units |
| 131 |
*/ |
| 132 |
public static function units($value) |
| 133 |
{ |
| 134 |
if(is_numeric($value) || $value === null) |
| 135 |
return $value; |
| 136 |
if(!is_string($value)) |
| 137 |
throw new \InvalidArgumentException("Unit value not a string"); |
| 138 |
|
| 139 |
if(!preg_match('/^([0-9.]+)(px|in|cm|mm|pt|pc)$/', $value, $parts)) |
| 140 |
throw new \InvalidArgumentException("Unit value {$value} not in supported format"); |
| 141 |
|
| 142 |
$count = (float)$parts[1]; |
| 143 |
$units = $parts[2]; |
| 144 |
$umap = [ |
| 145 |
'px' => 1.0, 'in' => 96.0, 'cm' => 37.795, 'mm' => 3.7795, 'pt' => 1.3333, 'pc' => 16.0 |
| 146 |
]; |
| 147 |
return $count * $umap[$units]; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|