1: <?php
2: namespace phpcassa\Schema\DataType;
3:
4: 5: 6: 7: 8: 9: 10:
11: class IntegerType extends CassandraType {
12:
13: public function pack($value, $is_name=null, $slice_end=null, $is_data=null)
14: {
15: $value = (int)$value;
16: $out = array();
17: if ($value >= 0) {
18: while ($value >= 256) {
19: $out[] = pack('C', 0xff & $value);
20: $value >>= 8;
21: }
22: $out[] = pack('C', 0xff & $value);
23: if ($value > 127) {
24: $out[] = chr('00');
25: }
26: } else {
27: $value = -1 - $value;
28: while ($value >= 256) {
29: $out[] = pack('C', 0xff & ~$value);
30: $value >>= 8;
31: }
32: if ($value <= 127) {
33: $out[] = pack('C', 0xff & ~$value);
34: } else {
35: $top = pack('n', 0xffff & ~$value);
36:
37: $out[] = strrev($top);
38: }
39: }
40:
41: return strrev(implode($out));
42: }
43:
44: public function unpack($data, $is_name=null)
45: {
46: $val = hexdec(bin2hex($data));
47: if ((ord($data[0]) & 128) != 0)
48: $val = $val - (1 << (strlen($data) * 8));
49: return $val;
50: }
51: }
52:
53: