mode = $mode; } public function parseResponse(string $responseLine, ?string $responseData): ArrayResponse { if ($responseLine === ResponseInterface::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 )); } $lines = array_filter(explode("\n", $responseData), function ($line) { return !empty($line) && $line !== '---'; }); return $this->mode === self::MODE_LIST ? $this->parseList($lines) : $this->parseDictionary($lines); } private function parseList(array $lines): ArrayResponse { $data = []; foreach ($lines as $line) { if (strncmp($line, '- ', 2) !== 0) { throw new ClientException("YAML parse error for line: $line" . print_r($lines, true)); } $data[] = substr($line, 2); } return new ArrayResponse('OK', $data); } private function parseDictionary(array $lines): ArrayResponse { $data = []; foreach ($lines as $line) { if (!preg_match('#(\S+):\s*(.*)#', $line, $matches)) { throw new ClientException("YAML parse error for line: $line"); } $data[$matches[1]] = $matches[2]; } return new ArrayResponse('OK', $data); } }