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