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