Breeze-PHP是Breeze序列化的php实现.
在composer.json中添加依赖:
"require" : {
"wei-breeze/breeze": "v0.1.0"
}
- 基础类型编解码
// 编码
$buf = new Buffer();
BreezeWriter::writeString($buf, 'test string');
//解码
$newBuf = new Buffer($buf->buffer());
$r = TypeString::instance()->read($newBuf);
var_dump($r);
- 集合类型编解码
$buf = new Buffer();
$tp = new TypePackedMap(TypeInt32::instance(), TypeString::instance());
BreezeWriter::writeMap($buf, [123 => 'erw', 45 => 'wer', 657 => 'terd'], $tp->getElemType()[0], $tp->getElemType()[1]);
//解码
$newBuf = new Buffer($buf->buffer());
$r = $tp->read($newBuf);
var_dump($r);
- Breeze Message编解码
$msg = new TestMsg();
$msg->setMyInt(1234);
$msg->setMyString('ewjo3**#J');
$subMsg = new TestSubMsg();
$subMsg->setMyString('J(*#^H');
$subMsg->setMyInt(-345);
$subMsg->setMyBool(true);
$subMsg->setMyBytes(pack('N', 2435));
$subMsg->setMyInt64(723847289347398);
$subMsg->setMyArray([234, 5467, -678, 0]);
$subMsg->setMyMap1(['j(*&*(' => 'fj98A)', 'J()*#' => pack('l', -4578)]);
$subMsg->setMyMap2([234 => [-45, 0], 3465 => [0, 345]]);
$msg->setMyArray([$subMsg]);
// 编码
$buf = new Buffer();
$tp = new TypeMessage(new TestMsg(false));
BreezeWriter::writeValue($buf, $msg, $tp);
//解码
$newBuf = new Buffer($buf->buffer());
$r = $tp->read($newBuf);
var_dump($r);
//直接编码
$buf = new Buffer();
$msg->writeTo($buf);
//直接解码
$newBuf = new Buffer($buf->buffer());
$r = new TestMsg();
$r->readFrom($newBuf);
var_dump($r);
- 任意类型编解码(不使用BreezeType指定类型)
//编码
$v = 1234;
$buf = new Buffer();
BreezeWriter::writeValue($buf, $v);
//解码
$newBuf = new Buffer($buf->buffer());
$r = BreezeReader::readValue($newBuf);
var_dump($r);
更多demo请参考单元测试