@@ -103,6 +103,15 @@ public function testBasicCustomCasting()
103
103
$ model = new TestEloquentModelWithCustomCast ;
104
104
$ model ->birthday_at = now ();
105
105
$ this ->assertIsString ($ model ->toArray ()['birthday_at ' ]);
106
+
107
+ $ model = new TestEloquentModelWithCustomCast ;
108
+ $ now = now ()->toImmutable ();
109
+ $ model ->anniversary_on_with_object_caching = $ now ;
110
+ $ model ->anniversary_on_without_object_caching = $ now ;
111
+ $ this ->assertSame ($ now , $ model ->anniversary_on_with_object_caching );
112
+ $ this ->assertSame ('UTC ' , $ model ->anniversary_on_with_object_caching ->format ('e ' ));
113
+ $ this ->assertNotSame ($ now , $ model ->anniversary_on_without_object_caching );
114
+ $ this ->assertNotSame ('UTC ' , $ model ->anniversary_on_without_object_caching ->format ('e ' ));
106
115
}
107
116
108
117
public function testGetOriginalWithCastValueObjects ()
@@ -299,6 +308,8 @@ class TestEloquentModelWithCustomCast extends Model
299
308
'value_object_caster_with_caster_instance ' => ValueObjectWithCasterInstance::class,
300
309
'undefined_cast_column ' => UndefinedCast::class,
301
310
'birthday_at ' => DateObjectCaster::class,
311
+ 'anniversary_on_with_object_caching ' => DateTimezoneCasterWithObjectCaching::class.':America/New_York ' ,
312
+ 'anniversary_on_without_object_caching ' => DateTimezoneCasterWithoutObjectCaching::class.':America/New_York ' ,
302
313
];
303
314
}
304
315
@@ -584,3 +595,25 @@ public function set($model, $key, $value, $attributes)
584
595
return $ value ->format ('Y-m-d ' );
585
596
}
586
597
}
598
+
599
+ class DateTimezoneCasterWithObjectCaching implements CastsAttributes
600
+ {
601
+ public function __construct (private string $ timezone = 'UTC ' )
602
+ {
603
+ }
604
+
605
+ public function get ($ model , $ key , $ value , $ attributes )
606
+ {
607
+ return Carbon::parse ($ value , $ this ->timezone );
608
+ }
609
+
610
+ public function set ($ model , $ key , $ value , $ attributes )
611
+ {
612
+ return $ value ->timezone ($ this ->timezone )->format ('Y-m-d ' );
613
+ }
614
+ }
615
+
616
+ class DateTimezoneCasterWithoutObjectCaching extends DateTimezoneCasterWithObjectCaching
617
+ {
618
+ public bool $ withoutObjectCaching = true ;
619
+ }
0 commit comments