_mode = $mode; } /* (non-phpdoc) * @see ResponseParser::parseResponse() */ public function parseResponse($responseLine, $responseData) { if ($responseLine == Response::RESPONSE_NOT_FOUND) { throw new Exception\ServerException(sprintf( 'Server reported %s', $responseLine )); } if (!preg_match('#^OK \d+$#', $responseLine)) { throw new Exception\ServerException(sprintf( 'Unhandled response: %s', $responseLine )); } $dataLines = preg_split("#[\r\n]+#", rtrim($responseData)); if (isset($dataLines[0]) && $dataLines[0] == '---') { array_shift($dataLines); // discard header line } $data = array_map(array($this, '_mapYamlList'), $dataLines); if ($this->_mode == self::MODE_DICT) { // TODO: do this better. $array = array(); foreach ($data as $line) { if (!preg_match('#(\S+):\s*(.*)#', $line, $matches)) { throw new Exception("YAML parse error for line: $line"); } list(, $key, $value) = $matches; $array[$key] = $value; } $data = $array; } return new Response\ArrayResponse('OK', $data); } /** * Callback for array_map to process YAML lines. * @param string $line * @return string */ private function _mapYamlList($line) { return ltrim($line, '- '); } }