|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace lroman242\LaravelCassandra\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Cassandra\Rows; |
|
7
|
|
|
use Cassandra\Timestamp; |
|
8
|
|
|
use lroman242\LaravelCassandra\Collection; |
|
9
|
|
|
use lroman242\LaravelCassandra\Query\Builder as QueryBuilder; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Model as BaseModel; |
|
11
|
|
|
use Illuminate\Support\Str; |
|
12
|
|
|
|
|
13
|
|
|
abstract class Model extends BaseModel |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Indicates if the IDs are auto-incrementing. |
|
17
|
|
|
* This is not possible in cassandra so we override this |
|
18
|
|
|
* |
|
19
|
|
|
* @var bool |
|
20
|
|
|
*/ |
|
21
|
|
|
public $incrementing = false; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @inheritdoc |
|
25
|
|
|
*/ |
|
26
|
18 |
|
public function newEloquentBuilder($query) |
|
27
|
|
|
{ |
|
28
|
18 |
|
return new Builder($query); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @inheritdoc |
|
33
|
|
|
*/ |
|
34
|
18 |
|
protected function newBaseQueryBuilder() |
|
35
|
|
|
{ |
|
36
|
18 |
|
$connection = $this->getConnection(); |
|
37
|
|
|
|
|
38
|
18 |
|
return new QueryBuilder($connection, null, $connection->getPostProcessor()); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
12 |
|
public function freshTimestamp() |
|
45
|
|
|
{ |
|
46
|
12 |
|
return new Timestamp(); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
*/ |
|
52
|
12 |
|
public function fromDateTime($value) |
|
53
|
|
|
{ |
|
54
|
|
|
// If the value is already a Timestamp instance, we don't need to parse it. |
|
55
|
12 |
|
if ($value instanceof Timestamp) { |
|
|
|
|
|
|
56
|
12 |
|
return $value; |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Let Eloquent convert the value to a DateTime instance. |
|
60
|
1 |
|
if (!$value instanceof \DateTime) { |
|
61
|
1 |
|
$value = parent::asDateTime($value); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
return new Timestamp($value->getTimestamp() * 1000); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritdoc |
|
69
|
|
|
*/ |
|
70
|
5 |
|
protected function asDateTime($value) |
|
71
|
|
|
{ |
|
72
|
|
|
// Convert UTCDateTime instances. |
|
73
|
5 |
|
if ($value instanceof Timestamp) { |
|
|
|
|
|
|
74
|
5 |
|
return Carbon::instance($value->toDateTime()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return parent::asDateTime($value); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @inheritdoc |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function originalIsNumericallyEquivalent($key) |
|
84
|
|
|
{ |
|
85
|
|
|
$current = $this->attributes[$key]; |
|
86
|
|
|
$original = $this->original[$key]; |
|
87
|
|
|
|
|
88
|
|
|
// Date comparison. |
|
89
|
|
|
if (in_array($key, $this->getDates())) { |
|
90
|
|
|
$current = $current instanceof Timestamp ? $this->asDateTime($current) : $current; |
|
|
|
|
|
|
91
|
|
|
$original = $original instanceof Timestamp ? $this->asDateTime($original) : $original; |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
return $current == $original; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return parent::originalIsNumericallyEquivalent($key); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get the table qualified key name. |
|
101
|
|
|
* Cassandra does not support the table.column annotation so |
|
102
|
|
|
* we override this |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
8 |
|
public function getQualifiedKeyName() |
|
107
|
|
|
{ |
|
108
|
8 |
|
return $this->getKeyName(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Qualify the given column name by the model's table. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $column |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
2 |
|
public function qualifyColumn($column) |
|
118
|
|
|
{ |
|
119
|
2 |
|
return $column; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Set a given attribute on the model. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $key |
|
126
|
|
|
* @param mixed $value |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
12 |
|
public function setAttribute($key, $value) |
|
130
|
|
|
{ |
|
131
|
|
|
// First we will check for the presence of a mutator for the set operation |
|
132
|
|
|
// which simply lets the developers tweak the attribute as it is set on |
|
133
|
|
|
// the model, such as "json_encoding" an listing of data for storage. |
|
134
|
12 |
|
if ($this->hasSetMutator($key)) { |
|
135
|
|
|
$method = 'set' . Str::studly($key) . 'Attribute'; |
|
136
|
|
|
|
|
137
|
|
|
return $this->{$method}($value); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// If an attribute is listed as a "date", we'll convert it from a DateTime |
|
141
|
|
|
// instance into a form proper for storage on the database tables using |
|
142
|
|
|
// the connection grammar's date format. We will auto set the values. |
|
143
|
12 |
|
elseif ($value !== null && $this->isDateAttribute($key)) { |
|
144
|
12 |
|
$value = $this->fromDateTime($value); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
12 |
|
if ($this->isJsonCastable($key) && !is_null($value)) { |
|
148
|
|
|
$value = $this->castAttributeAsJson($key, $value); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// If this attribute contains a JSON ->, we'll set the proper value in the |
|
152
|
|
|
// attribute's underlying array. This takes care of properly nesting an |
|
153
|
|
|
// attribute in the array's value in the case of deeply nested items. |
|
154
|
12 |
|
if (Str::contains($key, '->')) { |
|
155
|
|
|
return $this->fillJsonAttribute($key, $value); |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
12 |
|
$this->attributes[$key] = $value; |
|
159
|
|
|
|
|
160
|
12 |
|
return $this; |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @inheritdoc |
|
165
|
|
|
*/ |
|
166
|
18 |
|
public function __call($method, $parameters) |
|
167
|
|
|
{ |
|
168
|
|
|
// Unset method |
|
169
|
18 |
|
if ($method == 'unset') { |
|
170
|
|
|
return call_user_func_array([$this, 'drop'], $parameters); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
18 |
|
return parent::__call($method, $parameters); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Create a new Eloquent Collection instance. |
|
178
|
|
|
* |
|
179
|
|
|
* @param Rows $rows |
|
180
|
|
|
* |
|
181
|
|
|
* @return Collection |
|
182
|
|
|
*/ |
|
183
|
13 |
|
public function newCassandraCollection(Rows $rows) |
|
184
|
|
|
{ |
|
185
|
13 |
|
return new Collection($rows, $this); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Determine if the new and old values for a given key are equivalent. |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $key |
|
192
|
|
|
* @param mixed $current |
|
193
|
|
|
* @return bool |
|
194
|
|
|
*/ |
|
195
|
12 |
|
public function originalIsEquivalent($key, $current) |
|
196
|
|
|
{ |
|
197
|
12 |
|
if (!array_key_exists($key, $this->original)) { |
|
198
|
12 |
|
return false; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
2 |
|
$original = $this->getOriginal($key); |
|
202
|
|
|
|
|
203
|
2 |
|
if ($current === $original) { |
|
204
|
2 |
|
return true; |
|
205
|
2 |
|
} elseif (is_null($current)) { |
|
206
|
|
|
return false; |
|
207
|
2 |
|
} elseif ($this->isDateAttribute($key)) { |
|
208
|
2 |
|
return $this->fromDateTime($current) === |
|
209
|
2 |
|
$this->fromDateTime($original); |
|
210
|
1 |
|
} elseif ($this->hasCast($key)) { |
|
211
|
|
|
return $this->castAttribute($key, $current) === |
|
212
|
|
|
$this->castAttribute($key, $original); |
|
213
|
1 |
|
} elseif ($this->isCassandraObject($current)) { |
|
214
|
|
|
return $this->valueFromCassandraObject($current) === |
|
215
|
|
|
$this->valueFromCassandraObject($original); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
1 |
|
return is_numeric($current) && is_numeric($original) |
|
219
|
1 |
|
&& strcmp((string) $current, (string) $original) === 0; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Check if object is instance of any cassandra object types |
|
224
|
|
|
* |
|
225
|
|
|
* @param $obj |
|
226
|
|
|
* @return bool |
|
227
|
|
|
*/ |
|
228
|
1 |
|
protected function isCassandraObject($obj) |
|
229
|
|
|
{ |
|
230
|
1 |
|
if ($obj instanceof \Cassandra\Uuid || |
|
|
|
|
|
|
231
|
1 |
|
$obj instanceof \Cassandra\Date || |
|
|
|
|
|
|
232
|
1 |
|
$obj instanceof \Cassandra\Float || |
|
|
|
|
|
|
233
|
1 |
|
$obj instanceof \Cassandra\Decimal || |
|
|
|
|
|
|
234
|
1 |
|
$obj instanceof \Cassandra\Timestamp || |
|
|
|
|
|
|
235
|
1 |
|
$obj instanceof \Cassandra\Inet || |
|
|
|
|
|
|
236
|
1 |
|
$obj instanceof \Cassandra\Time |
|
|
|
|
|
|
237
|
|
|
) { |
|
238
|
|
|
return true; |
|
239
|
|
|
} else { |
|
240
|
1 |
|
return false; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Check if object is instance of any cassandra object types |
|
246
|
|
|
* |
|
247
|
|
|
* @param $obj |
|
248
|
|
|
* @return bool |
|
249
|
|
|
*/ |
|
250
|
|
|
protected function isCompareableCassandraObject($obj) |
|
251
|
|
|
{ |
|
252
|
|
|
if ($obj instanceof \Cassandra\Uuid || |
|
|
|
|
|
|
253
|
|
|
$obj instanceof \Cassandra\Inet |
|
|
|
|
|
|
254
|
|
|
) { |
|
255
|
|
|
return true; |
|
256
|
|
|
} else { |
|
257
|
|
|
return false; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Returns comparable value from cassandra object type |
|
263
|
|
|
* |
|
264
|
|
|
* @param $obj |
|
265
|
|
|
* @return mixed |
|
266
|
|
|
*/ |
|
267
|
|
|
protected function valueFromCassandraObject($obj) |
|
268
|
|
|
{ |
|
269
|
|
|
$class = get_class($obj); |
|
270
|
|
|
$value = ''; |
|
271
|
|
|
switch ($class) { |
|
272
|
|
|
case 'Cassandra\Date': |
|
273
|
|
|
$value = $obj->seconds(); |
|
274
|
|
|
break; |
|
275
|
|
|
case 'Cassandra\Time': |
|
276
|
|
|
$value = $obj->__toString(); |
|
277
|
|
|
break; |
|
278
|
|
|
case 'Cassandra\Timestamp': |
|
279
|
|
|
$value = $obj->time(); |
|
280
|
|
|
break; |
|
281
|
|
|
case 'Cassandra\Float': |
|
282
|
|
|
$value = $obj->value(); |
|
283
|
|
|
break; |
|
284
|
|
|
case 'Cassandra\Decimal': |
|
285
|
|
|
$value = $obj->value(); |
|
286
|
|
|
break; |
|
287
|
|
|
case 'Cassandra\Inet': |
|
288
|
|
|
$value = $obj->address(); |
|
289
|
|
|
break; |
|
290
|
|
|
case 'Cassandra\Uuid': |
|
291
|
|
|
$value = $obj->uuid(); |
|
292
|
|
|
break; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
return $value; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
} |
|
299
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.