<?php

declare(strict_types=1);

namespace PpPhp\Core\Discovery;

final class ManifestReader
{
    public static function read(string $path): array
    {
        if (!is_file($path)) {
            return [];
        }

        $json = json_decode((string) file_get_contents($path), true);

        return is_array($json) ? $json : [];
    }

    public static function idFromManifest(array $manifest, string $fallback): string
    {
        $id = trim((string) ($manifest['id'] ?? ''));

        return $id !== '' ? $id : $fallback;
    }
}
