public function encodeVarint($varint)
{
$values = [];
$value = sprintf('%u', $varint);
while (bccomp($value, 0, 0) > 0) {
// Get the last 7bits of the number
$bin = '';
$dec = $value;
do {
$rest = bcmod($dec, 2);
$dec = bcdiv($dec, 2, 0);
$bin = $rest . $bin;
} while ($dec > 0 && mb_strlen($bin, '8bit') < 7);
// Pack as a decimal and apply the flag
$values[] = intval($bin, 2) | 0x80;
$value = bcdiv($value, 0x80, 0);
}
return $values;
}