66use DateTime ;
77use Illuminate \Database \Eloquent \Model as BaseModel ;
88use Illuminate \Database \Eloquent \Relations \Relation ;
9+ use Illuminate \Support \Arr ;
910use Illuminate \Support \Str ;
1011use Jenssegers \Mongodb \Query \Builder as QueryBuilder ;
1112use MongoDB \BSON \ObjectID ;
@@ -46,7 +47,7 @@ public function getIdAttribute($value = null)
4647 {
4748 // If we don't have a value for 'id', we will use the Mongo '_id' value.
4849 // 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 )) {
5051 $ value = $ this ->attributes ['_id ' ];
5152 }
5253
@@ -131,12 +132,12 @@ public function getAttribute($key)
131132 }
132133
133134 // Dot notation support.
134- if (str_contains ($ key , '. ' ) and array_has ($ this ->attributes , $ key )) {
135+ if (Str:: contains ($ key , '. ' ) && Arr:: has ($ this ->attributes , $ key )) {
135136 return $ this ->getAttributeValue ($ key );
136137 }
137138
138139 // 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 )) {
140141 return $ this ->getRelationValue ($ key );
141142 }
142143
@@ -149,8 +150,8 @@ public function getAttribute($key)
149150 protected function getAttributeFromArray ($ key )
150151 {
151152 // 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 );
154155 }
155156
156157 return parent ::getAttributeFromArray ($ key );
@@ -162,17 +163,17 @@ protected function getAttributeFromArray($key)
162163 public function setAttribute ($ key , $ value )
163164 {
164165 // Convert _id to ObjectID.
165- if ($ key == '_id ' and is_string ($ value )) {
166+ if ($ key == '_id ' && is_string ($ value )) {
166167 $ builder = $ this ->newBaseQueryBuilder ();
167168
168169 $ value = $ builder ->convertKey ($ value );
169170 } // Support keys in dot notation.
170- elseif (str_contains ($ key , '. ' )) {
171+ elseif (Str:: contains ($ key , '. ' )) {
171172 if (in_array ($ key , $ this ->getDates ()) && $ value ) {
172173 $ value = $ this ->fromDateTime ($ value );
173174 }
174175
175- array_set ($ this ->attributes , $ key , $ value );
176+ Arr:: set ($ this ->attributes , $ key , $ value );
176177
177178 return ;
178179 }
@@ -199,8 +200,8 @@ public function attributesToArray()
199200
200201 // Convert dot-notation dates.
201202 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 )));
204205 }
205206 }
206207
@@ -218,20 +219,36 @@ public function getCasts()
218219 /**
219220 * @inheritdoc
220221 */
221- protected function originalIsNumericallyEquivalent ($ key )
222+ protected function originalIsEquivalent ($ key, $ current )
222223 {
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+ }
225237
226- // Date comparison.
227- if (in_array ($ key , $ this ->getDates ())) {
238+ if ($ this ->isDateAttribute ($ key )) {
228239 $ current = $ current instanceof UTCDateTime ? $ this ->asDateTime ($ current ) : $ current ;
229240 $ original = $ original instanceof UTCDateTime ? $ this ->asDateTime ($ original ) : $ original ;
230241
231242 return $ current == $ original ;
232243 }
233244
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 ;
235252 }
236253
237254 /**
@@ -242,9 +259,7 @@ protected function originalIsNumericallyEquivalent($key)
242259 */
243260 public function drop ($ columns )
244261 {
245- if (!is_array ($ columns )) {
246- $ columns = [$ columns ];
247- }
262+ $ columns = Arr::wrap ($ columns );
248263
249264 // Unset attributes
250265 foreach ($ columns as $ column ) {
@@ -263,16 +278,14 @@ public function push()
263278 if ($ parameters = func_get_args ()) {
264279 $ unique = false ;
265280
266- if (count ($ parameters ) == 3 ) {
281+ if (count ($ parameters ) === 3 ) {
267282 list ($ column , $ values , $ unique ) = $ parameters ;
268283 } else {
269284 list ($ column , $ values ) = $ parameters ;
270285 }
271286
272287 // Do batch push by default.
273- if (!is_array ($ values )) {
274- $ values = [$ values ];
275- }
288+ $ values = Arr::wrap ($ values );
276289
277290 $ query = $ this ->setKeysForSaveQuery ($ this ->newQuery ());
278291
@@ -294,9 +307,7 @@ public function push()
294307 public function pull ($ column , $ values )
295308 {
296309 // Do batch pull by default.
297- if (!is_array ($ values )) {
298- $ values = [$ values ];
299- }
310+ $ values = Arr::wrap ($ values );
300311
301312 $ query = $ this ->setKeysForSaveQuery ($ this ->newQuery ());
302313
@@ -318,11 +329,11 @@ protected function pushAttributeValues($column, array $values, $unique = false)
318329
319330 foreach ($ values as $ value ) {
320331 // 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) )) {
322333 continue ;
323334 }
324335
325- array_push ( $ current, $ value) ;
336+ $ current[] = $ value ;
326337 }
327338
328339 $ this ->attributes [$ column ] = $ current ;
@@ -340,11 +351,13 @@ protected function pullAttributeValues($column, array $values)
340351 {
341352 $ current = $ this ->getAttributeFromArray ($ column ) ?: [];
342353
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 );
345357
346- foreach ($ keys as $ key ) {
347- unset($ current [$ key ]);
358+ foreach ($ keys as $ key ) {
359+ unset($ current [$ key ]);
360+ }
348361 }
349362 }
350363
0 commit comments