<commit_before><?php

namespace LukasRos\JSONInclude;

class JSONInclude {
	
	private $options;
	
	public function __construct($options = array()) {
		$this->options = array_merge(array(
			'include_symbol' => '@',
			'base_dir' => '',
			'silent' => true	
		), $options);
	}
	
	public function parseDataWithIncludes($data, $baseDir = null, $fileChain = array()) {
		if (!$baseDir) $baseDir = $this->options['base_dir'];
		
		if (is_array($data)) {
			// Array is handled recursively
			$output = array();
			foreach ($data as $key => $value) {
				$output[$key] = $this->parseDataWithIncludes($value, $baseDir, $fileChain);
			}
			return $output;
		} elseif (is_string($data) && strlen($data)>0 && $data[0]==$this->options['include_symbol']) {
			// Include file
			return $this->parseFileWithIncludes(substr($data, 1), $baseDir, $fileChain);
		} else {
			// Return data
			return $data;
		}
	}
	
	public function parseFileWithIncludes($filename, $baseDir = null, $fileChain = array()) {
		if (!$baseDir) $baseDir = $this->options['base_dir'];
		
		if ($baseDir=='')
			$baseDir = dirname($filename);
		else
			$filename = realpath($baseDir.'/'.$filename);

		if (!file_exists($filename)) {
			if ($this->options['silent']) return null;
			else throw new \Exception('A requested file does not exist.');
		}
		if (in_array($filename, $fileChain)) {
			if ($this->options['silent']) return null;
			else throw new \Exception('A file is inserted recursively: '.$filename);
		}
		$fileChain[] = $filename;
		
		return $this->parseDataWithIncludes(json_decode(file_get_contents($filename), true), $baseDir, $fileChain);
	}
	
}<commit_msg>Integrate JSON comment parser<commit_after><?php

namespace LukasRos\JSONInclude;

class JSONInclude {
	
	private $options;
	
	public function __construct($options = array()) {
		$this->options = array_merge(array(
			'include_symbol' => '@',
			'base_dir' => '',
			'silent' => true,
			'accept_comments' => true	
		), $options);
	}
	
	public function parseDataWithIncludes($data, $baseDir = null, $fileChain = array()) {
		if (!$baseDir) $baseDir = $this->options['base_dir'];
		
		if (is_array($data)) {
			// Array is handled recursively
			$output = array();
			foreach ($data as $key => $value) {
				$output[$key] = $this->parseDataWithIncludes($value, $baseDir, $fileChain);
			}
			return $output;
		} elseif (is_string($data) && strlen($data)>0 && $data[0]==$this->options['include_symbol']) {
			// Include file
			return $this->parseFileWithIncludes(substr($data, 1), $baseDir, $fileChain);
		} else {
			// Return data
			return $data;
		}
	}
	
	public function parseFileWithIncludes($filename, $baseDir = null, $fileChain = array()) {
		if (!$baseDir) $baseDir = $this->options['base_dir'];
		
		if ($baseDir=='')
			$baseDir = dirname($filename);
		else
			$filename = realpath($baseDir.'/'.$filename);

		if (!file_exists($filename)) {
			if ($this->options['silent']) return null;
			else throw new \Exception('A requested file does not exist.');
		}
		if (in_array($filename, $fileChain)) {
			if ($this->options['silent']) return null;
			else throw new \Exception('A file is inserted recursively: '.$filename);
		}
		$fileChain[] = $filename;
		
		$jsonString = file_get_contents($filename);
		if ($this->options['accept_comments']) {
			// Parse comments - code taken from http://php.net/manual/de/function.json-decode.php#111551
			$jsonString = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t](//).*)#", '', $jsonString); 
		}
		return $this->parseDataWithIncludes(json_decode($jsonString, true), $baseDir, $fileChain);
	}
	
}
{'fraction_non_alphanumeric': 0.1903968914793228, 'fraction_numerical': 0.0033305578684429643, 'mean_word_length': 4.261313868613139, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '4029109', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1184, 'n_words': 258}