<?php

/**
 * Aphiria
 *
 * @link      https://www.aphiria.com
 * @copyright Copyright (C) 2025 David Young
 * @license   https://github.com/aphiria/aphiria/blob/1.x/LICENSE.md
 */

declare(strict_types=1);

namespace Aphiria\Application\Configuration;

use InvalidArgumentException;

/**
 * Defines the configuration reader that reads a PHP files
 */
class PhpConfigurationFileReader implements IConfigurationFileReader
{
    /**
     * @inheritdoc
     */
    public function readConfiguration(string $path, string $pathDelimiter = '.'): IConfiguration
    {
        if (empty($pathDelimiter)) {
            throw new InvalidArgumentException('Path delimiter cannot be empty');
        }

        if (!\file_exists($path)) {
            throw new InvalidConfigurationFileException("$path does not exist");
        }

        $hashTable = require $path;

        if (!\is_array($hashTable)) {
            throw new InvalidConfigurationFileException("Configuration in $path must be an array");
        }

        /** @var array<string, mixed> $hashTable */
        return new HashTableConfiguration($hashTable, $pathDelimiter);
    }
}
