Skip to content

Commit 0ce1f41

Browse files
committed
Merge branch 'a-bashtannik/9.x' into 9.x
2 parents ba805e8 + 05846e7 commit 0ce1f41

File tree

3 files changed

+116
-7
lines changed

3 files changed

+116
-7
lines changed

src/Illuminate/Database/Eloquent/Casts/AsArrayObject.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ public static function castUsing(array $arguments)
1919
{
2020
public function get($model, $key, $value, $attributes)
2121
{
22-
return isset($attributes[$key]) ? new ArrayObject(json_decode($attributes[$key], true)) : null;
22+
if (! isset($attributes[$key])) {
23+
return;
24+
}
25+
26+
$data = json_decode($attributes[$key], true);
27+
28+
return is_array($data) ? new ArrayObject($data) : null;
2329
}
2430

2531
public function set($model, $key, $value, $attributes)

src/Illuminate/Database/Eloquent/Casts/AsCollection.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ public static function castUsing(array $arguments)
2020
{
2121
public function get($model, $key, $value, $attributes)
2222
{
23-
return isset($attributes[$key]) ? new Collection(json_decode($attributes[$key], true)) : null;
23+
if (! isset($attributes[$key])) {
24+
return;
25+
}
26+
27+
$data = json_decode($attributes[$key], true);
28+
29+
return is_array($data) ? new Collection($data) : null;
2430
}
2531

2632
public function set($model, $key, $value, $attributes)

tests/Integration/Database/DatabaseCustomCastsTest.php

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,28 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
1717
Schema::create('test_eloquent_model_with_custom_casts', function (Blueprint $table) {
1818
$table->increments('id');
1919
$table->text('array_object');
20+
$table->json('array_object_json');
2021
$table->text('collection');
2122
$table->string('stringable');
2223
$table->timestamps();
2324
});
25+
26+
Schema::create('test_eloquent_model_with_custom_casts_nullables', function (Blueprint $table) {
27+
$table->increments('id');
28+
$table->text('array_object')->nullable();
29+
$table->json('array_object_json')->nullable();
30+
$table->text('collection')->nullable();
31+
$table->string('stringable')->nullable();
32+
$table->timestamps();
33+
});
2434
}
2535

2636
public function test_custom_casting()
2737
{
2838
$model = new TestEloquentModelWithCustomCasts;
2939

3040
$model->array_object = ['name' => 'Taylor'];
41+
$model->array_object_json = ['name' => 'Taylor'];
3142
$model->collection = collect(['name' => 'Taylor']);
3243
$model->stringable = Str::of('Taylor');
3344

@@ -36,21 +47,84 @@ public function test_custom_casting()
3647
$model = $model->fresh();
3748

3849
$this->assertEquals(['name' => 'Taylor'], $model->array_object->toArray());
50+
$this->assertEquals(['name' => 'Taylor'], $model->array_object_json->toArray());
3951
$this->assertEquals(['name' => 'Taylor'], $model->collection->toArray());
4052
$this->assertSame('Taylor', (string) $model->stringable);
4153

4254
$model->array_object['age'] = 34;
4355
$model->array_object['meta']['title'] = 'Developer';
4456

57+
$model->array_object_json['age'] = 34;
58+
$model->array_object_json['meta']['title'] = 'Developer';
59+
60+
$model->save();
61+
62+
$model = $model->fresh();
63+
64+
$this->assertEquals(
65+
[
66+
'name' => 'Taylor',
67+
'age' => 34,
68+
'meta' => ['title' => 'Developer'],
69+
],
70+
$model->array_object->toArray()
71+
);
72+
73+
$this->assertEquals(
74+
[
75+
'name' => 'Taylor',
76+
'age' => 34,
77+
'meta' => ['title' => 'Developer'],
78+
],
79+
$model->array_object_json->toArray()
80+
);
81+
}
82+
83+
public function test_custom_casting_nullable_values()
84+
{
85+
$model = new TestEloquentModelWithCustomCastsNullable();
86+
87+
$model->array_object = null;
88+
$model->array_object_json = null;
89+
$model->collection = collect();
90+
$model->stringable = null;
91+
92+
$model->save();
93+
94+
$model = $model->fresh();
95+
96+
$this->assertEmpty($model->array_object);
97+
$this->assertEmpty($model->array_object_json);
98+
$this->assertEmpty($model->collection);
99+
$this->assertSame('', (string) $model->stringable);
100+
101+
$model->array_object = ['name' => 'John'];
102+
$model->array_object['name'] = 'Taylor';
103+
$model->array_object['meta']['title'] = 'Developer';
104+
105+
$model->array_object_json = ['name' => 'John'];
106+
$model->array_object_json['name'] = 'Taylor';
107+
$model->array_object_json['meta']['title'] = 'Developer';
108+
45109
$model->save();
46110

47111
$model = $model->fresh();
48112

49-
$this->assertEquals([
50-
'name' => 'Taylor',
51-
'age' => 34,
52-
'meta' => ['title' => 'Developer'],
53-
], $model->array_object->toArray());
113+
$this->assertEquals(
114+
[
115+
'name' => 'Taylor',
116+
'meta' => ['title' => 'Developer'],
117+
],
118+
$model->array_object->toArray()
119+
);
120+
121+
$this->assertEquals(
122+
[
123+
'name' => 'Taylor',
124+
'meta' => ['title' => 'Developer'],
125+
],
126+
$model->array_object_json->toArray()
127+
);
54128
}
55129
}
56130

@@ -70,6 +144,29 @@ class TestEloquentModelWithCustomCasts extends Model
70144
*/
71145
protected $casts = [
72146
'array_object' => AsArrayObject::class,
147+
'array_object_json' => AsArrayObject::class,
148+
'collection' => AsCollection::class,
149+
'stringable' => AsStringable::class,
150+
];
151+
}
152+
153+
class TestEloquentModelWithCustomCastsNullable extends Model
154+
{
155+
/**
156+
* The attributes that aren't mass assignable.
157+
*
158+
* @var string[]
159+
*/
160+
protected $guarded = [];
161+
162+
/**
163+
* The attributes that should be cast to native types.
164+
*
165+
* @var array
166+
*/
167+
protected $casts = [
168+
'array_object' => AsArrayObject::class,
169+
'array_object_json' => AsArrayObject::class,
73170
'collection' => AsCollection::class,
74171
'stringable' => AsStringable::class,
75172
];

0 commit comments

Comments
 (0)