|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace fuitad\LaravelCassandra\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Cassandra\Timestamp; |
|
7
|
|
|
use fuitad\LaravelCassandra\Query\Builder as QueryBuilder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model as BaseModel; |
|
9
|
|
|
|
|
10
|
|
|
abstract class Model extends BaseModel |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Indicates if the IDs are auto-incrementing. |
|
14
|
|
|
* This is not possible in cassandra so we override this |
|
15
|
|
|
* |
|
16
|
|
|
* @var bool |
|
17
|
|
|
*/ |
|
18
|
|
|
public $incrementing = false; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @inheritdoc |
|
22
|
|
|
*/ |
|
23
|
|
|
public function newEloquentBuilder($query) |
|
24
|
|
|
{ |
|
25
|
|
|
return new Builder($query); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritdoc |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function newBaseQueryBuilder() |
|
32
|
|
|
{ |
|
33
|
|
|
$connection = $this->getConnection(); |
|
34
|
|
|
|
|
35
|
|
|
return new QueryBuilder($connection, $connection->getPostProcessor()); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
*/ |
|
41
|
|
|
public function freshTimestamp() |
|
42
|
|
|
{ |
|
43
|
|
|
return new Timestamp(); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
49
|
|
|
public function fromDateTime($value) |
|
50
|
|
|
{ |
|
51
|
|
|
// If the value is already a Timestamp instance, we don't need to parse it. |
|
52
|
|
|
if ($value instanceof Timestamp) { |
|
|
|
|
|
|
53
|
|
|
return $value; |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Let Eloquent convert the value to a DateTime instance. |
|
57
|
|
|
if (!$value instanceof DateTime) { |
|
|
|
|
|
|
58
|
|
|
$value = parent::asDateTime($value); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return new Timestamp($value->getTimestamp() * 1000); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritdoc |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function asDateTime($value) |
|
68
|
|
|
{ |
|
69
|
|
|
// Convert UTCDateTime instances. |
|
70
|
|
|
if ($value instanceof Timestamp) { |
|
|
|
|
|
|
71
|
|
|
return Carbon::instance($value->toDateTime()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return parent::asDateTime($value); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritdoc |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function originalIsNumericallyEquivalent($key) |
|
81
|
|
|
{ |
|
82
|
|
|
$current = $this->attributes[$key]; |
|
83
|
|
|
$original = $this->original[$key]; |
|
84
|
|
|
|
|
85
|
|
|
// Date comparison. |
|
86
|
|
|
if (in_array($key, $this->getDates())) { |
|
87
|
|
|
$current = $current instanceof Timestamp ? $this->asDateTime($current) : $current; |
|
|
|
|
|
|
88
|
|
|
$original = $original instanceof Timestamp ? $this->asDateTime($original) : $original; |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
return $current == $original; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return parent::originalIsNumericallyEquivalent($key); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get the table qualified key name. |
|
98
|
|
|
* Cassandra does not support the table.column annotation so |
|
99
|
|
|
* we override this |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getQualifiedKeyName() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->getKeyName(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritdoc |
|
110
|
|
|
*/ |
|
111
|
|
|
public function __call($method, $parameters) |
|
112
|
|
|
{ |
|
113
|
|
|
// Unset method |
|
114
|
|
|
if ($method == 'unset') { |
|
115
|
|
|
return call_user_func_array([$this, 'drop'], $parameters); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return parent::__call($method, $parameters); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
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.