| 1 |
<?php |
| 2 |
/** |
| 3 |
* SCSSPHP |
| 4 |
* |
| 5 |
* @copyright 2012-2015 Leaf Corcoran |
| 6 |
* |
| 7 |
* @license http://opensource.org/licenses/MIT MIT |
| 8 |
* |
| 9 |
* @link http://leafo.github.io/scssphp |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Leafo\ScssPhp\Node; |
| 13 |
|
| 14 |
use Leafo\ScssPhp\Node; |
| 15 |
use Leafo\ScssPhp\Type; |
| 16 |
|
| 17 |
/** |
| 18 |
* SCSS dimension + optional units |
| 19 |
* |
| 20 |
* {@internal |
| 21 |
* This is a work-in-progress. |
| 22 |
* |
| 23 |
* The \ArrayAccess interface is temporary until the migration is complete. |
| 24 |
* }} |
| 25 |
* |
| 26 |
* @author Anthon Pang <anthon.pang@gmail.com> |
| 27 |
*/ |
| 28 |
class Number extends Node implements \ArrayAccess |
| 29 |
{ |
| 30 |
/** |
| 31 |
* @var integer |
| 32 |
*/ |
| 33 |
static public $precision = 5; |
| 34 |
|
| 35 |
/** |
| 36 |
* @see http://www.w3.org/TR/2012/WD-css3-values-20120308/ |
| 37 |
* |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
static protected $unitTable = array( |
| 41 |
'in' => array( |
| 42 |
'in' => 1, |
| 43 |
'pc' => 6, |
| 44 |
'pt' => 72, |
| 45 |
'px' => 96, |
| 46 |
'cm' => 2.54, |
| 47 |
'mm' => 25.4, |
| 48 |
'q' => 101.6, |
| 49 |
), |
| 50 |
'turn' => array( |
| 51 |
'deg' => 180, |
| 52 |
'grad' => 200, |
| 53 |
'rad' => M_PI, |
| 54 |
'turn' => 0.5, |
| 55 |
), |
| 56 |
's' => array( |
| 57 |
's' => 1, |
| 58 |
'ms' => 1000, |
| 59 |
), |
| 60 |
'Hz' => array( |
| 61 |
'Hz' => 1, |
| 62 |
'kHz' => 0.001, |
| 63 |
), |
| 64 |
'dpi' => array( |
| 65 |
'dpi' => 1, |
| 66 |
'dpcm' => 2.54, |
| 67 |
'dppx' => 96, |
| 68 |
), |
| 69 |
); |
| 70 |
|
| 71 |
/** |
| 72 |
* @var integer|float |
| 73 |
*/ |
| 74 |
public $dimension; |
| 75 |
|
| 76 |
/** |
| 77 |
* @var string |
| 78 |
*/ |
| 79 |
public $units; |
| 80 |
|
| 81 |
/** |
| 82 |
* Initialize number |
| 83 |
* |
| 84 |
* @param mixed $dimension |
| 85 |
* @param string $initialUnit |
| 86 |
*/ |
| 87 |
public function __construct($dimension, $initialUnit) |
| 88 |
{ |
| 89 |
$this->type = Type::T_NUMBER; |
| 90 |
$this->dimension = $dimension; |
| 91 |
$this->units = $initialUnit; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Coerce number to target units |
| 96 |
* |
| 97 |
* @param array $units |
| 98 |
* |
| 99 |
* @return \Leafo\ScssPhp\Node\Number |
| 100 |
*/ |
| 101 |
public function coerce($units) |
| 102 |
{ |
| 103 |
$value = $this->dimension; |
| 104 |
|
| 105 |
if (isset(self::$unitTable[$this->units][$units])) { |
| 106 |
$value *= self::$unitTable[$this->units][$units]; |
| 107 |
} |
| 108 |
|
| 109 |
return new Number($value, $units); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Normalize number |
| 114 |
* |
| 115 |
* @return \Leafo\ScssPhp\Node\Number |
| 116 |
*/ |
| 117 |
public function normalize() |
| 118 |
{ |
| 119 |
if (isset(self::$unitTable['in'][$this->units])) { |
| 120 |
$conv = self::$unitTable['in'][$this->units]; |
| 121 |
|
| 122 |
return new Number($this->dimension / $conv, 'in'); |
| 123 |
} |
| 124 |
|
| 125 |
return new Number($this->dimension, $this->units); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* {@inheritdoc} |
| 130 |
*/ |
| 131 |
public function offsetExists($offset) |
| 132 |
{ |
| 133 |
if ($offset === -2) { |
| 134 |
return $sourceIndex !== null; |
| 135 |
} |
| 136 |
|
| 137 |
if ($offset === -1 |
| 138 |
|| $offset === 0 |
| 139 |
|| $offset === 1 |
| 140 |
|| $offset === 2 |
| 141 |
) { |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* {@inheritdoc} |
| 150 |
*/ |
| 151 |
public function offsetGet($offset) |
| 152 |
{ |
| 153 |
switch ($offset) { |
| 154 |
case -2: |
| 155 |
return $this->sourceIndex; |
| 156 |
|
| 157 |
case -1: |
| 158 |
return $this->sourcePosition; |
| 159 |
|
| 160 |
case 0: |
| 161 |
return $this->type; |
| 162 |
|
| 163 |
case 1: |
| 164 |
return $this->dimension; |
| 165 |
|
| 166 |
case 2: |
| 167 |
return $this->units; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* {@inheritdoc} |
| 173 |
*/ |
| 174 |
public function offsetSet($offset, $value) |
| 175 |
{ |
| 176 |
if ($offset === 1) { |
| 177 |
$this->dimension = $value; |
| 178 |
} elseif ($offset === 2) { |
| 179 |
$this->units = $value; |
| 180 |
} elseif ($offset == -1) { |
| 181 |
$this->sourcePosition = $value; |
| 182 |
} elseif ($offset == -2) { |
| 183 |
$this->sourceIndex = $value; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* {@inheritdoc} |
| 189 |
*/ |
| 190 |
public function offsetUnset($offset) |
| 191 |
{ |
| 192 |
if ($offset === 1) { |
| 193 |
$this->dimension = null; |
| 194 |
} elseif ($offset === 2) { |
| 195 |
$this->units = null; |
| 196 |
} elseif ($offset === -1) { |
| 197 |
$this->sourcePosition = null; |
| 198 |
} elseif ($offset === -2) { |
| 199 |
$this->sourceIndex = null; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Returns true if the number is unitless |
| 205 |
* |
| 206 |
* @return boolean |
| 207 |
*/ |
| 208 |
public function unitless() |
| 209 |
{ |
| 210 |
return empty($this->units); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Returns unit(s) as the product of numerator units divided by the product of denominator units |
| 215 |
* |
| 216 |
* @return string |
| 217 |
*/ |
| 218 |
public function unitStr() |
| 219 |
{ |
| 220 |
return $this->units; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* {@inheritdoc} |
| 225 |
*/ |
| 226 |
public function __toString() |
| 227 |
{ |
| 228 |
$value = round($this->dimension, self::$precision); |
| 229 |
|
| 230 |
if (empty($this->units)) { |
| 231 |
return (string) $value; |
| 232 |
} |
| 233 |
|
| 234 |
return (string) $value . $this->units; |
| 235 |
} |
| 236 |
} |
| 237 |
|