1
|
|
|
<?php |
2
|
|
|
namespace Cassandra\Type; |
3
|
|
|
|
4
|
|
|
abstract class Base{ |
5
|
|
|
|
6
|
|
|
const CUSTOM = 0x0000; |
7
|
|
|
const ASCII = 0x0001; |
8
|
|
|
const BIGINT = 0x0002; |
9
|
|
|
const BLOB = 0x0003; |
10
|
|
|
const BOOLEAN = 0x0004; |
11
|
|
|
const COUNTER = 0x0005; |
12
|
|
|
const DECIMAL = 0x0006; |
13
|
|
|
const DOUBLE = 0x0007; |
14
|
|
|
const FLOAT = 0x0008; |
15
|
|
|
const INT = 0x0009; |
16
|
|
|
const TEXT = 0x000A; // deprecated in Protocol v3 |
17
|
|
|
const TIMESTAMP = 0x000B; |
18
|
|
|
const UUID = 0x000C; |
19
|
|
|
const VARCHAR = 0x000D; |
20
|
|
|
const VARINT = 0x000E; |
21
|
|
|
const TIMEUUID = 0x000F; |
22
|
|
|
const INET = 0x0010; |
23
|
|
|
const COLLECTION_LIST = 0x0020; |
24
|
|
|
const COLLECTION_MAP = 0x0021; |
25
|
|
|
const COLLECTION_SET = 0x0022; |
26
|
|
|
const UDT = 0x0030; |
27
|
|
|
const TUPLE = 0x0031; |
28
|
|
|
|
29
|
|
|
public static $typeClassMap = [ |
30
|
|
|
self::ASCII => 'Cassandra\Type\Ascii', |
31
|
|
|
self::VARCHAR => 'Cassandra\Type\Varchar', |
32
|
|
|
self::TEXT => 'Cassandra\Type\Varchar', // deprecated in Protocol v3 |
33
|
|
|
self::VARINT => 'Cassandra\Type\Varint', |
34
|
|
|
self::BIGINT => 'Cassandra\Type\Bigint', |
35
|
|
|
self::COUNTER => 'Cassandra\Type\Counter', |
36
|
|
|
self::TIMESTAMP => 'Cassandra\Type\Timestamp', |
37
|
|
|
self::BLOB => 'Cassandra\Type\Blob', |
38
|
|
|
self::BOOLEAN => 'Cassandra\Type\Boolean', |
39
|
|
|
self::DECIMAL => 'Cassandra\Type\Decimal', |
40
|
|
|
self::DOUBLE => 'Cassandra\Type\Double', |
41
|
|
|
self::FLOAT => 'Cassandra\Type\PhpFloat', |
42
|
|
|
self::INT => 'Cassandra\Type\PhpInt', |
43
|
|
|
self::UUID => 'Cassandra\Type\Uuid', |
44
|
|
|
self::TIMEUUID => 'Cassandra\Type\Timeuuid', |
45
|
|
|
self::INET => 'Cassandra\Type\Inet', |
46
|
|
|
self::COLLECTION_LIST => 'Cassandra\Type\CollectionList', |
47
|
|
|
self::COLLECTION_SET => 'Cassandra\Type\CollectionSet', |
48
|
|
|
self::COLLECTION_MAP => 'Cassandra\Type\CollectionMap', |
49
|
|
|
self::UDT => 'Cassandra\Type\UDT', |
50
|
|
|
self::TUPLE => 'Cassandra\Type\Tuple', |
51
|
|
|
self::CUSTOM => 'Cassandra\Type\Custom', |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $_definition; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* @var mixed |
63
|
|
|
*/ |
64
|
|
|
protected $_value; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $_binary; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @param mixed $value |
74
|
|
|
*/ |
75
|
|
|
public function __construct($value = null){ |
76
|
|
|
$this->_value = $value; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
* @param string $binary |
82
|
|
|
* @return self |
83
|
|
|
*/ |
84
|
|
|
public function setBinary($binary){ |
85
|
|
|
$this->_binary = $binary; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getBinary(){ |
94
|
|
|
if ($this->_binary === null) |
95
|
|
|
$this->_binary = static::binary($this->_value, $this->_definition); |
|
|
|
|
96
|
|
|
|
97
|
|
|
return $this->_binary; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
public function getValue(){ |
104
|
|
|
if ($this->_value === null && $this->_binary !== null) |
105
|
|
|
$this->_value = static::parse($this->_binary, $this->_definition); |
106
|
|
|
|
107
|
|
|
return $this->_value; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function __toString(){ |
114
|
|
|
return (string) $this->_value; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public static function getBinaryByType($dataType, $value){ |
118
|
|
|
if (is_array($dataType)){ |
119
|
|
|
if (!isset($dataType['definition'])) |
120
|
|
|
throw new Exception('Since v0.7, collection types should have "definition" directive.'); |
121
|
|
|
$class = self::$typeClassMap[$dataType['type']]; |
122
|
|
|
return $class::binary($value, $dataType['definition']); |
123
|
|
|
} |
124
|
|
|
else{ |
125
|
|
|
$class = self::$typeClassMap[$dataType]; |
126
|
|
|
return $class::binary($value); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
* @param int|array $dataType |
133
|
|
|
* @param mixed $value |
134
|
|
|
* @throws Exception |
135
|
|
|
* @return Base|null |
136
|
|
|
*/ |
137
|
|
|
public static function getTypeObject($dataType, $value) { |
138
|
|
|
if ($value === null) |
139
|
|
|
return null; |
140
|
|
|
|
141
|
|
|
if (!is_array($dataType)){ |
142
|
|
|
$class = self::$typeClassMap[$dataType]; |
143
|
|
|
return new $class($value); |
144
|
|
|
} |
145
|
|
|
else{ |
146
|
|
|
$class = self::$typeClassMap[$dataType['type']]; |
147
|
|
|
return new $class($value, $dataType['definition']); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.