nowgoo / php-protobuf
简单的 protobuf 编码器和解码器
dev-master
2020-12-13 16:46 UTC
This package is auto-updated.
Last update: 2024-09-14 00:54:27 UTC
README
简单的 protobuf 编码器和解码器
特性
- 无需编译 .proto 文件
- 无需方案定义进行解码/编码
- grpc 客户端 [wip]
安装
composer require nowgoo/php-protobuf dev-master
用法
假设我们有一个定义为以下 .proto 的文件
message Example {
int32 foo = 1;
SubMessage bar = 2;
repeated baz = 3;
}
message SubMessage {
string bar_sub = 1;
}
代码说话
use \PhpProtobuf\Scheme; $scheme = new Scheme([ // simple field 1 => ['name'=>'foo', 'type'=>Scheme::TYPE_INT], // embeded field 2 => ['name'=>'bar', 'type'=>Scheme::TYPE_EMBEDED, 'fields'=>[ 1 => ['name'=>'bar_sub', 'type'=>Scheme::TYPE_STRING] ]], // repeated field 3 => ['name'=>'baz', 'type'=>Scheme::TYPE_INT, 'repeated'=>true], ]); $binary = $scheme->encode([ 'foo' => 123456, 'bar' => ['bar_sub' => 'hello, world'], 'baz' => [3, 270, 86942] ]); // binary = [08 c0 c4 07 12 0e 0a 0c 68 65 6c 6c 6f 2c 20 77 6f 72 6c 64 1a 06 03 8e 02 9e a7 05] $decoded = $scheme->decode($binary); // $decoded == ['foo'=>123456, 'bar'=>['bar_sub'=>'hello, world'], 'baz'=>[3,270,86942]];
无需方案定义进行解码/编码
$scheme = new UniversalScheme(); $binary = $scheme->encode([ 'keys' => 123456, 'were' => 'hello', 'skipped' => [ 'during' => [ 'encoding' => 'level 3 value' ], 'process' => 'level 2 value', ], ]); $decoded = $scheme->decode($binary); /* $decoded: [ 1 => 123456, 2 => 'hello', 3 => [ 1 => [ 1 => 'level 3 value' ], 2 => 'level 2 value', ], ] */
grpc
[wip]
许可
MIT 许可证