feat: project skeleton with Config and exceptions
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Weerscherm;
|
||||
|
||||
final class Config
|
||||
{
|
||||
private const DEFAULTS = [
|
||||
'data_source' => 'fixture',
|
||||
'netatmo_client_id' => '',
|
||||
'netatmo_client_secret' => '',
|
||||
'netatmo_device_id' => null,
|
||||
'timezone' => 'Europe/Berlin',
|
||||
'forecast_lat' => null,
|
||||
'forecast_lon' => null,
|
||||
'indoor_modules' => [],
|
||||
'cache_ttl' => [
|
||||
'stations' => 300,
|
||||
'measure' => 900,
|
||||
'forecast' => 3600,
|
||||
],
|
||||
'storage_path' => null,
|
||||
];
|
||||
|
||||
/** @param array<string, mixed> $values */
|
||||
private function __construct(private readonly array $values)
|
||||
{
|
||||
}
|
||||
|
||||
public static function load(string $file): self
|
||||
{
|
||||
if (!is_file($file)) {
|
||||
throw new \RuntimeException("Configuratiebestand ontbreekt: $file");
|
||||
}
|
||||
$raw = require $file;
|
||||
if (!is_array($raw)) {
|
||||
throw new \RuntimeException("Configuratiebestand moet een array teruggeven: $file");
|
||||
}
|
||||
|
||||
$values = array_replace(self::DEFAULTS, $raw);
|
||||
$values['cache_ttl'] = array_replace(self::DEFAULTS['cache_ttl'], is_array($raw['cache_ttl'] ?? null) ? $raw['cache_ttl'] : []);
|
||||
$values['storage_path'] ??= dirname($file) . '/storage';
|
||||
|
||||
return new self($values);
|
||||
}
|
||||
|
||||
public function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return array_key_exists($key, $this->values) ? $this->values[$key] : $default;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user