Skip to content

Commit 3f5a76e

Browse files
Fix morph map
1 parent 6462791 commit 3f5a76e

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/OzuServiceProvider.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Code16\OzuClient;
44

5+
use Closure;
6+
use Code16\OzuClient\OzuCms\OzuCollectionConfig;
57
use Code16\OzuClient\Support\Pagination\StaticLengthAwarePaginator;
68
use Code16\OzuClient\Support\Pagination\StaticPaginator;
79
use Code16\OzuClient\Support\Thumbnails\ImageKitThumbnail;
@@ -86,9 +88,25 @@ public function boot()
8688
// Use Ozu's collectionKey as the morphMap key
8789
Relation::enforceMorphMap(
8890
collect(config('ozu-client.collections'))
89-
->mapWithKeys(fn (string $className) => [
90-
(new $className())->ozuCollectionKey() => $className,
91-
])
91+
->map(fn ($collection) => match (true) {
92+
is_string($collection) => app($collection),
93+
$collection instanceof Closure => $collection(),
94+
default => $collection
95+
})
96+
->map(function ($model) {
97+
$sub = collect(
98+
$model::configureOzuCollection(new OzuCollectionConfig())
99+
->subCollections()
100+
)->map(fn ($subModel) => match (true) {
101+
is_string($subModel) => app($subModel),
102+
$subModel instanceof Closure => $subModel(),
103+
default => $subModel
104+
});
105+
106+
return collect([$model])->merge($sub);
107+
})
108+
->flatten()
109+
->mapWithKeys(fn ($class) => [$class->ozuCollectionKey() => $class::class])
92110
->toArray()
93111
);
94112
}

0 commit comments

Comments
 (0)