Ejemplo n.º 1
0
 /**
  * Read response from connection
  * 
  * @param Rediska_Connection $connection
  * @return mixed
  */
 public static function readResponseFromConnection(Rediska_Connection $connection)
 {
     $reply = $connection->readLine();
     if ($reply === null) {
         return $reply;
     }
     $type = substr($reply, 0, 1);
     $data = substr($reply, 1);
     switch ($type) {
         case self::REPLY_STATUS:
             if ($data == 'OK') {
                 return true;
             } else {
                 return $data;
             }
         case self::REPLY_ERROR:
             $message = substr($data, 4);
             throw new Rediska_Connection_Exec_Exception($message);
         case self::REPLY_INTEGER:
             if (strpos($data, '.') !== false) {
                 $number = (int) $data;
             } else {
                 $number = (double) $data;
             }
             return $number;
         case self::REPLY_BULK:
             if ($data == '-1') {
                 return null;
             } else {
                 $length = (int) $data;
                 return $connection->read($length);
             }
         case self::REPLY_MULTY_BULK:
             $count = (int) $data;
             $replies = array();
             for ($i = 0; $i < $count; $i++) {
                 $replies[] = self::readResponseFromConnection($connection);
             }
             return $replies;
         default:
             throw new Rediska_Connection_Exec_Exception("Invalid reply type: '{$type}'");
     }
 }
Ejemplo n.º 2
0
 protected function _readResponseFromConnection(Rediska_Connection $connection)
 {
     $reply = $connection->readLine();
     $type = substr($reply, 0, 1);
     $data = substr($reply, 1);
     switch ($type) {
         case self::REPLY_STATUS:
             if ($data == 'OK') {
                 return true;
             } else {
                 return $data;
             }
         case self::REPLY_ERROR:
             $message = substr($data, 4);
             throw new Rediska_Command_Exception($message);
         case self::REPLY_INTEGER:
             if (strpos($data, '.') !== false) {
                 $number = (int) $data;
             } else {
                 $number = (double) $data;
             }
             if ((string) $number != $data) {
                 throw new Rediska_Command_Exception("Can't convert data ':{$data}' to integer");
             }
             return $number;
         case self::REPLY_BULK:
             if ($data == '-1') {
                 return null;
             } else {
                 $length = (int) $data;
                 if ((string) $length != $data) {
                     throw new Rediska_Command_Exception("Can't convert bulk reply header '\${$data}' to integer");
                 }
                 return $connection->read($length);
             }
         case self::REPLY_MULTY_BULK:
             $count = (int) $data;
             if ((string) $count != $data) {
                 throw new Rediska_Command_Exception("Can't convert multi-response header '{$data}' to integer");
             }
             $replies = array();
             for ($i = 0; $i < $count; $i++) {
                 $replies[] = $this->_readResponseFromConnection($connection);
             }
             return $replies;
         default:
             throw new Rediska_Command_Exception("Invalid reply type: '{$type}'");
     }
 }