scssphp represents Sass values using the \ScssPhp\ScssPhp\Value\Value
class and its subclasses.
The Value class exposes various assert* methods to check the type of
values. These methods return the value refined to the proper type or throw a
SassScriptException about the unexpected type. Those methods accept a name
argument (without the leading $) in case the asserted value correspond to
an argument of a Sass function, to provide a better error message.
The scssphp value API is strongly typed. It is recommended to use static analysis on your code when using it to catch invalid usages.
scssphp used to represent Sass values using array|\ScssPhp\ScssPhp\Node\Number.
The index 0 of the array will be the type of value (one of the
\ScssPhp\ScssPhp\Type constants).
[!NOTE] The legacy value representation is deprecated. Migrate to the modern representation instead.
The Compiler class exposes various assert* methods to check the type of
values. These methods return the asserted value (which may not be exactly the
same as the input due to complex implementation details, so the return value
should be used).
If a string representation of a value is needed to include it in an error
message, the Compiler::compileValue method can be used.
Sass’ null value is representation using [Type::T_NULL]. It can be
referenced as Compiler::$null.
Sass’ boolean values are represented as Compiler::$true and Compiler::$false.
When accepting a parameter in a function, it is generally better to use
Compiler::isTruthy instead of explicitly checking for boolean value.
Sass’ numbers are represented using the \ScssPhp\ScssPhp\Node\Number class.
Sass’ strings are represented as an array with 3 elements:
Type::T_STRING" or '). When creating new quoted strings, use the "
character (the rendering will not always respect this quoting character anyway).Compiler::getStringText
to get the text of the string. When creating new strings, put the string
content as the single item in the array.Note: arguments received by functions may not always use the Type::T_STRING
due to internal details. However, Compiler::assertString guarantees that its
return value is actually a string using Type::T_STRING.
Sass’ colors are represented as an array with 4 or 5 elements (the alpha channel is optional):
Type::T_COLORNote: arguments might receive a Type::T_KEYWORD corresponding to a color
keyword. Always use Compiler::assertColor to get the actual color.
Sass’ maps are represented as an array with 3 elements:
Type::T_MAPThe actual handling of keys is weird, as it does not rely on the equality rules of values (which is not spec compliant). When writing custom functions, it is advised to stay away from maps as arguments or return value for now.
As Sass empty lists can be used as maps, always use Compiler::assertMap to get
the actual map value (this helper takes care of converting empty lists).
Sass’ lists are represented as an array with 3 elements:
Type::T_LISTBracketed lists are represented by adding an enclosing key with a value of
bracket.
Note that the handling of the list separator is not fully compliant. List values might have an undecided separators in cases where they should not in Sass.
Sass’s argument lists are representing the list of rest arguments for variadic functions.
They are represented as a Sass list (see above) containing positional arguments
with an additional element in the array to store the keyword arguments. To
access keyword arguments of the Sass argument list, use
Compiler::getArgumentListKeywords.