File tree 2 files changed +21
-0
lines changed
2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,11 @@ public function getAttribute($key)
23
23
// Allow other mutators and such to do their work first.
24
24
$ value = parent ::getAttribute ($ key );
25
25
26
+ // Don't cast empty $value.
27
+ if ($ value === null || $ value === '' ) {
28
+ return null ;
29
+ }
30
+
26
31
// Cache the instantiated value for future access.
27
32
// This allows tests such as ($model->casted === $model->casted) to be true.
28
33
if (!$ this ->isValueObjectCached ($ key )) {
Original file line number Diff line number Diff line change @@ -123,4 +123,20 @@ public function testModelToJsonWithValueObjects()
123
123
$ this ->assertJson ($ json );
124
124
$ this ->assertJsonStringEqualsJsonString (json_encode (['email ' => $ email ]), $ json );
125
125
}
126
+
127
+ public function testNullValuesDontGetCast ()
128
+ {
129
+ $ user = new UserModel ();
130
+
131
+ $ this ->assertNull ($ user ->email );
132
+ }
133
+
134
+ public function testEmptyStringValuesDontGetCast ()
135
+ {
136
+ $ user = new UserModel ();
137
+
138
+ $ user ->setInternalAttributes (['email ' => '' ]);
139
+
140
+ $ this ->assertNull ($ user ->email );
141
+ }
126
142
}
You can’t perform that action at this time.
0 commit comments