Skip to content

Commit 756ff1c

Browse files
fix: dont run properties through accessors when serialising
1 parent f5bce3f commit 756ff1c

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

src/Support/ModelAttributeFormatter.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@
33
namespace Orbit\Support;
44

55
use BackedEnum;
6+
use Carbon\Carbon;
67
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Support\Arr;
79
use Orbit\Contracts\Orbit;
810

911
class ModelAttributeFormatter
1012
{
1113
public static function format(Orbit&Model $model, array $attributes): array
1214
{
13-
$formatted = [];
15+
return Arr::map($attributes, static function (mixed $value, string $key) use ($model) {
16+
$cast = $model->{$key};
1417

15-
foreach ($attributes as $key => $value) {
16-
$value = $model->{$key};
17-
18-
$formatted[$key] = match (true) {
19-
$value instanceof BackedEnum => $value->value,
18+
return match (true) {
19+
$cast instanceof BackedEnum => $cast->value,
20+
$cast instanceof Carbon => $cast->toIso8601String(),
2021
default => $value,
2122
};
22-
}
23-
24-
return $formatted;
23+
});
2524
}
2625
}

tests/Feature/Orbital/CreateTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@
1515
title: 'Example Post'
1616
MD);
1717
});
18+
19+
it('does not use accessors when serialising saved data', function () {
20+
$post = Post::create([
21+
'title' => 'hello',
22+
]);
23+
24+
expect(base_path("content/posts/{$post->id}.md"))
25+
->toBeFile()
26+
->and(file_get_contents(base_path("content/posts/{$post->id}.md")))
27+
->not->toContain('Hello');
28+
});

tests/Feature/Orbital/SoftDeleteTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
->toBeFile()
1717
->and(file_get_contents(base_path("content/categories/{$category->id}.md")))
1818
->toContain(<<<MD
19-
deleted_at: {$category->deleted_at->toIso8601String()}
19+
deleted_at: '{$category->deleted_at->toIso8601String()}'
2020
MD);
2121
});
2222

@@ -34,7 +34,7 @@
3434
->toBeFile()
3535
->and(file_get_contents(base_path("content/categories/{$category->id}.md")))
3636
->toContain(<<<MD
37-
deleted_at: {$category->deleted_at->toIso8601String()}
37+
deleted_at: '{$category->deleted_at->toIso8601String()}'
3838
MD);
3939

4040
$category->restore();

tests/Fixtures/Models/Post.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Tests\Fixtures\Models;
44

5+
use Illuminate\Database\Eloquent\Casts\Attribute;
56
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Database\Schema\Blueprint;
8+
use Illuminate\Support\Str;
79
use Orbit\Concerns\Orbital;
810
use Orbit\Contracts\Orbit;
911

@@ -19,4 +21,9 @@ public function schema(Blueprint $table): void
1921
$table->string('title');
2022
$table->longText('content')->nullable();
2123
}
24+
25+
public function title(): Attribute
26+
{
27+
return Attribute::get(fn (string $value) => Str::headline($value));
28+
}
2229
}

0 commit comments

Comments
 (0)