Filter/AbstractFieldsFilter.php
protected function parse($filterString)
    {
        $length = strlen($filterString);
        if ($length == 0 || preg_match('/[^\w\[\],]+/', $filterString)) {
            return null;
        }

        $start = null;
        $current = [];
        $stack = [];
        $parent = [];
        $currentElement = null;

        for ($position = 0; $position < $length; $position++) {
            //Extracting field when encountering field separators
            if (in_array($filterString[$position], ['[', ']', ','])) {
                if ($start !== null) {
                    $currentElement = substr($filterString, $start, $position - $start);
                    $current[$currentElement] = 1;
                }
                $start = null;
            }
            switch ($filterString[$position]) {
                case '[':
                    array_push($parent, $currentElement);
                    // push current field in stack and initialize current
                    array_push($stack, $current);
                    $current = [];
                    break;

                case ']':
                    //cache current
                    $temp = $current;
                    //Initialize with previous
                    $current = array_pop($stack);
                    //Add from cache
                    $current[array_pop($parent)] = $temp;
                    break;

                //Do nothing on comma. On the next iteration field will be extracted
                case ',':
                    break;

                default:
                    //Move position if no field separators found
                    if ($start === null) {
                        $start = $position;
                    }
            }
        }
        //Check for wrongly formatted filter
        if (!empty($stack)) {
            return null;
        }
        //Check if there's any field remaining that's not added to response
        if ($start !== null) {
            $currentElement = substr($filterString, $start, $position - $start);
            $current[$currentElement] = 1;
        }
        return $current;
    }
protected function parse($filterString)
    {
        $length = strlen($filterString);
        if ($length == 0 || preg_match('/[^\w\[\],]+/', $filterString)) {
            return null;
        }

        $start = null;
        $current = [];
        $stack = [];
        $parent = [];
        $currentElement = null;

        for ($position = 0; $position < $length; $position++) {
            //Extracting field when encountering field separators
            if (in_array($filterString[$position], ['[', ']', ','])) {
                if ($start !== null) {
                    $currentElement = substr($filterString, $start, $position - $start);
                    $current[$currentElement] = 1;
                }
                $start = null;
            }
            switch ($filterString[$position]) {
                case '[':
                    array_push($parent, $currentElement);
                    // push current field in stack and initialize current
                    array_push($stack, $current);
                    $current = [];
                    break;

                case ']':
                    //cache current
                    $temp = $current;
                    //Initialize with previous
                    $current = array_pop($stack);
                    //Add from cache
                    $current[array_pop($parent)] = $temp;
                    break;

                //Do nothing on comma. On the next iteration field will be extracted
                case ',':
                    break;

                default:
                    //Move position if no field separators found
                    if ($start === null) {
                        $start = $position;
                    }
            }
        }
        //Check for wrongly formatted filter
        if (!empty($stack)) {
            return null;
        }
        //Check if there's any field remaining that's not added to response
        if ($start !== null) {
            $currentElement = substr($filterString, $start, $position - $start);
            $current[$currentElement] = 1;
        }
        return $current;
    }
protected function parse ( $ filterString ) { $ length = strlen ( $ filterString ) ; if ( $ length == 0 || preg_match ( '/[^\w\[\],]+/' , $ filterString ) ) { return null ; } $ start = null ; $ current = [ ] ; $ stack = [ ] ; $ parent = [ ] ; $ currentElement = null ; for ( $ position = 0 ; $ position < $ length ; $ position ++ ) { //Extracting field when encountering field separators if ( in_array ( $ filterString [ $ position ] , [ '[' , ']' , ',' ] ) ) { if ( $ start !== null ) { $ currentElement = substr ( $ filterString , $ start , $ position - $ start ) ; $ current [ $ currentElement ] = 1 ; } $ start = null ; } switch ( $ filterString [ $ position ] ) { case '[' : array_push ( $ parent , $ currentElement ) ; // push current field in stack and initialize current array_push ( $ stack , $ current ) ; $ current = [ ] ; break ; case ']' : //cache current $ temp = $ current ; //Initialize with previous $ current = array_pop ( $ stack ) ; //Add from cache $ current [ array_pop ( $ parent ) ] = $ temp ; break ; //Do nothing on comma. On the next iteration field will be extracted case ',' : break ; default : //Move position if no field separators found if ( $ start === null ) { $ start = $ position ; } } } //Check for wrongly formatted filter if ( ! empty ( $ stack ) ) { return null ; } //Check if there's any field remaining that's not added to response if ( $ start !== null ) { $ currentElement = substr ( $ filterString , $ start , $ position - $ start ) ; $ current [ $ currentElement ] = 1 ; } return $ current ; }
Parse filter string into associative array. Field names are returned as keys with values for scalar fields as 1.

@param string $filterString
<pre>
ex. customer[id,email],addresses[city,postcode,region[region_code,region]]
</pre>
@return array|null
<pre>
ex.
array(
'customer' =>
array(
'id' => 1,
'email' => 1,
),
'addresses' =>
array(
'city' => 1,
'postcode' => 1,
'region' =>
array(
'region_code' => 1,
'region' => 1,
),
),
)
</pre>

@SuppressWarnings(PHPMD.CyclomaticComplexity)
Parse filter string into associative array . Field names are returned as keys with values for scalar fields as 1 .