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;
    }