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
|
|
|
* @inheritdoc |
13
|
|
|
*/ |
14
|
|
|
public function newEloquentBuilder($query) |
15
|
|
|
{ |
16
|
|
|
return new Builder($query); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @inheritdoc |
21
|
|
|
*/ |
22
|
|
|
protected function newBaseQueryBuilder() |
23
|
|
|
{ |
24
|
|
|
$connection = $this->getConnection(); |
25
|
|
|
|
26
|
|
|
return new QueryBuilder($connection, $connection->getPostProcessor()); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
public function freshTimestamp() |
33
|
|
|
{ |
34
|
|
|
return new Timestamp(); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
|
public function fromDateTime($value) |
41
|
|
|
{ |
42
|
|
|
// If the value is already a Timestamp instance, we don't need to parse it. |
43
|
|
|
if ($value instanceof Timestamp) { |
|
|
|
|
44
|
|
|
return $value; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Let Eloquent convert the value to a DateTime instance. |
48
|
|
|
if (!$value instanceof DateTime) { |
|
|
|
|
49
|
|
|
$value = parent::asDateTime($value); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return new Timestamp($value->getTimestamp() * 1000); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
protected function asDateTime($value) |
58
|
|
|
{ |
59
|
|
|
// Convert UTCDateTime instances. |
60
|
|
|
if ($value instanceof Timestamp) { |
|
|
|
|
61
|
|
|
return parent::asDateTime($value->toDateTime()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return parent::asDateTime($value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
protected function originalIsNumericallyEquivalent($key) |
71
|
|
|
{ |
72
|
|
|
$current = $this->attributes[$key]; |
73
|
|
|
$original = $this->original[$key]; |
74
|
|
|
|
75
|
|
|
// Date comparison. |
76
|
|
|
if (in_array($key, $this->getDates())) { |
77
|
|
|
$current = $current instanceof Timestamp ? $this->asDateTime($current) : $current; |
|
|
|
|
78
|
|
|
$original = $original instanceof Timestamp ? $this->asDateTime($original) : $original; |
|
|
|
|
79
|
|
|
|
80
|
|
|
return $current == $original; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return parent::originalIsNumericallyEquivalent($key); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public function __call($method, $parameters) |
90
|
|
|
{ |
91
|
|
|
// Unset method |
92
|
|
|
if ($method == 'unset') { |
93
|
|
|
return call_user_func_array([$this, 'drop'], $parameters); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return parent::__call($method, $parameters); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.