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
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
|
11
|
|
|
abstract class Model extends BaseModel |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Indicates if the IDs are auto-incrementing. |
15
|
|
|
* This is not possible in cassandra so we override this |
16
|
|
|
* |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
public $incrementing = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @inheritdoc |
23
|
|
|
*/ |
24
|
|
|
public function newEloquentBuilder($query) |
25
|
|
|
{ |
26
|
|
|
return new Builder($query); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
protected function newBaseQueryBuilder() |
33
|
|
|
{ |
34
|
|
|
$connection = $this->getConnection(); |
35
|
|
|
|
36
|
|
|
return new QueryBuilder($connection, null, $connection->getPostProcessor()); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public function freshTimestamp() |
43
|
|
|
{ |
44
|
|
|
return new Timestamp(); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
public function fromDateTime($value) |
51
|
|
|
{ |
52
|
|
|
// If the value is already a Timestamp instance, we don't need to parse it. |
53
|
|
|
if ($value instanceof Timestamp) { |
|
|
|
|
54
|
|
|
return $value; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Let Eloquent convert the value to a DateTime instance. |
58
|
|
|
if (!$value instanceof DateTime) { |
|
|
|
|
59
|
|
|
$value = parent::asDateTime($value); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return new Timestamp($value->getTimestamp() * 1000); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
protected function asDateTime($value) |
69
|
|
|
{ |
70
|
|
|
// Convert UTCDateTime instances. |
71
|
|
|
if ($value instanceof Timestamp) { |
|
|
|
|
72
|
|
|
return Carbon::instance($value->toDateTime()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return parent::asDateTime($value); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
protected function originalIsNumericallyEquivalent($key) |
82
|
|
|
{ |
83
|
|
|
$current = $this->attributes[$key]; |
84
|
|
|
$original = $this->original[$key]; |
85
|
|
|
|
86
|
|
|
// Date comparison. |
87
|
|
|
if (in_array($key, $this->getDates())) { |
88
|
|
|
$current = $current instanceof Timestamp ? $this->asDateTime($current) : $current; |
|
|
|
|
89
|
|
|
$original = $original instanceof Timestamp ? $this->asDateTime($original) : $original; |
|
|
|
|
90
|
|
|
|
91
|
|
|
return $current == $original; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return parent::originalIsNumericallyEquivalent($key); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the table qualified key name. |
99
|
|
|
* Cassandra does not support the table.column annotation so |
100
|
|
|
* we override this |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getQualifiedKeyName() |
105
|
|
|
{ |
106
|
|
|
return $this->getKeyName(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set a given attribute on the model. |
111
|
|
|
* |
112
|
|
|
* @param string $key |
113
|
|
|
* @param mixed $value |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function setAttribute($key, $value) |
117
|
|
|
{ |
118
|
|
|
// First we will check for the presence of a mutator for the set operation |
119
|
|
|
// which simply lets the developers tweak the attribute as it is set on |
120
|
|
|
// the model, such as "json_encoding" an listing of data for storage. |
121
|
|
|
if ($this->hasSetMutator($key)) { |
122
|
|
|
$method = 'set'.Str::studly($key).'Attribute'; |
123
|
|
|
|
124
|
|
|
return $this->{$method}($value); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// If an attribute is listed as a "date", we'll convert it from a DateTime |
128
|
|
|
// instance into a form proper for storage on the database tables using |
129
|
|
|
// the connection grammar's date format. We will auto set the values. |
130
|
|
|
elseif ($value !== null && $this->isDateAttribute($key)) { |
131
|
|
|
$value = $this->fromDateTime($value); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($this->isJsonCastable($key) && ! is_null($value)) { |
135
|
|
|
$value = $this->castAttributeAsJson($key, $value); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// If this attribute contains a JSON ->, we'll set the proper value in the |
139
|
|
|
// attribute's underlying array. This takes care of properly nesting an |
140
|
|
|
// attribute in the array's value in the case of deeply nested items. |
141
|
|
|
if (Str::contains($key, '->')) { |
142
|
|
|
return $this->fillJsonAttribute($key, $value); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->attributes[$key] = $value; |
146
|
|
|
|
147
|
|
|
return $this; |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @inheritdoc |
152
|
|
|
*/ |
153
|
|
|
public function __call($method, $parameters) |
154
|
|
|
{ |
155
|
|
|
// Unset method |
156
|
|
|
if ($method == 'unset') { |
157
|
|
|
return call_user_func_array([$this, 'drop'], $parameters); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return parent::__call($method, $parameters); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
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.