RTPHPLib
RTPHPLib copied to clipboard
Fix mutiline from fields and parse format
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 Thanks for contributing! Please resubmit as a pull request, and I'll be happy to review it for inclusion.
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
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;
}
}
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