LightnCandy\Compiler::getVariableName PHP Method

getVariableName() protected static method

Get string presentation of a variable
protected static getVariableName ( arraystring | integer> &$context, arraystring | integer> $var, array | null $lookup = null, $args = null ) : array
$context arraystring | integer>
$var arraystring | integer>
$lookup array | null
return array
    protected static function getVariableName(&$context, $var, $lookup = null, $args = null)
    {
        if (isset($var[0]) && $var[0] === Parser::LITERAL) {
            if ($var[1] === "undefined") {
                $var[1] = "null";
            }
            return array($var[1], preg_replace('/\'(.*)\'/', '$1', $var[1]));
        }
        list($levels, $spvar, $var) = Expression::analyze($context, $var);
        $exp = Expression::toString($levels, $spvar, $var);
        $base = $spvar ? "\$cx['sp_vars']" : '$in';
        // change base when trace to parent
        if ($levels > 0) {
            if ($spvar) {
                $base .= str_repeat("['_parent']", $levels);
            } else {
                $base = "\$cx['scopes'][count(\$cx['scopes'])-{$levels}]";
            }
        }
        if ((count($var) == 0 || $var[0] === null && count($var) == 1) && $lookup === null) {
            return array($base, $exp);
        }
        if (count($var) > 0 && $var[0] === null) {
            array_shift($var);
        }
        // To support recursive context lookup, instance properties + methods and lambdas
        // the only way is using slower rendering time variable resolver.
        if ($context['flags']['prop'] || $context['flags']['method'] || $context['flags']['mustlok'] || $context['flags']['mustlam'] || $context['flags']['lambda']) {
            $L = $lookup ? ", {$lookup['0']}" : '';
            $A = $args ? ",{$args['0']}" : '';
            $E = $args ? ' ' . implode(' ', $args[1]) : '';
            return array(static::getFuncName($context, 'v', $exp) . "\$cx, \$in, isset({$base}) ? {$base} : null, array(" . Expression::listString($var) . "{$L}){$A})", $lookup ? "lookup {$exp} {$lookup['1']}" : "{$exp}{$E}");
        }
        $n = Expression::arrayString($var);
        $k = array_pop($var);
        $L = $lookup ? "[{$lookup[0]}]" : '';
        $p = $lookup ? $n : (count($var) ? Expression::arrayString($var) : '');
        $checks = array();
        if ($levels > 0) {
            $checks[] = "isset({$base})";
        }
        if (!$spvar) {
            if ($levels === 0 && $p) {
                $checks[] = "isset({$base}{$p})";
            }
            $checks[] = "is_array({$base}{$p})";
        }
        $checks[] = "isset({$base}{$n}{$L})";
        $check = (count($checks) > 1 ? '(' : '') . implode(' && ', $checks) . (count($checks) > 1 ? ')' : '');
        $lenStart = '';
        $lenEnd = '';
        if ($context['flags']['jslen']) {
            if ($lookup === null && $k === 'length') {
                array_pop($checks);
                $lenStart = '(' . (count($checks) > 1 ? '(' : '') . implode(' && ', $checks) . (count($checks) > 1 ? ')' : '') . " ? count({$base}" . Expression::arrayString($var) . ') : ';
                $lenEnd = ')';
            }
        }
        return array("({$check} ? {$base}{$n}{$L} : {$lenStart}" . ($context['flags']['debug'] ? static::getFuncName($context, 'miss', '') . "\$cx, '{$exp}')" : 'null') . "){$lenEnd}", $lookup ? "lookup {$exp} {$lookup['1']}" : $exp);
    }