$slots slot => file name without .svg */ public function __construct( public readonly string $key, public readonly string $name, public readonly ?string $description, public readonly string $path, public readonly array $slots, ) {} public static function fromFile(string $file): self { try { $data = json_decode((string) file_get_contents($file), true, 512, JSON_THROW_ON_ERROR); } catch (JsonException $exception) { throw new RuntimeException('Bildwelt '.$file.' ist kein gültiges JSON: '.$exception->getMessage()); } $path = dirname($file); $key = basename($path); return new self( key: $key, name: is_string($data['name'] ?? null) ? $data['name'] : $key, description: is_string($data['description'] ?? null) ? $data['description'] : null, path: $path, slots: array_filter( is_array($data['slots'] ?? null) ? $data['slots'] : [], fn (mixed $file, mixed $slot): bool => is_string($slot) && is_string($file), ARRAY_FILTER_USE_BOTH, ), ); } /** * The file behind a slot, or null when this world does not fill it. A missing slot is not an error: * a template asks for decoration and simply gets none. */ public function fileFor(string $slot): ?string { $name = $this->slots[$slot] ?? null; if ($name === null) { return null; } $file = $this->path.DIRECTORY_SEPARATOR.'svg'.DIRECTORY_SEPARATOR.$name.'.svg'; return is_file($file) ? $file : null; } /** * @return list */ public function missingFiles(): array { return array_values(array_filter( array_keys($this->slots), fn (string $slot): bool => $this->fileFor($slot) === null, )); } }