RTPHPLib icon indicating copy to clipboard operation
RTPHPLib copied to clipboard

Fix mutiline from fields and parse format

Open oijkn opened this issue 6 years ago • 4 comments

Hi, thank you for your great LIB, in my case I have some fields with multiline and your actual code doesn't work with them. So i modified your code to return all needed lines. And we can use this function with search format.

I hope that can help someone else :)

    private function parseResponseBody(array $response, $delimiter = ':')
    {
        $responseArray = array();
        $lastkey = null;

        foreach ($response as $line) {
            //RT will always preface a multiline with the length of the last key + length of $delimiter + one space)
            if(! is_null($lastkey) && preg_match('/^\s{' . ( strlen($lastkey) + strlen($delimiter) + 1 ) . '}(.*)$/', $line, $matches)) {
                $responseArray[$lastkey] .= "\n" . $matches[1];
            }
            elseif(! is_null($lastkey) && strlen($line) == 0) {
                $lastkey = null;
            }
            elseif(preg_match('/^#/', $line, $matches)) {
                $responseArray[$line] = '';
            }
            elseif(preg_match('~^([a-zA-Z0-9]+|CF\.{[^}]*})' . $delimiter . '\s+(.*)~', $line, $matches)) {
                $responseArray[$lastkey = $matches[1]] = $matches[2];
            }
            elseif ((bool) $line) {
                if (preg_match('/\s{4}/i', $line))
                    $line = preg_replace('/\s{4}/i', '', $line);

                $responseArray[$lastkey] .= PHP_EOL . $line;
            }
            elseif(is_null($lastkey) && preg_match('/\t/', $line)) {
                foreach ($response as $line) {
                    if (preg_match('/\t/', $line)) {
                        if (is_null($lastkey)) {
                            $lastkey ++;
                            $fields = explode("\t", $line);
                        }
                        elseif (! is_null($lastkey)) {
                            $result = explode("\t", $line);
                                foreach($fields as $key => $val) {
                                    $combined[$val] = $result[$key];
                                }
                            $responseArray[] = $combined;
                        }
                    }
                }
            }
        }

        return $responseArray;
    }

oijkn avatar Feb 26 '18 11:02 oijkn

@oijkn Thanks for contributing! Please resubmit as a pull request, and I'll be happy to review it for inclusion.

dersam avatar Mar 15 '18 15:03 dersam

Hello, is possible to have a check of this solution, and have an improvment of your API with it ? I have the same problem, and a copy/paste leave me with an error at this line :

$responseArray[$lastkey] .= PHP_EOL . $line;

-> undefined index

I'm using the "search" function in "-s" format and several custom fields.

thank you in advance

Abzuu avatar May 31 '18 14:05 Abzuu

Hello, It works with the "l" search format (error with "s" format).

            elseif ((bool) $line && !is_null($lastkey)) {
                if (preg_match('/\s{4}/i', $line)){
                    $line = preg_replace('/\s{4}/i', '', $line);

                }
                if ($lastkey !== null){
                        $responseArray[$lastkey] .= PHP_EOL . $line;
                }
            }

Abzuu avatar Jun 11 '18 11:06 Abzuu

Just to confirm this issue, the data comes out of the API like this for me:


CF.{Example Custom Field}: First line
    Second line
    
    Additional line
    Another line

danpoltawski avatar Feb 27 '19 17:02 danpoltawski