Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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 <anthon.pang@gmail.com>
  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, but not on PHP 7.2 as mb_chr(0) is buggy there
  86          if (\PHP_VERSION_ID > 70300 && \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      /**
 105       * mb_strlen() wrapper
 106       *
 107       * @param string $string
 108       * @return int
 109       */
 110      public static function mbStrlen($string)
 111      {
 112          // Use the native implementation if available.
 113          if (\function_exists('mb_strlen')) {
 114              return mb_strlen($string, 'UTF-8');
 115          }
 116  
 117          if (\function_exists('iconv_strlen')) {
 118              return @iconv_strlen($string, 'UTF-8');
 119          }
 120  
 121          return strlen($string);
 122      }
 123  
 124      /**
 125       * mb_substr() wrapper
 126       * @param string $string
 127       * @param int $start
 128       * @param null|int $length
 129       * @return string
 130       */
 131      public static function mbSubstr($string, $start, $length = null)
 132      {
 133          // Use the native implementation if available.
 134          if (\function_exists('mb_substr')) {
 135              return mb_substr($string, $start, $length, 'UTF-8');
 136          }
 137  
 138          if (\function_exists('iconv_substr')) {
 139              if ($start < 0) {
 140                  $start = static::mbStrlen($string) + $start;
 141                  if ($start < 0) {
 142                      $start = 0;
 143                  }
 144              }
 145  
 146              if (null === $length) {
 147                  $length = 2147483647;
 148              } elseif ($length < 0) {
 149                  $length = static::mbStrlen($string) + $length - $start;
 150                  if ($length < 0) {
 151                      return '';
 152                  }
 153              }
 154  
 155              return (string)iconv_substr($string, $start, $length, 'UTF-8');
 156          }
 157  
 158          return substr($string, $start, $length);
 159      }
 160  
 161      /**
 162       * mb_strpos wrapper
 163       * @param string $haystack
 164       * @param string $needle
 165       * @param int $offset
 166       *
 167       * @return int|false
 168       */
 169      public static function mbStrpos($haystack, $needle, $offset = 0)
 170      {
 171          if (\function_exists('mb_strpos')) {
 172              return mb_strpos($haystack, $needle, $offset, 'UTF-8');
 173          }
 174  
 175          if (\function_exists('iconv_strpos')) {
 176              return iconv_strpos($haystack, $needle, $offset, 'UTF-8');
 177          }
 178  
 179          return strpos($haystack, $needle, $offset);
 180      }
 181  }