<commit_before><?php
namespace Cohesion\Config;

class Config {

    protected $data = array();
    protected $cache;

    public function __construct() {
    }

    public function setCache($cache) {
        $this->cache = $cache;
    }

    public function loadFromCache($cacheKey, $key = null) {
        if (!$this->cache) {
            throw new CacheException('Cache not set');
        }
        $values = $this->cache->load($cacheKey);
        if ($values) {
            $this->load($values, $key);
            return true;
        } else {
            return false;
        }
    }

    public function loadFromFile($file, $key = null) {
        if (preg_match('/\.json$/', $file)) {
            $contents = file_get_contents($file);
            $params = json_decode($this->json_minify($contents), true);
            if (!$params) {
                throw new \InvalidArgumentException('Invlid config file format. ' . $file . ' cannot be decoded as JSON');
            }
            $this->load($params, $key);
        } else {
            throw new \InvalidArgumentException('Invalid config file format');
        }
    }

    /**
     * Load from array
     */
    public function load($data, $key = null) {
        if ($key) {
            if (isset($this->data[$key])) {
                $this->data[$key] = self::array_merge_recursive_unique($this->data[$key], $data);
            } else {
                $this->data[$key] = $data;
            }
        } else {
            if (is_array($data)) {
                $this->data = self::array_merge_recursive_unique($this->data, $data);
            }
        }
    }

    /**
     * Add a new key value pairing.
     *
     * @throws InvalidArgumentException if the key is already set
     */
    public function add($key, $value) {
        if (isset($this->data[$key])) {
            throw new \InvalidArgumentException("Key $key is already set");
        }
        $this->data[$key] = $value;
    }

    /**
     * Add a key value pairing, overwriting the key if it's already set
     */
    public function overwrite($key, $value) {
        $this->data[$key] = $value;
    }

    /**
     * Add a key value pairing, merging the value with any existing data for the key
     */
    public function merge($key, array $value) {
        $this->load($value, $key);
    }

    public function saveToCache($cacheKey) {
        if (!$this->cache) {
            throw new CacheException('Cache not set');
        }
        $this->cache->save($this->data, $cacheKey);
    }

    /**
     * Get the value for a key with the given name
     * Can use dot notation for acessing nested keys
     *
     * Example:
     * $config->get('database') will return an associative array of all the database values
     * $config->get('database.host') will return the value for 'host' within the array stored at key 'database'
     */
    public function get($name = null) {
        $data = $this->data;
        if ($name) {
            $names = explode('.', $name);
            if ($names) {
                foreach ($names as $name) {
                    if (array_key_exists($name, $data)) {
                        $data = $data[$name];
                    } else {
                        return null;
                    }
                }
            }
        }
        return $data;
    }

    /**
     * Get the value at the given key and return it as a config object
     */
    public function getConfig($name) {
        $data = $this->get($name);
        $config = new self();
        $config->load($data);
        return $config;
    }

    protected static function isAssociativeArray($arr) {
        return is_array($arr) && array_keys($arr) !== range(0, count($arr) - 1);
    }<commit_msg>Fix bug for missing config item<commit_after><?php
namespace Cohesion\Config;

class Config {

    protected $data = array();
    protected $cache;

    public function __construct() {
    }

    public function setCache($cache) {
        $this->cache = $cache;
    }

    public function loadFromCache($cacheKey, $key = null) {
        if (!$this->cache) {
            throw new CacheException('Cache not set');
        }
        $values = $this->cache->load($cacheKey);
        if ($values) {
            $this->load($values, $key);
            return true;
        } else {
            return false;
        }
    }

    public function loadFromFile($file, $key = null) {
        if (preg_match('/\.json$/', $file)) {
            $contents = file_get_contents($file);
            $params = json_decode($this->json_minify($contents), true);
            if (!$params) {
                throw new \InvalidArgumentException('Invlid config file format. ' . $file . ' cannot be decoded as JSON');
            }
            $this->load($params, $key);
        } else {
            throw new \InvalidArgumentException('Invalid config file format');
        }
    }

    /**
     * Load from array
     */
    public function load($data, $key = null) {
        if ($key) {
            if (isset($this->data[$key])) {
                $this->data[$key] = self::array_merge_recursive_unique($this->data[$key], $data);
            } else {
                $this->data[$key] = $data;
            }
        } else {
            if (is_array($data)) {
                $this->data = self::array_merge_recursive_unique($this->data, $data);
            }
        }
    }

    /**
     * Add a new key value pairing.
     *
     * @throws InvalidArgumentException if the key is already set
     */
    public function add($key, $value) {
        if (isset($this->data[$key])) {
            throw new \InvalidArgumentException("Key $key is already set");
        }
        $this->data[$key] = $value;
    }

    /**
     * Add a key value pairing, overwriting the key if it's already set
     */
    public function overwrite($key, $value) {
        $this->data[$key] = $value;
    }

    /**
     * Add a key value pairing, merging the value with any existing data for the key
     */
    public function merge($key, array $value) {
        $this->load($value, $key);
    }

    public function saveToCache($cacheKey) {
        if (!$this->cache) {
            throw new CacheException('Cache not set');
        }
        $this->cache->save($this->data, $cacheKey);
    }

    /**
     * Get the value for a key with the given name
     * Can use dot notation for acessing nested keys
     *
     * Example:
     * $config->get('database') will return an associative array of all the database values
     * $config->get('database.host') will return the value for 'host' within the array stored at key 'database'
     */
    public function get($name = null) {
        $data = $this->data;
        if ($name) {
            $names = explode('.', $name);
            if ($names) {
                foreach ($names as $name) {
                    if (is_array($data) && array_key_exists($name, $data)) {
                        $data = $data[$name];
                    } else {
                        return null;
                    }
                }
            }
        }
        return $data;
    }

    /**
     * Get the value at the given key and return it as a config object
     */
    public function getConfig($name) {
        $data = $this->get($name);
        $config = new self();
        $config->load($data);
        return $config;
    }

    protected static function isAssociativeArray($arr) {
        return is_array($arr) && array_keys($arr) !== range(0, count($arr) - 1);
    }
{'fraction_non_alphanumeric': 0.13670785173375846, 'fraction_numerical': 0.0005314202205393915, 'mean_word_length': 1.7334785766158316, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '621245', 'n_tokens_mistral': 2133, 'n_tokens_neox': 2059, 'n_words': 584}