Protobuf\Binary\Platform\BcNegativeEncoder::encodeVarint PHP Метод

encodeVarint() публичный Метод

public encodeVarint ( $varint )
    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;
    }

Usage Example

 public function testEncodeVarint()
 {
     $encoder = new BcNegativeEncoder();
     $actual = $encoder->encodeVarint(-10);
     $expected = [246, 255, 255, 255, 255, 255, 255, 255, 255, 129];
     $this->assertEquals($expected, $actual);
 }