| 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; |
| 13 |
|
| 14 |
use Leafo\ScssPhp\Base\Range; |
| 15 |
|
| 16 |
/** |
| 17 |
* SCSS utilties |
| 18 |
* |
| 19 |
* @author Anthon Pang <anthon.pang@gmail.com> |
| 20 |
*/ |
| 21 |
class Util |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Asserts that `value` falls within `range` (inclusive), leaving |
| 25 |
* room for slight floating-point errors. |
| 26 |
* |
| 27 |
* @param string $name The name of the value. Used in the error message. |
| 28 |
* @param Range $range Range of values. |
| 29 |
* @param array $value The value to check. |
| 30 |
* @param string $unit The unit of the value. Used in error reporting. |
| 31 |
* |
| 32 |
* @return mixed `value` adjusted to fall within range, if it was outside by a floating-point margin. |
| 33 |
* |
| 34 |
* @throws \Exception |
| 35 |
*/ |
| 36 |
public static function checkRange($name, Range $range, $value, $unit = '') |
| 37 |
{ |
| 38 |
$val = $value[1]; |
| 39 |
$grace = new Range(-0.00001, 0.00001); |
| 40 |
|
| 41 |
if ($range->includes($val)) { |
| 42 |
return $val; |
| 43 |
} |
| 44 |
|
| 45 |
if ($grace->includes($val - $range->first)) { |
| 46 |
return $range->first; |
| 47 |
} |
| 48 |
|
| 49 |
if ($grace->includes($val - $range->last)) { |
| 50 |
return $range->last; |
| 51 |
} |
| 52 |
|
| 53 |
throw new \Exception("$name {$val} must be between {$range->first} and {$range->last}$unit"); |
| 54 |
} |
| 55 |
} |
| 56 |
|