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()
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
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));
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.
59 public static function fromPhp($value)
61 if ($value instanceof Number
) {
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
])) {
69 if ($value === null) {
70 return Compiler
::$null;
73 if ($value === true) {
74 return Compiler
::$true;
77 if ($value === false) {
78 return Compiler
::$false;
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)));