Merge branch 'MDL-76525-MOODLE_400_STABLE' of https://github.com/PhMemmel/moodle...
[moodle.git] / lib / scssphp / ValueConverter.php
blobe12a0eb542a48048df6f9fc709e04384cfb473ef
1 <?php
3 /**
4 * SCSSPHP
6 * @copyright 2012-2020 Leaf Corcoran
8 * @license http://opensource.org/licenses/MIT MIT
10 * @link http://scssphp.github.io/scssphp
13 namespace ScssPhp\ScssPhp;
15 use ScssPhp\ScssPhp\Node\Number;
17 final class ValueConverter
19 // Prevent instantiating it
20 private function __construct()
24 /**
25 * Parses a value from a Scss source string.
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.
32 * @param string $source
34 * @return mixed
36 public static function parseValue($source)
38 $parser = new Parser(__CLASS__);
40 if (!$parser->parseValue($source, $value)) {
41 throw new \InvalidArgumentException(sprintf('Invalid value source "%s".', $source));
44 return $value;
47 /**
48 * Converts a PHP value to a Sass value
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.
55 * @param mixed $value
57 * @return mixed
59 public static function fromPhp($value)
61 if ($value instanceof Number) {
62 return $value;
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;
69 if ($value === null) {
70 return Compiler::$null;
73 if ($value === true) {
74 return Compiler::$true;
77 if ($value === false) {
78 return Compiler::$false;
81 if ($value === '') {
82 return Compiler::$emptyString;
85 if (\is_int($value) || \is_float($value)) {
86 return new Number($value, '');
89 if (\is_string($value)) {
90 return [Type::T_STRING, '"', [$value]];
93 throw new \InvalidArgumentException(sprintf('Cannot convert the value of type "%s" to a Sass value.', gettype($value)));