adrema/app/Setting/SettingFactory.php

46 lines
1.1 KiB
PHP
Raw Permalink Normal View History

2022-10-20 02:15:28 +02:00
<?php
namespace App\Setting;
use Illuminate\Routing\Router;
class SettingFactory
{
/**
* @var array<int, class-string<LocalSettings>>
*/
private array $settings = [];
/**
2024-08-01 11:32:14 +02:00
* @param class-string<LocalSettings> $setting
2022-10-20 02:15:28 +02:00
*/
public function register(string $setting): void
{
$this->settings[] = $setting;
if (1 === count($this->settings)) {
2024-08-01 11:32:14 +02:00
app(Router::class)->redirect('/setting', '/setting/' . $setting::group());
2022-10-20 02:15:28 +02:00
}
}
/**
* @return array<int, array{url: string, is_active: bool}>
*/
public function getShare(): array
{
return collect($this->settings)->map(fn ($setting) => [
2024-08-01 13:33:28 +02:00
'url' => (new $setting)->url(),
'is_active' => url(request()->path()) === (new $setting)->url(),
2022-10-20 02:15:28 +02:00
'title' => $setting::title(),
])
2024-08-01 10:25:48 +02:00
->toArray();
2022-10-20 02:15:28 +02:00
}
2024-08-01 11:17:49 +02:00
2024-08-01 11:32:14 +02:00
public function resolveGroupName(string $name): LocalSettings
2024-08-01 11:17:49 +02:00
{
2024-08-01 11:32:14 +02:00
$settingClass = collect($this->settings)->first(fn ($setting) => $setting::group() === $name);
2024-08-01 11:17:49 +02:00
return app($settingClass);
}
2022-10-20 02:15:28 +02:00
}