MDL-66397 filter_h5p: converts H5P URLs to embed code
[moodle.git] / lib / scssphp / Util.php
blobb0b76fdb42eaa9e42b8d3326234455e61f52ce6c
1 <?php
2 /**
3 * SCSSPHP
5 * @copyright 2012-2019 Leaf Corcoran
7 * @license http://opensource.org/licenses/MIT MIT
9 * @link http://scssphp.github.io/scssphp
12 namespace ScssPhp\ScssPhp;
14 use ScssPhp\ScssPhp\Base\Range;
15 use ScssPhp\ScssPhp\Exception\RangeException;
17 /**
18 * Utilty functions
20 * @author Anthon Pang <anthon.pang@gmail.com>
22 class Util
24 /**
25 * Asserts that `value` falls within `range` (inclusive), leaving
26 * room for slight floating-point errors.
28 * @param string $name The name of the value. Used in the error message.
29 * @param \ScssPhp\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.
33 * @return mixed `value` adjusted to fall within range, if it was outside by a floating-point margin.
35 * @throws \ScssPhp\ScssPhp\Exception\RangeException
37 public static function checkRange($name, Range $range, $value, $unit = '')
39 $val = $value[1];
40 $grace = new Range(-0.00001, 0.00001);
42 if ($range->includes($val)) {
43 return $val;
46 if ($grace->includes($val - $range->first)) {
47 return $range->first;
50 if ($grace->includes($val - $range->last)) {
51 return $range->last;
54 throw new RangeException("$name {$val} must be between {$range->first} and {$range->last}$unit");
57 /**
58 * Encode URI component
60 * @param string $string
62 * @return string
64 public static function encodeURIComponent($string)
66 $revert = ['%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')'];
68 return strtr(rawurlencode($string), $revert);