<?php
/**
 * Configuration Repository
 *
 * @package Licenzo\Framework\Config
 */

namespace Licenzo\Framework\Config;

use Licenzo\Framework\Support\Arr;

// Prevent direct access

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Configuration repository for accessing config data
 */
class Repository
{
    /**
     * Configuration data
     *
     * @var array
     */
    protected $config = [];

    /**
     * Constructor
     *
     * @param array $config
     */
    public function __construct(array $config = [])
    {
        $this->config = $config;
    }

    /**
     * Get a configuration value using dot notation
     *
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public function get($key, $default = null)
    {
        return Arr::get($this->config, $key, $default);
    }

    /**
     * Check if a configuration value exists
     *
     * @param string $key
     * @return bool
     */
    public function has(string $key): bool
    {
        return $this->get($key) !== null;
    }

    /**
     * Get all configuration values
     *
     * @return array
     */
    public function all(): array
    {
        return $this->config;
    }

    public function set($key, $value = null)
    {
        if (is_array($key)) {
            foreach ($key as $k => $v) {
                $this->set($k, $v);
            }
            return;
        }

        Arr::set($this->config, $key, $value);
    }

    public function merge(array $config, bool $recursive = true)
    {
        if ($recursive) {
            $this->config = array_merge_recursive($this->config, $config);
        } else {
            $this->config = array_merge($this->config, $config);
        }
    }
}
