File tree Expand file tree Collapse file tree 4 files changed +28
-11
lines changed
Expand file tree Collapse file tree 4 files changed +28
-11
lines changed Original file line number Diff line number Diff line change 33namespace Orbit \Support ;
44
55use BackedEnum ;
6+ use Carbon \Carbon ;
67use Illuminate \Database \Eloquent \Model ;
8+ use Illuminate \Support \Arr ;
79use Orbit \Contracts \Orbit ;
810
911class 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}
Original file line number Diff line number Diff line change 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+ });
Original file line number Diff line number Diff line change 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
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 ();
Original file line number Diff line number Diff line change 22
33namespace Tests \Fixtures \Models ;
44
5+ use Illuminate \Database \Eloquent \Casts \Attribute ;
56use Illuminate \Database \Eloquent \Model ;
67use Illuminate \Database \Schema \Blueprint ;
8+ use Illuminate \Support \Str ;
79use Orbit \Concerns \Orbital ;
810use 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}
You can’t perform that action at this time.
0 commit comments