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