adrema/app/Setting/SettingFactory.php

48 lines
1.2 KiB
PHP
Raw Normal View History

2022-10-20 02:15:28 +02:00
<?php
namespace App\Setting;
2024-08-01 11:17:49 +02:00
use App\Invoice\InvoiceSettings;
2023-06-01 12:04:31 +02:00
use App\Setting\Contracts\Storeable;
2022-10-20 02:15:28 +02:00
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
}