} */ public array $sources; public struct_settings $settings; } /** */ class _state { public static ?struct_root $data = null; } /** */ function validate_source_name( string $source_name ) : string { $matchs = null; $result = preg_match('/^[0-9a-zA-Z]+$/', $source_name, $matches); return ($result === 1); } /** */ function load( string $path ) : void { $data_raw = \json_decode( \file_get_contents( $path ), true ); { $data = new struct_root(); // auth { $auth = new struct_auth(); $auth->kind = (($data_raw['auth'] ?? [])['kind'] ?? 'none'); $auth->data = (($data_raw['auth'] ?? [])['data'] ?? null); $data->auth = $auth; } // sources { $sources = []; /** * @todo check for name duplicates */ $data->sources = \davina\helpers\list_\map( $data_raw['sources'], function ($source_raw) { if (! validate_source_name($source_raw['name'])) { throw (new \Exception(\sprintf('invalid source name: "%s"', $source_raw['name']))); } else { $source = new struct_source(); $source->name = $source_raw['name']; $source->kind = $source_raw['kind']; $source->data = ($source_raw['data'] ?? null); return $source; } } ); } // settings { $settings = new struct_settings(); $settings->timezone = (($data_raw['settings'] ?? [])['timezone'] ?? 'UTC'); $data->settings = $settings; } _state::$data = $data; } } /** */ function get( ) : struct_root { if (_state::$data === null) { throw (new \Exception('not yet loaded')); } else { return _state::$data; } } ?>