1 | <?php |
||
13 | abstract class Model extends BaseModel |
||
14 | { |
||
15 | use CassandraTypesTrait; |
||
16 | |||
17 | /** |
||
18 | * The connection name for the model. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $connection = 'cassandra'; |
||
23 | |||
24 | /** |
||
25 | * Indicates if the IDs are auto-incrementing. |
||
26 | * This is not possible in cassandra so we override this |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | public $incrementing = false; |
||
31 | |||
32 | /** |
||
33 | * @inheritdoc |
||
34 | */ |
||
35 | 55 | public function newEloquentBuilder($query) |
|
36 | { |
||
37 | 55 | return new Builder($query); |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * @inheritdoc |
||
42 | */ |
||
43 | 55 | protected function newBaseQueryBuilder() |
|
44 | { |
||
45 | 55 | $connection = $this->getConnection(); |
|
46 | |||
47 | 55 | return new QueryBuilder($connection, null, $connection->getPostProcessor()); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @inheritdoc |
||
52 | */ |
||
53 | 13 | public function freshTimestamp() |
|
54 | { |
||
55 | 13 | return new Timestamp(); |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * @inheritdoc |
||
60 | */ |
||
61 | 13 | public function fromDateTime($value) |
|
62 | { |
||
63 | // If the value is already a Timestamp instance, we don't need to parse it. |
||
64 | 13 | if ($value instanceof Timestamp) { |
|
65 | 13 | return $value; |
|
66 | } |
||
67 | |||
68 | // Let Eloquent convert the value to a DateTime instance. |
||
69 | 1 | if (!$value instanceof \DateTime) { |
|
70 | 1 | $value = parent::asDateTime($value); |
|
71 | } |
||
72 | |||
73 | 1 | return new Timestamp($value->getTimestamp() * 1000); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @inheritdoc |
||
78 | */ |
||
79 | 6 | protected function asDateTime($value) |
|
80 | { |
||
81 | // Convert UTCDateTime instances. |
||
82 | 6 | if ($value instanceof Timestamp) { |
|
83 | 6 | return Carbon::instance($value->toDateTime()); |
|
84 | } |
||
85 | |||
86 | return parent::asDateTime($value); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get the table qualified key name. |
||
91 | * Cassandra does not support the table.column annotation so |
||
92 | * we override this |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 8 | public function getQualifiedKeyName() |
|
97 | { |
||
98 | 8 | return $this->getKeyName(); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Qualify the given column name by the model's table. |
||
103 | * |
||
104 | * @param string $column |
||
105 | * @return string |
||
106 | */ |
||
107 | 2 | public function qualifyColumn($column) |
|
108 | { |
||
109 | 2 | return $column; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | */ |
||
115 | 55 | public function __call($method, $parameters) |
|
116 | { |
||
117 | // Unset method |
||
118 | 55 | if ($method == 'unset') { |
|
119 | return call_user_func_array([$this, 'drop'], $parameters); |
||
120 | } |
||
121 | |||
122 | 55 | return parent::__call($method, $parameters); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Create a new Eloquent Collection instance. |
||
127 | * |
||
128 | * @param Rows|array $rows |
||
129 | * |
||
130 | * @return Collection |
||
131 | * |
||
132 | * @throws \Exception |
||
133 | */ |
||
134 | 50 | public function newCassandraCollection($rows) |
|
135 | { |
||
136 | 50 | if (!is_array($rows) && !$rows instanceof Rows) { |
|
137 | throw new \Exception('Wrong type to create collection');//TODO: customize error |
||
138 | } |
||
139 | |||
140 | 50 | $items = []; |
|
141 | 50 | foreach ($rows as $row) { |
|
142 | 47 | $items[] = $this->newFromBuilder($row); |
|
143 | } |
||
144 | |||
145 | 50 | $collection = new Collection($items); |
|
146 | |||
147 | 50 | if ($rows instanceof Rows) { |
|
148 | 19 | $collection->setRowsInstance($rows); |
|
149 | } |
||
150 | |||
151 | 50 | return $collection; |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * Determine if the new and old values for a given key are equivalent. |
||
156 | * |
||
157 | * @param string $key |
||
158 | * @param mixed $current |
||
159 | * @return bool |
||
160 | */ |
||
161 | 13 | public function originalIsEquivalent($key, $current) |
|
162 | { |
||
163 | 13 | if (!array_key_exists($key, $this->original)) { |
|
164 | 13 | return false; |
|
165 | } |
||
166 | |||
167 | 2 | $original = $this->getOriginal($key); |
|
168 | |||
169 | 2 | if ($current === $original) { |
|
170 | 2 | return true; |
|
171 | 2 | } elseif (is_null($current)) { |
|
172 | return false; |
||
173 | 2 | } elseif ($this->isDateAttribute($key)) { |
|
174 | 2 | return $this->fromDateTime($current) === |
|
175 | 2 | $this->fromDateTime($original); |
|
176 | 1 | } elseif ($this->hasCast($key)) { |
|
177 | return $this->castAttribute($key, $current) === |
||
178 | $this->castAttribute($key, $original); |
||
179 | 1 | } elseif ($this->isCassandraValueObject($current)) { |
|
180 | return $this->valueFromCassandraObject($current) === |
||
181 | $this->valueFromCassandraObject($original); |
||
182 | } |
||
183 | |||
184 | 1 | return is_numeric($current) && is_numeric($original) |
|
185 | 1 | && strcmp((string) $current, (string) $original) === 0; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get the value of the model's primary key. |
||
190 | * |
||
191 | * @return mixed |
||
192 | */ |
||
193 | 16 | public function getKey() |
|
203 | |||
204 | } |
||
205 |
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.