|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* SCSSPHP |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright 2012-2018 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
|
|
|
use Leafo\ScssPhp\Exception\RangeException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Utilty functions |
|
19
|
|
|
* |
|
20
|
|
|
* @author Anthon Pang <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Util |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Asserts that `value` falls within `range` (inclusive), leaving |
|
26
|
|
|
* room for slight floating-point errors. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $name The name of the value. Used in the error message. |
|
29
|
|
|
* @param \Leafo\ScssPhp\Base\Range $range Range of values. |
|
30
|
|
|
* @param array $value The value to check. |
|
31
|
|
|
* @param string $unit The unit of the value. Used in error reporting. |
|
32
|
|
|
* |
|
33
|
|
|
* @return mixed `value` adjusted to fall within range, if it was outside by a floating-point margin. |
|
34
|
|
|
* |
|
35
|
|
|
* @throws \Leafo\ScssPhp\Exception\RangeException |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function checkRange($name, Range $range, $value, $unit = '') |
|
38
|
|
|
{ |
|
39
|
|
|
$val = $value[1]; |
|
40
|
|
|
$grace = new Range(-0.00001, 0.00001); |
|
41
|
|
|
|
|
42
|
|
|
if ($range->includes($val)) { |
|
43
|
|
|
return $val; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if ($grace->includes($val - $range->first)) { |
|
47
|
|
|
return $range->first; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($grace->includes($val - $range->last)) { |
|
51
|
|
|
return $range->last; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
throw new RangeException("$name {$val} must be between {$range->first} and {$range->last}$unit"); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Encode URI component |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $string |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function encodeURIComponent($string) |
|
65
|
|
|
{ |
|
66
|
|
|
$revert = array('%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')'); |
|
67
|
|
|
|
|
68
|
|
|
return strtr(rawurlencode($string), $revert); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|