2
2
3
3
namespace Illuminate \Tests \Integration \Database ;
4
4
5
+ use GMP ;
5
6
use Illuminate \Contracts \Database \Eloquent \CastsAttributes ;
7
+ use Illuminate \Contracts \Database \Eloquent \SerializesCastableAttributes ;
6
8
use Illuminate \Database \Capsule \Manager as DB ;
7
9
use Illuminate \Database \Eloquent \Model as Eloquent ;
8
10
use Illuminate \Database \Schema \Blueprint ;
@@ -40,6 +42,7 @@ public function createSchema()
40
42
$ table ->increments ('id ' );
41
43
$ table ->string ('address_line_one ' );
42
44
$ table ->string ('address_line_two ' );
45
+ $ table ->integer ('amount ' );
43
46
$ table ->string ('string_field ' );
44
47
$ table ->timestamps ();
45
48
});
@@ -63,6 +66,7 @@ public function testSavingCastedAttributesToDatabase()
63
66
/** @var \Illuminate\Tests\Integration\Database\CustomCasts $model */
64
67
$ model = CustomCasts::create ([
65
68
'address ' => new AddressModel ('address_line_one_value ' , 'address_line_two_value ' ),
69
+ 'amount ' => gmp_init ('1000 ' , 10 ),
66
70
'string_field ' => null ,
67
71
]);
68
72
@@ -72,6 +76,8 @@ public function testSavingCastedAttributesToDatabase()
72
76
$ this ->assertSame ('address_line_two_value ' , $ model ->getOriginal ('address_line_two ' ));
73
77
$ this ->assertSame ('address_line_two_value ' , $ model ->getAttribute ('address_line_two ' ));
74
78
79
+ $ this ->assertSame ('1000 ' , $ model ->getRawOriginal ('amount ' ));
80
+
75
81
$ this ->assertNull ($ model ->getOriginal ('string_field ' ));
76
82
$ this ->assertNull ($ model ->getAttribute ('string_field ' ));
77
83
$ this ->assertSame ('' , $ model ->getRawOriginal ('string_field ' ));
@@ -80,20 +86,23 @@ public function testSavingCastedAttributesToDatabase()
80
86
$ another_model = CustomCasts::create ([
81
87
'address_line_one ' => 'address_line_one_value ' ,
82
88
'address_line_two ' => 'address_line_two_value ' ,
89
+ 'amount ' => gmp_init ('500 ' , 10 ),
83
90
'string_field ' => 'string_value ' ,
84
91
]);
85
92
86
93
$ this ->assertInstanceOf (AddressModel::class, $ another_model ->address );
87
94
88
95
$ this ->assertSame ('address_line_one_value ' , $ model ->address ->lineOne );
89
96
$ this ->assertSame ('address_line_two_value ' , $ model ->address ->lineTwo );
97
+ $ this ->assertInstanceOf (GMP ::class, $ model ->amount );
90
98
}
91
99
92
100
public function testInvalidArgumentExceptionOnInvalidValue ()
93
101
{
94
102
/** @var \Illuminate\Tests\Integration\Database\CustomCasts $model */
95
103
$ model = CustomCasts::create ([
96
104
'address ' => new AddressModel ('address_line_one_value ' , 'address_line_two_value ' ),
105
+ 'amount ' => gmp_init ('1000 ' , 10 ),
97
106
'string_field ' => 'string_value ' ,
98
107
]);
99
108
@@ -111,6 +120,7 @@ public function testInvalidArgumentExceptionOnNull()
111
120
/** @var \Illuminate\Tests\Integration\Database\CustomCasts $model */
112
121
$ model = CustomCasts::create ([
113
122
'address ' => new AddressModel ('address_line_one_value ' , 'address_line_two_value ' ),
123
+ 'amount ' => gmp_init ('1000 ' , 10 ),
114
124
'string_field ' => 'string_value ' ,
115
125
]);
116
126
@@ -123,6 +133,27 @@ public function testInvalidArgumentExceptionOnNull()
123
133
$ this ->assertSame ('address_line_two_value ' , $ model ->address ->lineTwo );
124
134
}
125
135
136
+ public function testModelsWithCustomCastsCanBeConvertedToArrays ()
137
+ {
138
+ /** @var \Illuminate\Tests\Integration\Database\CustomCasts $model */
139
+ $ model = CustomCasts::create ([
140
+ 'address ' => new AddressModel ('address_line_one_value ' , 'address_line_two_value ' ),
141
+ 'amount ' => gmp_init ('1000 ' , 10 ),
142
+ 'string_field ' => 'string_value ' ,
143
+ ]);
144
+
145
+ // Ensure model values remain unchanged
146
+ $ this ->assertSame ([
147
+ 'address_line_one ' => 'address_line_one_value ' ,
148
+ 'address_line_two ' => 'address_line_two_value ' ,
149
+ 'amount ' => '1000 ' ,
150
+ 'string_field ' => 'string_value ' ,
151
+ 'updated_at ' => $ model ->updated_at ->toJSON (),
152
+ 'created_at ' => $ model ->created_at ->toJSON (),
153
+ 'id ' => 1 ,
154
+ ], $ model ->toArray ());
155
+ }
156
+
126
157
/**
127
158
* Get a database connection instance.
128
159
*
@@ -188,6 +219,51 @@ public function set($model, $key, $value, $attributes)
188
219
}
189
220
}
190
221
222
+ class GMPCast implements CastsAttributes, SerializesCastableAttributes
223
+ {
224
+ /**
225
+ * Cast the given value.
226
+ *
227
+ * @param \Illuminate\Database\Eloquent\Model $model
228
+ * @param string $key
229
+ * @param string $value
230
+ * @param array $attributes
231
+ * @return string|null
232
+ */
233
+ public function get ($ model , $ key , $ value , $ attributes )
234
+ {
235
+ return gmp_init ($ value , 10 );
236
+ }
237
+
238
+ /**
239
+ * Prepare the given value for storage.
240
+ *
241
+ * @param \Illuminate\Database\Eloquent\Model $model
242
+ * @param string $key
243
+ * @param string|null $value
244
+ * @param array $attributes
245
+ * @return string
246
+ */
247
+ public function set ($ model , $ key , $ value , $ attributes )
248
+ {
249
+ return gmp_strval ($ value , 10 );
250
+ }
251
+
252
+ /**
253
+ * Serialize the attribute when converting the model to an array.
254
+ *
255
+ * @param \Illuminate\Database\Eloquent\Model $model
256
+ * @param string $key
257
+ * @param mixed $value
258
+ * @param array $attributes
259
+ * @return mixed
260
+ */
261
+ public function serialize ($ model , string $ key , $ value , array $ attributes )
262
+ {
263
+ return gmp_strval ($ value , 10 );
264
+ }
265
+ }
266
+
191
267
class NonNullableString implements CastsAttributes
192
268
{
193
269
/**
@@ -239,6 +315,7 @@ class CustomCasts extends Eloquent
239
315
*/
240
316
protected $ casts = [
241
317
'address ' => AddressCast::class,
318
+ 'amount ' => GMPCast::class,
242
319
'string_field ' => NonNullableString::class,
243
320
];
244
321
}
0 commit comments