Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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\Node\Number;
  16  
  17  final class ValueConverter
  18  {
  19      // Prevent instantiating it
  20      private function __construct()
  21      {
  22      }
  23  
  24      /**
  25       * Parses a value from a Scss source string.
  26       *
  27       * The returned value is guaranteed to be supported by the
  28       * Compiler methods for registering custom variables. No other
  29       * guarantee about it is provided. It should be considered
  30       * opaque values by the caller.
  31       *
  32       * @param string $source
  33       *
  34       * @return mixed
  35       */
  36      public static function parseValue($source)
  37      {
  38          $parser = new Parser(__CLASS__);
  39  
  40          if (!$parser->parseValue($source, $value)) {
  41              throw new \InvalidArgumentException(sprintf('Invalid value source "%s".', $source));
  42          }
  43  
  44          return $value;
  45      }
  46  
  47      /**
  48       * Converts a PHP value to a Sass value
  49       *
  50       * The returned value is guaranteed to be supported by the
  51       * Compiler methods for registering custom variables. No other
  52       * guarantee about it is provided. It should be considered
  53       * opaque values by the caller.
  54       *
  55       * @param mixed $value
  56       *
  57       * @return mixed
  58       */
  59      public static function fromPhp($value)
  60      {
  61          if ($value instanceof Number) {
  62              return $value;
  63          }
  64  
  65          if (is_array($value) && isset($value[0]) && \in_array($value[0], [Type::T_NULL, Type::T_COLOR, Type::T_KEYWORD, Type::T_LIST, Type::T_MAP, Type::T_STRING])) {
  66              return $value;
  67          }
  68  
  69          if ($value === null) {
  70              return Compiler::$null;
  71          }
  72  
  73          if ($value === true) {
  74              return Compiler::$true;
  75          }
  76  
  77          if ($value === false) {
  78              return Compiler::$false;
  79          }
  80  
  81          if ($value === '') {
  82              return Compiler::$emptyString;
  83          }
  84  
  85          if (\is_int($value) || \is_float($value)) {
  86              return new Number($value, '');
  87          }
  88  
  89          if (\is_string($value)) {
  90              return [Type::T_STRING, '"', [$value]];
  91          }
  92  
  93          throw new \InvalidArgumentException(sprintf('Cannot convert the value of type "%s" to a Sass value.', gettype($value)));
  94      }
  95  }