Skip to content

Custom variables: list or map from php #641

@uuf6429

Description

@uuf6429

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.

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..

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions