/** * @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 bytes off from the stream. * * @param int|null $maxlen * * @return string The response. */ public function readBytes($maxlen = null) { return $this->connected->readBytes($maxlen); }