@@ -93,6 +93,33 @@ public function testBasicCustomCasting()
93
93
$ this ->assertTrue (is_string ($ model ->toArray ()['birthday_at ' ]));
94
94
}
95
95
96
+ public function testGetOriginalWithCastValueObjects ()
97
+ {
98
+ $ model = new TestEloquentModelWithCustomCast ([
99
+ 'address ' => new Address ('110 Kingsbrook St. ' , 'My Childhood House ' )
100
+ ]);
101
+
102
+ $ model ->syncOriginal ();
103
+
104
+ $ model ->address = new Address ('117 Spencer St. ' , 'Another house. ' );
105
+
106
+ $ this ->assertEquals ('110 Kingsbrook St. ' , $ model ->getOriginal ('address ' )->lineOne );
107
+ $ this ->assertEquals ('117 Spencer St. ' , $ model ->address ->lineOne );
108
+
109
+
110
+ $ model = new TestEloquentModelWithCustomCast ([
111
+ 'address ' => new Address ('110 Kingsbrook St. ' , 'My Childhood House ' )
112
+ ]);
113
+
114
+ $ model ->syncOriginal ();
115
+
116
+ $ model ->address = null ;
117
+
118
+ $ this ->assertNull ($ model ->address );
119
+ $ this ->assertInstanceOf (Address::class, $ model ->getOriginal ('address ' ));
120
+ $ this ->assertNull ($ model ->address );
121
+ }
122
+
96
123
public function testOneWayCasting ()
97
124
{
98
125
// CastsInboundAttributes is used for casting that is unidirectional... only use case I can think of is one-way hashing...
@@ -213,11 +240,22 @@ class AddressCaster implements CastsAttributes
213
240
{
214
241
public function get ($ model , $ key , $ value , $ attributes )
215
242
{
243
+ if (is_null ($ attributes ['address_line_one ' ])) {
244
+ return null ;
245
+ }
246
+
216
247
return new Address ($ attributes ['address_line_one ' ], $ attributes ['address_line_two ' ]);
217
248
}
218
249
219
250
public function set ($ model , $ key , $ value , $ attributes )
220
251
{
252
+ if (is_null ($ value )) {
253
+ return [
254
+ 'address_line_one ' => null ,
255
+ 'address_line_two ' => null ,
256
+ ];
257
+ }
258
+
221
259
return ['address_line_one ' => $ value ->lineOne , 'address_line_two ' => $ value ->lineTwo ];
222
260
}
223
261
}
0 commit comments