Skip to content

Commit 8737a65

Browse files
Add a new cast named AsFluent (#56046)
1 parent 6d3fbc5 commit 8737a65

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Eloquent\Casts;
4+
5+
use Illuminate\Contracts\Database\Eloquent\Castable;
6+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
7+
use Illuminate\Support\Fluent;
8+
9+
class AsFluent implements Castable
10+
{
11+
/**
12+
* Get the caster class to use when casting from / to this cast target.
13+
*
14+
* @param array $arguments
15+
* @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Support\Fluent, string>
16+
*/
17+
public static function castUsing(array $arguments)
18+
{
19+
return new class implements CastsAttributes
20+
{
21+
public function get($model, $key, $value, $attributes)
22+
{
23+
return isset($value) ? new Fluent(Json::decode($value)) : null;
24+
}
25+
26+
public function set($model, $key, $value, $attributes)
27+
{
28+
return isset($value) ? [$key => Json::encode($value)] : null;
29+
}
30+
};
31+
}
32+
}

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Illuminate\Database\Eloquent\Casts\AsEncryptedCollection;
2727
use Illuminate\Database\Eloquent\Casts\AsEnumArrayObject;
2828
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
29+
use Illuminate\Database\Eloquent\Casts\AsFluent;
2930
use Illuminate\Database\Eloquent\Casts\AsHtmlString;
3031
use Illuminate\Database\Eloquent\Casts\AsStringable;
3132
use Illuminate\Database\Eloquent\Casts\AsUri;
@@ -47,6 +48,7 @@
4748
use Illuminate\Support\Carbon;
4849
use Illuminate\Support\Collection as BaseCollection;
4950
use Illuminate\Support\Facades\Crypt;
51+
use Illuminate\Support\Fluent;
5052
use Illuminate\Support\HtmlString;
5153
use Illuminate\Support\InteractsWithTime;
5254
use Illuminate\Support\Stringable;
@@ -304,6 +306,30 @@ public function testDirtyOnCastedUri()
304306
$this->assertTrue($model->isDirty('asUriAttribute'));
305307
}
306308

309+
public function testDirtyOnCastedFluent()
310+
{
311+
$value = [
312+
'address' => [
313+
'street' => 'test_street',
314+
'city' => 'test_city',
315+
],
316+
];
317+
318+
$model = new EloquentModelCastingStub;
319+
$model->setRawAttributes(['asFluentAttribute' => json_encode($value)]);
320+
$model->syncOriginal();
321+
322+
$this->assertInstanceOf(Fluent::class, $model->asFluentAttribute);
323+
$this->assertFalse($model->isDirty('asFluentAttribute'));
324+
325+
$model->asFluentAttribute = new Fluent($value);
326+
$this->assertFalse($model->isDirty('asFluentAttribute'));
327+
328+
$value['address']['street'] = 'updated_street';
329+
$model->asFluentAttribute = new Fluent($value);
330+
$this->assertTrue($model->isDirty('asFluentAttribute'));
331+
}
332+
307333
// public function testDirtyOnCastedEncryptedCollection()
308334
// {
309335
// $this->encrypter = m::mock(Encrypter::class);
@@ -3802,6 +3828,7 @@ protected function casts(): array
38023828
'asStringableAttribute' => AsStringable::class,
38033829
'asHtmlStringAttribute' => AsHtmlString::class,
38043830
'asUriAttribute' => AsUri::class,
3831+
'asFluentAttribute' => AsFluent::class,
38053832
'asCustomCollectionAttribute' => AsCollection::using(CustomCollection::class),
38063833
'asEncryptedArrayObjectAttribute' => AsEncryptedArrayObject::class,
38073834
'asEncryptedCustomCollectionAttribute' => AsEncryptedCollection::using(CustomCollection::class),

0 commit comments

Comments
 (0)