Skip to content

Api protocolbuffers

Shuhei Tanuma edited this page Dec 24, 2013 · 6 revisions
Name
ProtocolBuffers
Alias
ProtocolBuffers

Description

Old-style protocol buffers serializer / deserializer. You should use generated class which extends ProtocolBuffers\Message instead of this class.

Constants

Name Description
ProtocolBuffers::WIRETYPE_VARINT
ProtocolBuffers::WIRETYPE_FIXED64
ProtocolBuffers::WIRETYPE_LENGTH_DELIMITED
ProtocolBuffers::WIRETYPE_START_GROUP
ProtocolBuffers::WIRETYPE_END_GROUP
ProtocolBuffers::WIRETYPE_FIXED32
ProtocolBuffers::TYPE_DOUBLE
ProtocolBuffers::TYPE_FLOAT
ProtocolBuffers::TYPE_INT64
ProtocolBuffers::TYPE_UINT64
ProtocolBuffers::TYPE_INT32
ProtocolBuffers::TYPE_FIXED64
ProtocolBuffers::TYPE_FIXED32
ProtocolBuffers::TYPE_BOOL
ProtocolBuffers::TYPE_STRING
ProtocolBuffers::TYPE_GROUP
ProtocolBuffers::TYPE_MESSAGE
ProtocolBuffers::TYPE_BYTES
ProtocolBuffers::TYPE_UINT32
ProtocolBuffers::TYPE_ENUM
ProtocolBuffers::TYPE_SFIXED32
ProtocolBuffers::TYPE_SFIXED64
ProtocolBuffers::TYPE_SINT32
ProtocolBuffers::TYPE_SINT64
ProtocolBuffers::MAX_FIELD_TYPE
ProtocolBuffers::PHP_MESSAGE_OPTION

Methods

string public static ProtocolBuffers::encode(mixed $class)

Description

encode PHP object to raw protocolbuffers message.

Parameters

$class: class which have getDescriptor method and returns correct ProtocolBuffers\Descriptor.

Return Value

string: raw protocolbuffers message

Example
<?php
class Article
{
  protected $IsDraft;

  public static function getDescriptor()
  {
    static $descriptor;

    if (!isset($descriptor)) {
      $desc = new ProtocolBuffersDescriptorBuilder();
      $desc->addField(1, new ProtocolBuffersFieldDescriptor(array(
        "type"     => ProtocolBuffers::TYPE_BOOL,
        "name"     => "IsDraft",
        "required" => false,
        "optional" => true,
        "repeated" => false,
        "packable" => false,
        "default"  => false,
      )));
      $descriptor = $desc->build();
    }

    return $descriptor;
  }  
}

$article = new Article(["IsDraft" => true]);
// You can encode any class which has expected `getDescriptor` method.
echo ProtocolBuffers::encode($article);

mixed public static ProtocolBuffers::decode(string $class_name, string $raw_message)

Description

decode raw protcolbuffers message into $class_name.

Parameters

$class_name: class which have getDescriptor method and returns correct ProtocolBuffers\Descriptor. $raw_message: valid raw protocolbuffers message

Return Value

mixed: class

Example