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