1. Go to this page and download the library: Download thesis/protobuf library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
thesis / protobuf example snippets
use Thesis\Protobuf\Reflection;
final readonly class CreateUserRequest
{
/**
* @param list<string> $roles
*/
public function __construct(
#[Reflection\Field(1, Reflection\Int32T::T)]
public int $id = 0,
#[Reflection\Field(2, Reflection\StringT::T)]
public string $name = '',
#[Reflection\Field(3, new Reflection\ListT(Reflection\StringT::T))]
public array $roles = [],
) {}
}
use Thesis\Protobuf\Encoder;
$encoder = Encoder\Builder::buildDefault();
use Thesis\Protobuf\Encoder;
$encoder = new Encoder\Builder()
->withCache(/** cache implementation */)
->build();
use Thesis\Protobuf\Decoder;
$decoder = Decoder\Builder::buildDefault();
use Thesis\Protobuf\Decoder;
$decoder = new Decoder\Builder()
->withCache(/** cache implementation */)
->build();
$request = $decoder->decode(/** protobuf buffer here */, CreateUserRequest::class);
echo $request->name;
use Thesis\Protobuf\Decoder;
use Thesis\Protobuf\Reflection;
$decoder = Decoder\Builder::buildDefault();
try {
$message = $decoder->decode($buffer, CreateUserRequest::class);
} catch (Reflection\Exception\MappingError $e) {
foreach ($e->reasons as $reason) {
if ($reason instanceof Reflection\Exception\PropertyRequired) {
echo $reason->class . "::$" . $reason->property . PHP_EOL;
}
}
}
use Thesis\Protobuf\Decoder;
use Thesis\Protobuf\UnknownFields;
$decoder = new Decoder\Builder()
->withUnknownHandler(UnknownFields::handler())
->build();
$request = $decoder->decode($buffer, CreateUserRequest::class);
// Get unknown fields for a specific object.
$unknowns = UnknownFields::of($request);
foreach ($unknowns as $field) {
echo "Field #{$field->tag->num}, wire type: {$field->tag->type->name}\n";
}
$unknowns = UnknownFields::of($request->nested);
use Thesis\Protobuf\Decoder;
use Thesis\Protobuf\UnknownFields;
$decoder = new Decoder\Builder()
->withUnknownHandler(new UnknownFields\UnknownFieldsCallback(
static function (object $message, array $unknowns): void {
$logger->warning('Unknown fields detected', [
'class' => $message::class,
'fields' => array_map(
static fn(UnknownFields\UnknownField $f) => $f->tag->num,
$unknowns,
),
]);
},
))
->build();
use Thesis\Protobuf\UnknownFields;
final readonly class MyHandler implements UnknownFields\Handler
{
#[\Override]
public function handle(object $message, array $unknowns): void
{
// your logic here
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.