6
6
use DateTime ;
7
7
use Illuminate \Database \Eloquent \Model as BaseModel ;
8
8
use Illuminate \Database \Eloquent \Relations \Relation ;
9
+ use Illuminate \Support \Arr ;
9
10
use Illuminate \Support \Str ;
10
11
use Jenssegers \Mongodb \Query \Builder as QueryBuilder ;
11
12
use MongoDB \BSON \ObjectID ;
@@ -46,7 +47,7 @@ public function getIdAttribute($value = null)
46
47
{
47
48
// If we don't have a value for 'id', we will use the Mongo '_id' value.
48
49
// This allows us to work with models in a more sql-like way.
49
- if (!$ value and array_key_exists ('_id ' , $ this ->attributes )) {
50
+ if (!$ value && array_key_exists ('_id ' , $ this ->attributes )) {
50
51
$ value = $ this ->attributes ['_id ' ];
51
52
}
52
53
@@ -131,12 +132,12 @@ public function getAttribute($key)
131
132
}
132
133
133
134
// Dot notation support.
134
- if (str_contains ($ key , '. ' ) and array_has ($ this ->attributes , $ key )) {
135
+ if (Str:: contains ($ key , '. ' ) && Arr:: has ($ this ->attributes , $ key )) {
135
136
return $ this ->getAttributeValue ($ key );
136
137
}
137
138
138
139
// This checks for embedded relation support.
139
- if (method_exists ($ this , $ key ) and !method_exists (self ::class, $ key )) {
140
+ if (method_exists ($ this , $ key ) && !method_exists (self ::class, $ key )) {
140
141
return $ this ->getRelationValue ($ key );
141
142
}
142
143
@@ -149,8 +150,8 @@ public function getAttribute($key)
149
150
protected function getAttributeFromArray ($ key )
150
151
{
151
152
// Support keys in dot notation.
152
- if (str_contains ($ key , '. ' )) {
153
- return array_get ($ this ->attributes , $ key );
153
+ if (Str:: contains ($ key , '. ' )) {
154
+ return Arr:: get ($ this ->attributes , $ key );
154
155
}
155
156
156
157
return parent ::getAttributeFromArray ($ key );
@@ -162,17 +163,17 @@ protected function getAttributeFromArray($key)
162
163
public function setAttribute ($ key , $ value )
163
164
{
164
165
// Convert _id to ObjectID.
165
- if ($ key == '_id ' and is_string ($ value )) {
166
+ if ($ key == '_id ' && is_string ($ value )) {
166
167
$ builder = $ this ->newBaseQueryBuilder ();
167
168
168
169
$ value = $ builder ->convertKey ($ value );
169
170
} // Support keys in dot notation.
170
- elseif (str_contains ($ key , '. ' )) {
171
+ elseif (Str:: contains ($ key , '. ' )) {
171
172
if (in_array ($ key , $ this ->getDates ()) && $ value ) {
172
173
$ value = $ this ->fromDateTime ($ value );
173
174
}
174
175
175
- array_set ($ this ->attributes , $ key , $ value );
176
+ Arr:: set ($ this ->attributes , $ key , $ value );
176
177
177
178
return ;
178
179
}
@@ -199,8 +200,8 @@ public function attributesToArray()
199
200
200
201
// Convert dot-notation dates.
201
202
foreach ($ this ->getDates () as $ key ) {
202
- if (str_contains ($ key , '. ' ) and array_has ($ attributes , $ key )) {
203
- array_set ($ attributes , $ key , (string ) $ this ->asDateTime (array_get ($ attributes , $ key )));
203
+ if (Str:: contains ($ key , '. ' ) && Arr:: has ($ attributes , $ key )) {
204
+ Arr:: set ($ attributes , $ key , (string ) $ this ->asDateTime (Arr:: get ($ attributes , $ key )));
204
205
}
205
206
}
206
207
@@ -218,20 +219,36 @@ public function getCasts()
218
219
/**
219
220
* @inheritdoc
220
221
*/
221
- protected function originalIsNumericallyEquivalent ($ key )
222
+ protected function originalIsEquivalent ($ key, $ current )
222
223
{
223
- $ current = $ this ->attributes [$ key ];
224
- $ original = $ this ->original [$ key ];
224
+ if (!array_key_exists ($ key , $ this ->original )) {
225
+ return false ;
226
+ }
227
+
228
+ $ original = $ this ->getOriginal ($ key );
229
+
230
+ if ($ current === $ original ) {
231
+ return true ;
232
+ }
233
+
234
+ if (null === $ current ) {
235
+ return false ;
236
+ }
225
237
226
- // Date comparison.
227
- if (in_array ($ key , $ this ->getDates ())) {
238
+ if ($ this ->isDateAttribute ($ key )) {
228
239
$ current = $ current instanceof UTCDateTime ? $ this ->asDateTime ($ current ) : $ current ;
229
240
$ original = $ original instanceof UTCDateTime ? $ this ->asDateTime ($ original ) : $ original ;
230
241
231
242
return $ current == $ original ;
232
243
}
233
244
234
- return parent ::originalIsNumericallyEquivalent ($ key );
245
+ if ($ this ->hasCast ($ key )) {
246
+ return $ this ->castAttribute ($ key , $ current ) ===
247
+ $ this ->castAttribute ($ key , $ original );
248
+ }
249
+
250
+ return is_numeric ($ current ) && is_numeric ($ original )
251
+ && strcmp ((string ) $ current , (string ) $ original ) === 0 ;
235
252
}
236
253
237
254
/**
@@ -242,9 +259,7 @@ protected function originalIsNumericallyEquivalent($key)
242
259
*/
243
260
public function drop ($ columns )
244
261
{
245
- if (!is_array ($ columns )) {
246
- $ columns = [$ columns ];
247
- }
262
+ $ columns = Arr::wrap ($ columns );
248
263
249
264
// Unset attributes
250
265
foreach ($ columns as $ column ) {
@@ -263,16 +278,14 @@ public function push()
263
278
if ($ parameters = func_get_args ()) {
264
279
$ unique = false ;
265
280
266
- if (count ($ parameters ) == 3 ) {
281
+ if (count ($ parameters ) === 3 ) {
267
282
list ($ column , $ values , $ unique ) = $ parameters ;
268
283
} else {
269
284
list ($ column , $ values ) = $ parameters ;
270
285
}
271
286
272
287
// Do batch push by default.
273
- if (!is_array ($ values )) {
274
- $ values = [$ values ];
275
- }
288
+ $ values = Arr::wrap ($ values );
276
289
277
290
$ query = $ this ->setKeysForSaveQuery ($ this ->newQuery ());
278
291
@@ -294,9 +307,7 @@ public function push()
294
307
public function pull ($ column , $ values )
295
308
{
296
309
// Do batch pull by default.
297
- if (!is_array ($ values )) {
298
- $ values = [$ values ];
299
- }
310
+ $ values = Arr::wrap ($ values );
300
311
301
312
$ query = $ this ->setKeysForSaveQuery ($ this ->newQuery ());
302
313
@@ -318,11 +329,11 @@ protected function pushAttributeValues($column, array $values, $unique = false)
318
329
319
330
foreach ($ values as $ value ) {
320
331
// Don't add duplicate values when we only want unique values.
321
- if ($ unique and in_array ($ value , $ current )) {
332
+ if ($ unique && (! is_array ( $ current ) || in_array ($ value , $ current) )) {
322
333
continue ;
323
334
}
324
335
325
- array_push ( $ current, $ value) ;
336
+ $ current[] = $ value ;
326
337
}
327
338
328
339
$ this ->attributes [$ column ] = $ current ;
@@ -340,11 +351,13 @@ protected function pullAttributeValues($column, array $values)
340
351
{
341
352
$ current = $ this ->getAttributeFromArray ($ column ) ?: [];
342
353
343
- foreach ($ values as $ value ) {
344
- $ keys = array_keys ($ current , $ value );
354
+ if (is_array ($ current )) {
355
+ foreach ($ values as $ value ) {
356
+ $ keys = array_keys ($ current , $ value );
345
357
346
- foreach ($ keys as $ key ) {
347
- unset($ current [$ key ]);
358
+ foreach ($ keys as $ key ) {
359
+ unset($ current [$ key ]);
360
+ }
348
361
}
349
362
}
350
363
0 commit comments