<commit_before><?php 

/**
*       PHP psyDuck 
*  Just a small php class to store data (acting as a database) using json files
*
* the very first thing to do when instantiate a new object, is define the storage folder, which can be done passing as string argument on object construct or with the setContainer method
*   and it's also very important to verify the write permissions of the defined storage folder
*
* @author <NAME> <<EMAIL>>
* @version 0.01
*/

class psyDuck
{

	// store the folder path to store the json files
	private $container_path;
	private $file_pointer; // current json file pointer
	private $file_name; // currente json file name
	private $supply_file_pointer; // pointer for temporary file, used in transations
	private $supply_file_name; // temporay file name
	
	/**
	 * On contruct verify if was passed a conteiner, which can be defined later
	 * @param string $container (optional) a folder path which can be defined later
	 */
	function __construct( $container = null )
	{
		if (isset($container))
			$this->setContainer($container);
	}

	/**
	 * Set the folder to store the data files
	 *  try to create if doens't exist
	 * @param string $path 
	 */
	public function setContainer ( $path=null )
	{
		if($path) $path .= DIRECTORY_SEPARATOR; else return false;

		if (!is_dir($path)) // if doesn't exist
			mkdir( $path, 0777, true ); // try to create
		
		$this->container_path = $path;
		return true;
	}

	/**
	 * Define the name of the file container to store data (works like a table)
	 * @param  string $file define the storage file, must be passed without the (dot).JSON extension
	 * @return object  (return a instance of the obj itself, helps with chain methods)
	 */
	public function in ( $file )
	{
		$this->close();
		$this->file_name = $file;
		$file = $this->container_path . $file . '.json';
		if($this->file_pointer = fopen( $file, 'a+') ) {
			return $this;
		} else {
			$this->say("Failed to open the file to storage, check the write permissions of the <i>{$this->container_path}</i> directory");
		}
	}

	/**
	 * insert a new data line in the storage file
	 * @param  array  $data 
	 * @param  boolean $mult if true, insert as a multi line array
	 * @return void
	 */
	public function insert ( $data, $mult=false )
	{
		if (!is_array($data)) 
			$this->say('The argument must be array');
		if (!$this->file_pointer) 
			$this->say('File pointer not defined');

		if ( $mult === true ) {
			for ($i=0; $i < count($data); $i++) { 
				$this->insert( $data[$i] );
			}
		} else {
			$_content = json_encode($data) . PHP_EOL;
			fwrite( $this->file_pointer, $_content );
		}
	}

	/**
	 * Sugar for the function "each", if no closure filter function defined, return all data<commit_msg>add function get<commit_after><?php 

/**
*       PHP psyDuck 
*  Just a small php class to store data (acting as a database) using json files
*
* the very first thing to do when instantiate a new object, is define the storage folder, which can be done passing as string argument on object construct or with the setContainer method
*   and it's also very important to verify the write permissions of the defined storage folder
*
* @author <NAME> <<EMAIL>>
* @version 0.01
*/

class psyDuck
{

	// store the folder path to store the json files
	private $container_path;
	private $file_pointer; // current json file pointer
	private $file_name; // currente json file name
	private $supply_file_pointer; // pointer for temporary file, used in transations
	private $supply_file_name; // temporay file name
	
	/**
	 * On contruct verify if was passed a conteiner, which can be defined later
	 * @param string $container (optional) a folder path which can be defined later
	 */
	function __construct( $container = null )
	{
		if (isset($container))
			$this->setContainer($container);
	}

	/**
	 * Set the folder to store the data files
	 *  try to create if doens't exist
	 * @param string $path 
	 */
	public function setContainer ( $path=null )
	{
		if($path) $path .= DIRECTORY_SEPARATOR; else return false;

		if (!is_dir($path)) // if doesn't exist
			mkdir( $path, 0777, true ); // try to create
		
		$this->container_path = $path;
		return true;
	}

	/**
	 * Define the name of the file container to store data (works like a table)
	 * @param  string $file define the storage file, must be passed without the (dot).JSON extension
	 * @return object  (return a instance of the obj itself, helps with chain methods)
	 */
	public function in ( $file )
	{
		$this->close();
		$this->file_name = $file;
		$file = $this->container_path . $file . '.json';
		if($this->file_pointer = fopen( $file, 'a+') ) {
			return $this;
		} else {
			$this->say("Failed to open the file to storage, check the write permissions of the <i>{$this->container_path}</i> directory");
		}
	}

	/**
	 * insert a new data line in the storage file
	 * @param  array  $data 
	 * @param  boolean $mult if true, insert as a multi line array
	 * @return void
	 */
	public function insert ( $data, $mult=false )
	{
		if (!is_array($data)) 
			$this->say('The argument must be array');
		if (!$this->file_pointer) 
			$this->say('File pointer not defined');

		if ( $mult === true ) {
			for ($i=0; $i < count($data); $i++) { 
				$this->insert( $data[$i] );
			}
		} else {
			$_content = json_encode($data) . PHP_EOL;
			fwrite( $this->file_pointer, $_content );
		}
	}

	/**
	 * runs the list of values and if the value being pointed return some value or true, stop the loop.
	 *           if true, it returns the entire object, if nothing found return false.
	 * @param  function $callbk
	 * @return array|false
	 */
	public function get ( $callbk=null )
	{
		if (is_callable($callbk)) {
			foreach ($this->fetch() as $value):
				$func_result = $callbk( $value );
				if( true === $func_result ):
					return $value;
				elseif( ! $func_result ):
					continue;
				else:
					return $func_result;
				endif;
			endforeach;
			return false;
		} else {
			$this->say("the passed argument must be callable");
		}
	}

	/**
	 * Sugar for the function "each", if no closure filter function defined, return all data
{'fraction_non_alphanumeric': 0.12078559738134206, 'fraction_numerical': 0.002618657937806874, 'mean_word_length': 3.3126323218066336, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 41, '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': '3039815', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1846, 'n_words': 736}