From ad796d9cb7bea572360a4b4e1cc546c1a90599ab Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Sun, 29 Mar 2015 05:03:24 +0100 Subject: [PATCH] Extract target from numeric replies --- src/Parser.php | 25 +++++++++++++++---------- tests/ParserTest.php | 22 +++++++++++----------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index 6eb5438..8467dc2 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -390,17 +390,22 @@ public function parse($message) $parsed['code'] = $command; } if (!empty($parsed['params'])) { - $all = $this->strip($parsed['params']); - if (strpos($parsed['params'], ' :') !== false) { - list($head, $tail) = explode(' :', $parsed['params'], 2); - } else { - $head = $parsed['params']; - $tail = ''; + // Slice off the target from the front of the reply + $temp = explode(' ', ltrim($parsed['params']), 2); + $parsed['target'] = array_shift($temp); + if ($parsed['params'] = (!empty($temp)) ? (' ' . array_shift($temp)) : '') { + $all = $this->strip($parsed['params']); + if (strpos($parsed['params'], ' :') !== false) { + list($head, $tail) = explode(' :', $parsed['params'], 2); + } else { + $head = $parsed['params']; + $tail = ''; + } + $params = explode(' ', $head); + $params[] = $tail; + $parsed['params'] = array_filter($params); + $parsed['params']['all'] = $all; } - $params = explode(' ', $head); - $params[] = $tail; - $parsed['params'] = array_filter($params); - $parsed['params']['all'] = $all; } } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 20f68be..e6fad7f 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -2527,15 +2527,15 @@ public function dataProviderTestParse() 'servername' => 'pratchett.freenode.net', 'command' => '004', 'params' => array( - 1 => 'Phergie3', - 2 => 'pratchett.freenode.net', - 3 => 'ircd-seven-1.1.3', - 4 => 'DOQRSZaghilopswz', - 5 => 'CFILMPQbcefgijklmnopqrstvz', - 6 => 'bkloveqjfI', - 'all' => 'Phergie3 pratchett.freenode.net ircd-seven-1.1.3 DOQRSZaghilopswz CFILMPQbcefgijklmnopqrstvz bkloveqjfI', + 1 => 'pratchett.freenode.net', + 2 => 'ircd-seven-1.1.3', + 3 => 'DOQRSZaghilopswz', + 4 => 'CFILMPQbcefgijklmnopqrstvz', + 5 => 'bkloveqjfI', + 'all' => 'pratchett.freenode.net ircd-seven-1.1.3 DOQRSZaghilopswz CFILMPQbcefgijklmnopqrstvz bkloveqjfI', ), 'code' => '004', + 'target' => 'Phergie3', ), ), @@ -2547,13 +2547,13 @@ public function dataProviderTestParse() 'servername' => 'services.', 'command' => '328', 'params' => array( - 1 => 'Phergie3', - 2 => '#laravel', - 3 => 'http://laravel.com', - 'all' => 'Phergie3 #laravel :http://laravel.com', + 1 => '#laravel', + 2 => 'http://laravel.com', + 'all' => '#laravel :http://laravel.com', ), 'message' => ":services. 328 Phergie3 #laravel :http://laravel.com\r\n", 'code' => '328', + 'target' => 'Phergie3', ), ),