Drest\Query\ExposeFields::parseStringParts PHP Method

parseStringParts() private method

Get information on parsed (top-level) brackets
private parseStringParts ( string $string ) : stdClass
$string string
return stdClass $information contains parse information object containing a $parts array eg array( 'openBracket' => xx, - The position of the open bracket 'closeBracket' => xx - The position of the close bracket 'contents' => xx - The contents of the bracket 'tagName' => xx - The name of the accompanying tag )
    private function parseStringParts($string)
    {
        $information = new \stdClass();
        $information->parts = [];
        $openPos = null;
        $closePos = null;
        $bracketCounter = 0;
        foreach (str_split($string) as $key => $char) {
            if ($char === '[') {
                if (is_null($openPos) && $bracketCounter === 0) {
                    $openPos = $key;
                }
                $bracketCounter++;
            }
            if ($char === ']') {
                if (is_null($closePos) && $bracketCounter === 1) {
                    $closePos = $key;
                }
                $bracketCounter--;
            }
            if (is_numeric($openPos) && is_numeric($closePos)) {
                // Work backwards from openPos until we hit [|]
                $stopPos = 0;
                foreach (['|', '[', ']', '%'] as $stopChar) {
                    if (($pos = strrpos(substr($string, 0, $openPos), $stopChar)) !== false) {
                        $stopPos = ++$pos > $stopPos ? $pos : $stopPos;
                    }
                }
                if ($openPos + 1 === $closePos) {
                    // Where no offset has been defined, blank out the [] characters
                    $rangeSize = $closePos - $openPos + 1;
                    $string = substr_replace($string, str_repeat('%', $rangeSize), $openPos, $rangeSize);
                } else {
                    $information->parts[] = ['openBracket' => $openPos, 'closeBracket' => $closePos, 'contents' => substr($string, $openPos + 1, $closePos - $openPos - 1), 'tagName' => substr($string, $stopPos, $openPos - $stopPos), 'tagStart' => $stopPos, 'tagEnd' => $openPos - 1];
                    $rangeSize = $closePos - $stopPos + 1;
                    $string = substr_replace($string, str_repeat('%', $rangeSize), $stopPos, $rangeSize);
                }
                $openPos = $closePos = null;
            }
        }
        $string = str_replace('%', '', $string);
        $string = str_replace('||', '|', $string);
        $information->remaining_string = trim($string, '|');
        return $information;
    }