@@ -189,7 +189,7 @@ public function toArray(bool $onlyChanged = false, bool $cast = true, bool $recu
189
189
* Returns the raw values of the current attributes.
190
190
*
191
191
* @param bool $onlyChanged If true, only return values that have changed since object creation
192
- * @param bool $recursive If true, inner entities will be casted as array as well.
192
+ * @param bool $recursive If true, inner entities will be cast as array as well.
193
193
*/
194
194
public function toRawArray (bool $ onlyChanged = false , bool $ recursive = false ): array
195
195
{
@@ -212,7 +212,7 @@ public function toRawArray(bool $onlyChanged = false, bool $recursive = false):
212
212
}
213
213
214
214
foreach ($ this ->attributes as $ key => $ value ) {
215
- if (! $ this ->hasChanged ($ key )) {
215
+ if (! $ this ->hasChangedAttributes ($ key )) {
216
216
continue ;
217
217
}
218
218
@@ -247,17 +247,40 @@ public function syncOriginal()
247
247
* was created. Or, without a parameter, checks if any
248
248
* properties have changed.
249
249
*
250
- * @param string $key
250
+ * @param string|null $key class property
251
251
*/
252
252
public function hasChanged (?string $ key = null ): bool
253
253
{
254
254
// If no parameter was given then check all attributes
255
255
if ($ key === null ) {
256
- return $ this ->original !== $ this -> attributes ;
256
+ return $ this ->hasChangedAttributes () ;
257
257
}
258
258
259
259
$ key = $ this ->mapProperty ($ key );
260
260
261
+ return $ this ->hasChangedAttributes ($ key );
262
+ }
263
+
264
+ /**
265
+ * Checks a attribute to see if it has changed since the entity
266
+ * was created. Or, without a parameter, checks if any
267
+ * attributes have changed.
268
+ *
269
+ * @param string|null $key key of $this->attributes
270
+ */
271
+ private function hasChangedAttributes (?string $ key = null ): bool
272
+ {
273
+ // If no parameter was given then check all attributes
274
+ if ($ key === null ) {
275
+ foreach ($ this ->attributes as $ key => $ value ) {
276
+ if ($ this ->isChanged ($ key )) {
277
+ return true ;
278
+ }
279
+ }
280
+
281
+ return false ;
282
+ }
283
+
261
284
// Key doesn't exist in either
262
285
if (! array_key_exists ($ key , $ this ->original ) && ! array_key_exists ($ key , $ this ->attributes )) {
263
286
return false ;
@@ -268,7 +291,7 @@ public function hasChanged(?string $key = null): bool
268
291
return true ;
269
292
}
270
293
271
- return $ this ->original [ $ key] !== $ this -> attributes [ $ key ] ;
294
+ return $ this ->isChanged ( $ key) ;
272
295
}
273
296
274
297
/**
@@ -470,6 +493,7 @@ public function __set(string $key, $value = null)
470
493
* $p = $this->getMyProperty()
471
494
*
472
495
* @throws Exception
496
+ * @params string $key class property
473
497
*
474
498
* @return mixed
475
499
*/
@@ -487,7 +511,6 @@ public function __get(string $key)
487
511
if (method_exists ($ this , $ method )) {
488
512
$ result = $ this ->{$ method }();
489
513
}
490
-
491
514
// Otherwise return the protected property
492
515
// if it exists.
493
516
elseif (array_key_exists ($ key , $ this ->attributes )) {
@@ -506,6 +529,30 @@ public function __get(string $key)
506
529
return $ result ;
507
530
}
508
531
532
+ /**
533
+ * Get cast value from the data array.
534
+ *
535
+ * @return mixed|null
536
+ */
537
+ private function _getCastData (string $ key , array $ data )
538
+ {
539
+ $ result = null ;
540
+
541
+ if (array_key_exists ($ key , $ data )) {
542
+ $ result = $ this ->castAs ($ data [$ key ], $ key );
543
+ }
544
+
545
+ return $ result ;
546
+ }
547
+
548
+ /**
549
+ * Check if the key value is changed.
550
+ */
551
+ private function isChanged (string $ key ): bool
552
+ {
553
+ return $ this ->_getCastData ($ key , $ this ->original ) !== $ this ->_getCastData ($ key , $ this ->attributes );
554
+ }
555
+
509
556
/**
510
557
* Returns true if a property exists names $key, or a getter method
511
558
* exists named like for __get().
0 commit comments