Automattic\WooCommerce\Vendor\Symfony\Component\CssSelector\Parser

Parser::parseSeriespublic staticWC 1.0

Parses the arguments for ":nth-child()" and friends.

Method of the class: Parser{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = Parser::parseSeries( $tokens ): array;
$tokens(Token[]) (required)
.

Parser::parseSeries() code WC 10.5.0

public static function parseSeries(array $tokens): array
{
    foreach ($tokens as $token) {
        if ($token->isString()) {
            throw SyntaxErrorException::stringAsFunctionArgument();
        }
    }

    $joined = trim(implode('', array_map(function (Token $token) {
        return $token->getValue();
    }, $tokens)));

    $int = function ($string) {
        if (!is_numeric($string)) {
            throw SyntaxErrorException::stringAsFunctionArgument();
        }

        return (int) $string;
    };

    switch (true) {
        case 'odd' === $joined:
            return [2, 1];
        case 'even' === $joined:
            return [2, 0];
        case 'n' === $joined:
            return [1, 0];
        case !str_contains($joined, 'n'):
            return [0, $int($joined)];
    }

    $split = explode('n', $joined);
    $first = $split[0] ?? null;

    return [
        $first ? ('-' === $first || '+' === $first ? $int($first.'1') : $int($first)) : 1,
        isset($split[1]) && $split[1] ? $int($split[1]) : 0,
    ];
}