<commit_before><?php

namespace Parvula\Models;

use Parvula\ArrayableInterface;
use Psr\Container\ContainerInterface;

/**
 * Configuration wrapper for config array
 *
 * @package Parvula
 * @version 0.8.0
 * @since 0.1.0
 * @author <NAME>
 * @license MIT License
 */
class Config extends Model implements ContainerInterface, ArrayableInterface {

	/**
	 * @var array
	 */
	protected $config = [];

	/**
	 * @var array
	 */
	protected $invisible = [
		'_id'
	];

	/**
	 * Populate class with config array
	 *
	 * @param array $config
	 */
	public function __construct(array $config) {
		$this->config = $config;
	}

	/**
	 * Append config to Config class
	 *
	 * @param array $config
	 * @return Config
	 */
	public function append(array $config) {
		$this->config = $config + $this->config;

		return $this;
	}

	/**
	 * Get configuration value from key
	 *
	 * @param mixed $key
	 * @param mixed $default optional Default if value if nothing
	 * @return mixed Value from config
	 */
	public function get($key, $default = null) {
		$pieces = explode('.', $key);
		$ptr = &$this->config;

		foreach ($pieces as $step) {
			if (!isset($ptr[$step])) {
				return $default;
			}
			$ptr = &$ptr[$step];
		}

		return $ptr;
	}

	/**
	 * Set configuration value from key (create a new key if needed)
	 *
	 * @param mixed $key
	 * @param mixed $value
	 * @param bool True if value is set
	 */
	public function set($key, $value) {
		$pieces = explode('.', $key);
		$ptr = &$this->config;

		foreach ($pieces as $step) {
			$ptr = &$ptr[$step];
			if (!isset($ptr[$step])) {
				$ptr = [];
			}
		}

		return $ptr = $value;
	}

	/**
	 * Edit configuration value from key (without creating a new key)
	 *
	 * @param mixed $key
	 * @param mixed $value
	 * @param bool False if the value does not exists
	 */
	public function edit($key, $value) {
		$pieces = explode('.', $key);
		$ptr = &$this->config;

		foreach ($pieces as $step) {
			if (!isset($ptr[$step])) {
				return false;
			}
			$ptr = &$ptr[$step];
		}

		$ptr = $value;
		return true;
	}

	/**
	 * Check if key exists
	 *
	 * @return bool
	 */
	public function has($key) {
		$pieces = explode('.', $key);
		$ptr = &$this->config;

		foreach ($pieces as $step) {
			if (!isset($ptr[$step])) {
				return false;
			}
			$ptr = &$ptr[$step];
		}

		return true;
	}

	/**
	 * Get the configuration as an array
	 *
	 * @return array
	 */
	public function toArray() {
		return $this->config;
	}

	/**
	 * Get the configuration as an object
	 *
	 * @return object
	 */
	public function toObject() {
		return (object) $this->config;
	}
}<commit_msg>Add ArrayAccess to simplify the use of Config<commit_after><?php

namespace Parvula\Models;

use ArrayAccess;
use Parvula\ArrayableInterface;
use Psr\Container\ContainerInterface;

/**
 * Configuration wrapper for config array
 *
 * @package Parvula
 * @version 0.8.0
 * @since 0.1.0
 * @author <NAME>
 * @license MIT License
 */
class Config extends Model implements ContainerInterface, ArrayableInterface, ArrayAccess {

	/**
	 * @var array
	 */
	protected $config = [];

	/**
	 * @var array
	 */
	protected $invisible = [
		'_id'
	];

	/**
	 * Populate class with config array
	 *
	 * @param array $config
	 */
	public function __construct(array $config) {
		$this->config = $config;
	}

	/**
	 * Append config to Config class
	 *
	 * @param array $config
	 * @return Config
	 */
	public function append(array $config) {
		$this->config = $config + $this->config;

		return $this;
	}

	/**
	 * Get configuration value from key
	 *
	 * @param mixed $key
	 * @param mixed $default optional Default if value if nothing
	 * @return mixed Value from config
	 */
	public function get($key, $default = null) {
		$pieces = explode('.', $key);
		$ptr = &$this->config;

		foreach ($pieces as $step) {
			if (!isset($ptr[$step])) {
				return $default;
			}
			$ptr = &$ptr[$step];
		}

		return $ptr;
	}

	/**
	 * Set configuration value from key (create a new key if needed)
	 *
	 * @param mixed $key
	 * @param mixed $value
	 * @param bool True if value is set
	 */
	public function set($key, $value) {
		$pieces = explode('.', $key);
		$ptr = &$this->config;

		foreach ($pieces as $step) {
			$ptr = &$ptr[$step];
			if (!isset($ptr[$step])) {
				$ptr = [];
			}
		}

		return $ptr = $value;
	}

	/**
	 * Edit configuration value from key (without creating a new key)
	 *
	 * @param mixed $key
	 * @param mixed $value
	 * @param bool False if the value does not exists
	 */
	public function edit($key, $value) {
		$pieces = explode('.', $key);
		$ptr = &$this->config;

		foreach ($pieces as $step) {
			if (!isset($ptr[$step])) {
				return false;
			}
			$ptr = &$ptr[$step];
		}

		$ptr = $value;
		return true;
	}

	/**
	 * Check if key exists
	 *
	 * @return bool
	 */
	public function has($key) {
		$pieces = explode('.', $key);
		$ptr = &$this->config;

		foreach ($pieces as $step) {
			if (!isset($ptr[$step])) {
				return false;
			}
			$ptr = &$ptr[$step];
		}

		return true;
	}

	/**
	 * Get the configuration as an array
	 *
	 * @return array
	 */
	public function toArray() {
		return $this->config;
	}

	/**
	 * Get the configuration as an object
	 *
	 * @return object
	 */
	public function toObject() {
		return (object) $this->config;
	}

    /**
     * alias for `set`
	 *
	 * @see Config::set
	 */
    public function offsetSet($key, $value) {
		$this->set($key, $value);
    }

    /**
     * alias for `has`
	 *
	 * @see Config::has
	 */
    public function offsetExists($key) {
        return $this->has($key);
    }

    public function offsetUnset($offset) {
    }

    /**
     * alias for `get`
	 *
	 * @see Config::get
	 */
    public function offsetGet($key) {
        return $this->get($key);
    }
}
{'fraction_non_alphanumeric': 0.16308713113322856, 'fraction_numerical': 0.0020953378732320588, 'mean_word_length': 2.74869109947644, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 26, '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': '5190139', 'n_tokens_mistral': 2146, 'n_tokens_neox': 1847, 'n_words': 568}