adrema/app/Setting/SettingFactory.php

42 lines
945 B
PHP
Raw Normal View History

2022-10-20 02:15:28 +02:00
<?php
namespace App\Setting;
use Illuminate\Routing\Router;
2024-10-14 20:25:11 +02:00
use Illuminate\Support\Collection;
2022-10-20 02:15:28 +02:00
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
}
}
/**
2024-10-14 20:25:11 +02:00
* @return Collection<int, LocalSettings>
2022-10-20 02:15:28 +02:00
*/
2024-10-14 20:25:11 +02:00
public function all(): Collection
2022-10-20 02:15:28 +02:00
{
2024-10-14 20:25:11 +02:00
return collect($this->settings)->map(fn ($setting) => new $setting);
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
}