<commit_before><?php

/**
 * Author: samokspv <<EMAIL>>
 * Date: 06.12.2013
 * Time: 12:00:00
 */

/**
 * Class for key-value storage
 *
 */
class DBConfigure {

	/**
	 * Engine instance
	 *
	 * @var object
	 */
	protected static $_Engine = null;

	/**
	 * Engine name
	 *
	 * @var string
	 */
	protected static $_EngineName = 'ConfigureStore';
	
	/**
	 * Return engine instance
	 *
	 * @return object
	 */
	protected static function _getEngine() {
		if (!self::$_Engine) {
			self::$_Engine = ClassRegistry::init('DBConfigure.' . self::$_EngineName);
		}

		return self::$_Engine;
	}

	/**
	 * Get config data from storage/config file
	 *
	 * @param string $key Variable key
	 * @return mixed
	 */
	protected static function _getConfigByKey($key) {
		$dbConfig = self::_getEngine()->get($key);
		$fileConfig = Configure::read($key);
		if (!empty($fileConfig)) {
			$dbConfig = Hash::mergeDiff((array)$dbConfig, $fileConfig);
		}
		
		return $dbConfig;
	}

	/**
	 * Adds (or update) variable to storage
	 *
	 * @param string $key Variable key
	 * @param mixed $value Variable value
	 * @return boolean
	 */
	public static function write($key, $value) {
		if (empty($key) || empty($value)) {
			return false;
		}

		$key = explode('.', $key);
		$config = self::_getConfigByKey($key[0]);
		$cntKey = count($key);
		if (is_null($config) && $cntKey > 1) {
			$config = array();
		}
		if (!is_null($config)) {
			if ($cntKey > 1) {
				$value = Hash::insert((array)$config, implode('.', array_slice($key, 1)), $value);
			}
		}
		self::_getEngine()->add($key[0], $value);<commit_msg>if value for save is array - merge with default data and then insert by key, see #2<commit_after><?php

/**
 * Author: samokspv <<EMAIL>>
 * Date: 06.12.2013
 * Time: 12:00:00
 */

/**
 * Class for key-value storage
 *
 */
class DBConfigure {

	/**
	 * Engine instance
	 *
	 * @var object
	 */
	protected static $_Engine = null;

	/**
	 * Engine name
	 *
	 * @var string
	 */
	protected static $_EngineName = 'ConfigureStore';
	
	/**
	 * Return engine instance
	 *
	 * @return object
	 */
	protected static function _getEngine() {
		if (!self::$_Engine) {
			self::$_Engine = ClassRegistry::init('DBConfigure.' . self::$_EngineName);
		}

		return self::$_Engine;
	}

	/**
	 * Get config data from storage/config file
	 *
	 * @param string $key Variable key
	 * @return mixed
	 */
	protected static function _getConfigByKey($key) {
		$dbConfig = self::_getEngine()->get($key);
		$fileConfig = Configure::read($key);
		if (!empty($fileConfig)) {
			$dbConfig = Hash::mergeDiff((array)$dbConfig, $fileConfig);
		}
		
		return $dbConfig;
	}

	/**
	 * Adds (or update) variable to storage
	 *
	 * @param string $key Variable key
	 * @param mixed $value Variable value
	 * @return boolean
	 */
	public static function write($key, $value) {
		if (empty($key) || empty($value)) {
			return false;
		}

		$key = explode('.', $key);
		$config = self::_getConfigByKey($key[0]);
		$cntKey = count($key);
		if (is_null($config) && $cntKey > 1) {
			$config = array();
		}
		if (!is_null($config)) {
			if ($cntKey > 1) {
				$afterFirstKey = implode('.', array_slice($key, 1));
				if (is_array($value)) {
					$valueDefault = Hash::extract($config, $afterFirstKey);
					$value = Hash::mergeDiff($value, (array)$valueDefault);
				}
				$value = Hash::insert((array)$config, $afterFirstKey, $value);
			}
		}
		self::_getEngine()->add($key[0], $value);
{'fraction_non_alphanumeric': 0.17958467388125182, 'fraction_numerical': 0.011406844106463879, 'mean_word_length': 3.430051813471503, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '6114310', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1130, 'n_words': 281}