<?php

namespace App\Util;

use ArrayAccess;

class Config implements ArrayAccess
{
    protected $path;

    protected $config = [];

    public function __construct($path)
    {
        $this->path = $path;
    }

    public function offsetExists($key)
    {
        return isset($this->config[$key]);
    }

    public function offsetGet($key)
    {
        if (! isset($this->config[$key])) {
            $file = $this->path.'/'.$key.'.php';

            $this->config[$key] = file_exists($file) ? require $file : null;
        }

        return $this->config[$key];
    }

    public function offsetSet($key, $value)
    {
        $this->config[$key] = $value;
    }

    public function offsetUnset($key)
    {
        unset($this->config[$key]);
    }
}