Passed
Push — master ( 36baab...67c376 )
by Chris
04:26
created

Util   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 5
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