[model, route parameter]. * * @var array, 1: string}> */ public const WITH_HISTORY = [ 'pages' => [Page::class, 'page'], 'categories' => [Category::class, 'category'], 'projects' => [Project::class, 'project'], 'members' => [Member::class, 'member'], 'services' => [Service::class, 'service'], 'clients' => [Client::class, 'client'], 'testimonials' => [Testimonial::class, 'testimonial'], 'talks' => [Talk::class, 'talk'], 'technologies' => [Technology::class, 'technology'], 'calls' => [Call::class, 'call'], ]; /** * What goes through a review before it reaches the site, and the fields * that put each one there — the ones `{resource}.publish` guards. * * @var array> */ public const REVIEWABLE = [ 'pages' => ['status', 'published_at', 'unpublish_at'], 'projects' => ['published_at', 'unpublish_at'], 'services' => ['is_published', 'published_at', 'unpublish_at'], ]; /** * What has an address on the public site to preview, by the type name * `SiteUrls` knows it as. * * @var array */ public const PREVIEWABLE = [ 'pages' => 'page', 'projects' => 'project', 'services' => 'service', 'calls' => 'call', ]; /** * @return class-string */ public static function model(string $resource): string { return self::WITH_HISTORY[$resource][0] ?? abort(404); } public static function parameter(string $resource): string { return self::WITH_HISTORY[$resource][1] ?? abort(404); } /** * The record a route names, binned ones included: their history is still * worth reading, and restoring them is what the bin is for. */ public static function find(string $resource, mixed $id): Model { return self::findOf(self::model($resource), $id); } /** * @param class-string $model */ public static function findOf(string $model, mixed $id): Model { $query = $model::query(); if (in_array(SoftDeletes::class, class_uses_recursive($model), true)) { $query->withTrashed(); } return $query->findOrFail($id); } /** * The API name of a record, for a review queue that mixes several kinds. */ public static function nameOf(Model $record): ?string { foreach (self::WITH_HISTORY as $resource => [$model]) { if ($record instanceof $model) { return $resource; } } return null; } /** * The fields only `{resource}.publish` may write, for this record. * * @return array */ public static function publicationFields(Model $record): array { return self::REVIEWABLE[self::nameOf($record)] ?? []; } }