MatthiasMullie\Minify\JS::propertyNotation PHP Method

propertyNotation() protected method

Replaces all occurrences of array['key'] by array.key.
protected propertyNotation ( string $content ) : string
$content string
return string
    protected function propertyNotation($content)
    {
        // PHP only supports $this inside anonymous functions since 5.4
        $minifier = $this;
        $keywords = $this->keywordsReserved;
        $callback = function ($match) use($minifier, $keywords) {
            $property = trim($minifier->extracted[$match[1]], '\'"');
            /*
             * Check if the property is a reserved keyword. In this context (as
             * property of an object literal/array) it shouldn't matter, but IE8
             * freaks out with "Expected identifier".
             */
            if (in_array($property, $keywords)) {
                return $match[0];
            }
            /*
             * See if the property is in a variable-like format (e.g.
             * array['key-here'] can't be replaced by array.key-here since '-'
             * is not a valid character there.
             */
            if (!preg_match('/^' . $minifier::REGEX_VARIABLE . '$/u', $property)) {
                return $match[0];
            }
            return '.' . $property;
        };
        /*
         * Figure out if previous character is a variable name (of the array
         * we want to use property notation on) - this is to make sure
         * standalone ['value'] arrays aren't confused for keys-of-an-array.
         * We can (and only have to) check the last character, because PHP's
         * regex implementation doesn't allow unfixed-length look-behind
         * assertions.
         */
        preg_match('/(\\[[^\\]]+\\])[^\\]]*$/', static::REGEX_VARIABLE, $previousChar);
        $previousChar = $previousChar[1];
        /*
         * Make sure word preceding the ['value'] is not a keyword, e.g.
         * return['x']. Because -again- PHP's regex implementation doesn't allow
         * unfixed-length look-behind assertions, I'm just going to do a lot of
         * separate look-behind assertions, one for each keyword.
         */
        $keywords = $this->getKeywordsForRegex($keywords);
        $keywords = '(?<!' . implode(')(?<!', $keywords) . ')';
        return preg_replace_callback('/(?<=' . $previousChar . '|\\])' . $keywords . '\\[\\s*(([\'"])[0-9]+\\2)\\s*\\]/u', $callback, $content);
    }