-
Notifications
You must be signed in to change notification settings - Fork 86
Description
First of all, I don't understand why ScssPhp\ValueConverter::fromPhp assumes scalar types are purely from PHP (correct) but array types are potentially Scss values (unexpected for me, bordering to wrong). Especially when one has to look at the code to figure that out.
scssphp/src/ValueConverter.php
Line 65 in 957888e
| 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])) { |
Secondly, the function signature takes in "mixed" type - nowhere does it say anything about objects or arrays not being supported.
Thirdly, this functionality is a very common use case (when it comes to custom variables), so it really should be implemented. 😅
I tried the following approach but it didn't work:
class MyClass {
// ....
private function scssFromPhp(mixed $value): mixed
{
if ($value === null || is_scalar($value)) {
return ScssPhp\ValueConverter::fromPhp($value);
}
if (is_array($value) && array_is_list($value)) {
return [ScssPhp\Type::T_LIST, '', array_map($this->scssFromPhp(...), $value)];
}
if (is_array($value) || $value instanceof stdClass) {
$value = (array)$value;
return [
ScssPhp\Type::T_MAP,
array_map($this->scssFromPhp(...), array_keys($value)),
array_map($this->scssFromPhp(...), array_values($value))
];
}
throw new InvalidArgumentException('...'); // error message should detail exactly where the conversion failed and why
}
}I don't have much knowledge about these types' structure to be sure about that code, would be nice if someone could figure out what's wrong.
Seems the code does work 🤔 maybe it could be added to the implementation though..