/** * @param StreamInterface $stream * * @return array|int|null|string * * @throws CommandException * @throws \RuntimeException */ public static function deserialize(StreamInterface $stream) { $rsp = $stream->readLine(); list($type, $result) = [$rsp[0], trim(substr($rsp, 1, strlen($rsp)))]; switch ($type) { case '-': // ERRORS throw new CommandException($result); case '+': // SIMPLE STRINGS return $result; case ':': // INTEGERS return (int) $result; case '$': // BULK STRINGS $result = (int) $result; if ($result == -1) { return null; } return trim($stream->readBytes($result + 2)); case '*': // ARRAYS $cnt = (int) $result; $out = []; for ($i = 0; $i < $cnt; $i++) { $out[] = static::deserialize($stream); } return $out; default: throw new \RuntimeException('unhandled protocol response: ' . $rsp); } }
/** * Read a line from the stream * * @return string */ public function readLine() { return $this->connected->readLine(); }