partikus/protobuf-encoder

v0.0.1 2017-02-26 10:43 UTC

This package is auto-updated.

Last update: 2024-09-05 19:14:48 UTC


README

Build Status

这是一个简单的PHP库,允许将Protobuf消息编码为字节码。此外,此库允许将消息编码/解码为单个消息。实现基于这篇文章

如何使用它?

准备Proto消息文件

syntax = "proto3";

package Your.Namespace;

message DummyMessage {
    string Title = 1;
    string Description = 2;
    string CreatedAt = 3;
    string UpdatedAt = 4;
}

生成PHP文件

vendor/bin/protobuf --include-descriptors -i . -o src/ DummyMessage.proto

创建PHP对象

$dummyMessage = Your\Namespace\DummyMessage::fromArray([
    'Title' => 'Test 123',
    'Description' => 'Description from the text',
    'CreatedAt' => '2016-12-12 12:00:00',
    'UpdatedAt' => '2016-12-12 13:00:00',
]);

$encoder = new ClearCode\Protobuf\ByteEncoder();
/** @var \Protobuf\Stream $stream */
$stream = $encoder->encode($dummyMessage);
file_put_contents(__DIR__ . '/dummyMessage.pb.bin', $stream);