1: <?php
2: namespace phpcassa\Schema\DataType;
3:
4: 5: 6: 7: 8:
9: class LongType extends CassandraType {
10:
11: public function pack($value, $is_name=null, $slice_end=null, $is_data=null)
12: {
13:
14:
15:
16: if (PHP_INT_SIZE == 4) {
17: $neg = $value < 0;
18:
19: if ($neg) {
20: $value *= -1;
21: }
22:
23: $hi = (int)($value / 4294967296);
24: $lo = (int)$value;
25:
26: if ($neg) {
27: $hi = ~$hi;
28: $lo = ~$lo;
29: if (($lo & (int)0xffffffff) == (int)0xffffffff) {
30: $lo = 0;
31: $hi++;
32: } else {
33: $lo++;
34: }
35: }
36: $data = pack('N2', $hi, $lo);
37: } else {
38: $hi = $value >> 32;
39: $lo = $value & 0xFFFFFFFF;
40: $data = pack('N2', $hi, $lo);
41: }
42: return $data;
43: }
44:
45: public function unpack($data, $is_name=null)
46: {
47: $arr = unpack('N2', $data);
48:
49:
50:
51:
52: if (PHP_INT_SIZE == 4) {
53:
54: $hi = $arr[1];
55: $lo = $arr[2];
56: $isNeg = $hi < 0;
57:
58:
59: if ($isNeg) {
60: $hi = ~$hi & (int)0xffffffff;
61: $lo = ~$lo & (int)0xffffffff;
62:
63: if ($lo == (int)0xffffffff) {
64: $hi++;
65: $lo = 0;
66: } else {
67: $lo++;
68: }
69: }
70:
71:
72:
73:
74: if ($hi & (int)0x80000000) {
75: $hi &= (int)0x7fffffff;
76: $hi += 0x80000000;
77: }
78:
79: if ($lo & (int)0x80000000) {
80: $lo &= (int)0x7fffffff;
81: $lo += 0x80000000;
82: }
83:
84: $value = $hi * 4294967296 + $lo;
85:
86: if ($isNeg)
87: $value = 0 - $value;
88:
89: } else {
90:
91: if ($arr[2] & 0x80000000)
92: $arr[2] = $arr[2] & 0xffffffff;
93:
94:
95: if ($arr[1] & 0x80000000) {
96: $arr[1] = $arr[1] & 0xffffffff;
97: $arr[1] = $arr[1] ^ 0xffffffff;
98: $arr[2] = $arr[2] ^ 0xffffffff;
99: $value = 0 - $arr[1]*4294967296 - $arr[2] - 1;
100: } else {
101: $value = $arr[1]*4294967296 + $arr[2];
102: }
103: }
104: return $value;
105: }
106: }
107:
108: