|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* SCSSPHP |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright 2012-2020 Leaf Corcoran |
|
7
|
|
|
* |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://scssphp.github.io/scssphp |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace ScssPhp\ScssPhp; |
|
14
|
|
|
|
|
15
|
|
|
use ScssPhp\ScssPhp\Base\Range; |
|
16
|
|
|
use ScssPhp\ScssPhp\Exception\RangeException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Utilty functions |
|
20
|
|
|
* |
|
21
|
|
|
* @author Anthon Pang <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Util |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Asserts that `value` falls within `range` (inclusive), leaving |
|
27
|
|
|
* room for slight floating-point errors. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $name The name of the value. Used in the error message. |
|
30
|
|
|
* @param \ScssPhp\ScssPhp\Base\Range $range Range of values. |
|
31
|
|
|
* @param array $value The value to check. |
|
32
|
|
|
* @param string $unit The unit of the value. Used in error reporting. |
|
33
|
|
|
* |
|
34
|
|
|
* @return mixed `value` adjusted to fall within range, if it was outside by a floating-point margin. |
|
35
|
|
|
* |
|
36
|
|
|
* @throws \ScssPhp\ScssPhp\Exception\RangeException |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function checkRange($name, Range $range, $value, $unit = '') |
|
39
|
|
|
{ |
|
40
|
|
|
$val = $value[1]; |
|
41
|
|
|
$grace = new Range(-0.00001, 0.00001); |
|
42
|
|
|
|
|
43
|
|
|
if (! \is_numeric($val)) { |
|
44
|
|
|
throw new RangeException("$name {$val} is not a number."); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($range->includes($val)) { |
|
48
|
|
|
return $val; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($grace->includes($val - $range->first)) { |
|
52
|
|
|
return $range->first; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($grace->includes($val - $range->last)) { |
|
56
|
|
|
return $range->last; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
throw new RangeException("$name {$val} must be between {$range->first} and {$range->last}$unit"); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Encode URI component |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $string |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function encodeURIComponent($string) |
|
70
|
|
|
{ |
|
71
|
|
|
$revert = ['%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')']; |
|
72
|
|
|
|
|
73
|
|
|
return strtr(rawurlencode($string), $revert); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* mb_chr() wrapper |
|
78
|
|
|
* |
|
79
|
|
|
* @param integer $code |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function mbChr($code) |
|
84
|
|
|
{ |
|
85
|
|
|
// Use the native implementation if available. |
|
86
|
|
|
if (\function_exists('mb_chr')) { |
|
87
|
|
|
return mb_chr($code, 'UTF-8'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (0x80 > $code %= 0x200000) { |
|
91
|
|
|
$s = \chr($code); |
|
92
|
|
|
} elseif (0x800 > $code) { |
|
93
|
|
|
$s = \chr(0xC0 | $code >> 6) . \chr(0x80 | $code & 0x3F); |
|
94
|
|
|
} elseif (0x10000 > $code) { |
|
95
|
|
|
$s = \chr(0xE0 | $code >> 12) . \chr(0x80 | $code >> 6 & 0x3F) . \chr(0x80 | $code & 0x3F); |
|
96
|
|
|
} else { |
|
97
|
|
|
$s = \chr(0xF0 | $code >> 18) . \chr(0x80 | $code >> 12 & 0x3F) |
|
98
|
|
|
. \chr(0x80 | $code >> 6 & 0x3F) . \chr(0x80 | $code & 0x3F); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $s; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|