<commit_before><?php
/**
* AutoModeler
*
* @package        AutoModeler
* @author         <NAME>
* @copyright      (c) 2010 <NAME>
* @license        http://www.opensource.org/licenses/isc-license.txt
*/
class AutoModeler extends Model implements ArrayAccess
{
	const VERSION = '3.7.0';

	// The database table name
	protected $_table_name = '';

	// The database fields
	protected $_data = array();

	// Validation rules in a 'field' => 'rules' array
	protected $_rules = array();
	protected $_callbacks = array();

	protected $_validation = array();

	protected $_validated = FALSE;

	protected $_lang = 'form_errors';

	protected $_state = AutoModeler::STATE_NEW;

	const STATE_NEW = 'new';
	const STATE_LOADING = 'loading';
	const STATE_LOADED = 'loaded';
	const STATE_DELETED = 'deleted';

	// Lists available states for this model
	protected $_states = array(
		AutoModeler::STATE_NEW,
		AutoModeler::STATE_LOADING,
		AutoModeler::STATE_LOADED,
		AutoModeler::STATE_DELETED
	);

	/**
	 * Standard constructor, accepts an `id` column to look for
	 *
	 * @param string $id  an id to search for
	 *
	 */
	public function __construct($id = NULL)
	{
		parent::__construct();

		if ($id !== NULL)
		{
			$this->load(db::select_array(array_keys($this->_data))->where('id', '=', $id));
		}
	}

	/**
	 * Loads a database result. Can be used to load a single item into this model
	 * or return a result set of many models. You can pass any query builder object
	 * into the first parameter to load the specific data you need. Common usage:
	 * 
	 * 	$user = new Model_User;
	 * 	// Load a specific row
	 * 	$user->load(db::select_array($user->fields())->where('id', '=', '1'));
	 * 
	 * 	// Load many rows with where
	 * 	$result = Model::factory('user')->load(db::select_array($user->fields())->where('id', '>', '3'), NULL);
	 * 
	 * 	// Load all rows
	 * 	$result = Model::factory('user')->load(NULL, NULL);
	 * 
	 * 	// Load first two rows
	 * 	$result = Model::factory('user')->load(NULL, 2);
	 * 
	 * @param Database_Query_Builder_Select $query an optional query builder object to load with
	 * @param integer                       $limit a number greater than one will return a data set
	 *
	 * @return $this when loading one object
	 * @return Database_Result when loading multiple results
	 */
	public function load(Database_Query_Builder_Select $query = NULL, $limit = 1)
	{
		// Start
		$this->_state = AutoModeler::STATE_LOADING;

		// Use a normal select query by default
		if ($query == NULL)
		{
			$query = db::select_array(array_keys($this->_data));
		}

		// Add limit if passed
		if ($limit)
		{
			$query->limit($limit);
		}

		$query->from($this->_table_name);

		// If we are going to return a data set, we want objects back
		if ($limit != 1)
		{
			$query->as_object(get_class($this));
		}

		$data = $query->execute($this->_db);

		if ($limit != 1)
		{
			return $data;
		}

		// Process the results with this model's logic
		if (count($data) AND $data = $data->current())
		{
			$this->process_load($data);
		}

		// We are done!
		$this->_state = AutoModeler::STATE_LOADED;

		return $this;
	}

	/**
	 * Processes a load() from a result
	 *
	 * @return null
	 */
	protected function process_load($data)
	{
		$this->_data = $data;
	}

	/**
	 * Magic get method, gets model properties from the db
	 *
	 * @param string $key the field name to look for
	 * 
	 * @throws AutoModeler_Exception
	 *
	 * @return String
	 */
	public function __get($key)
	{
		if (array_key_exists($key, $this->_data))
		 	return $this->_data[$key];

		throw new AutoModeler_Exception('Field '.$key.' does not exist in '.get_class($this).'!', array(), '');
	}

	/**
	 * Magic get method, set model properties to the model
	 *
	 * @param string $key   the field name to set
	 * @param string $value the value to set to
	 * 
	 * @throws AutoModeler_Exception
	 *<commit_msg>Fix problem with model state if non NULL value is passed to load() but it isnt found<commit_after><?php
/**
* AutoModeler
*
* @package        AutoModeler
* @author         <NAME>
* @copyright      (c) 2010 <NAME>
* @license        http://www.opensource.org/licenses/isc-license.txt
*/
class AutoModeler extends Model implements ArrayAccess
{
	const VERSION = '3.7.0';

	// The database table name
	protected $_table_name = '';

	// The database fields
	protected $_data = array();

	// Validation rules in a 'field' => 'rules' array
	protected $_rules = array();
	protected $_callbacks = array();

	protected $_validation = array();

	protected $_validated = FALSE;

	protected $_lang = 'form_errors';

	protected $_state = AutoModeler::STATE_NEW;

	const STATE_NEW = 'new';
	const STATE_LOADING = 'loading';
	const STATE_LOADED = 'loaded';
	const STATE_DELETED = 'deleted';

	// Lists available states for this model
	protected $_states = array(
		AutoModeler::STATE_NEW,
		AutoModeler::STATE_LOADING,
		AutoModeler::STATE_LOADED,
		AutoModeler::STATE_DELETED
	);

	/**
	 * Standard constructor, accepts an `id` column to look for
	 *
	 * @param string $id  an id to search for
	 *
	 */
	public function __construct($id = NULL)
	{
		parent::__construct();

		if ($id !== NULL)
		{
			$this->load(db::select_array(array_keys($this->_data))->where('id', '=', $id));
		}
	}

	/**
	 * Loads a database result. Can be used to load a single item into this model
	 * or return a result set of many models. You can pass any query builder object
	 * into the first parameter to load the specific data you need. Common usage:
	 * 
	 * 	$user = new Model_User;
	 * 	// Load a specific row
	 * 	$user->load(db::select_array($user->fields())->where('id', '=', '1'));
	 * 
	 * 	// Load many rows with where
	 * 	$result = Model::factory('user')->load(db::select_array($user->fields())->where('id', '>', '3'), NULL);
	 * 
	 * 	// Load all rows
	 * 	$result = Model::factory('user')->load(NULL, NULL);
	 * 
	 * 	// Load first two rows
	 * 	$result = Model::factory('user')->load(NULL, 2);
	 * 
	 * @param Database_Query_Builder_Select $query an optional query builder object to load with
	 * @param integer                       $limit a number greater than one will return a data set
	 *
	 * @return $this when loading one object
	 * @return Database_Result when loading multiple results
	 */
	public function load(Database_Query_Builder_Select $query = NULL, $limit = 1)
	{
		// Start
		$this->_state = AutoModeler::STATE_LOADING;

		// Use a normal select query by default
		if ($query == NULL)
		{
			$query = db::select_array(array_keys($this->_data));
		}

		// Add limit if passed
		if ($limit)
		{
			$query->limit($limit);
		}

		$query->from($this->_table_name);

		// If we are going to return a data set, we want objects back
		if ($limit != 1)
		{
			$query->as_object(get_class($this));
		}

		$data = $query->execute($this->_db);

		if ($limit != 1)
		{
			return $data;
		}

		// Process the results with this model's logic
		if (count($data) AND $data = $data->current())
		{
			$this->process_load($data);
		}

		// We are done!
		$this->process_load_state();

		return $this;
	}

	/**
	 * Processes a load() from a result
	 *
	 * @return null
	 */
	protected function process_load($data)
	{
		$this->_data = $data;
	}

	/**
	 * Processes the object state before a load() finishes
	 *
	 * @return null
	 */
	public function process_load_state()
	{
		if ($this->id)
		{
			$this->_state = AutoModeler::STATE_LOADED;
		}
		else
		{
			$this->_state = AutoModeler::STATE_NEW;
		}
	}

	/**
	 * Magic get method, gets model properties from the db
	 *
	 * @param string $key the field name to look for
	 * 
	 * @throws AutoModeler_Exception
	 *
	 * @return String
	 */
	public function __get($key)
	{
		if (array_key_exists($key, $this->_data))
		 	return $this->_data[$key];

		throw new AutoModeler_Exception('Field '.$key.' does not exist in '.get_class($this).'!', array(), '');
	}

	/**
	 * Magic get method, set model properties to the model
	 *
	 * @param string $key   the field name to set
	 * @param string $value the value to set to
	 * 
	 * @throws AutoModeler_Exception
	 *
{'fraction_non_alphanumeric': 0.14500497017892644, 'fraction_numerical': 0.0032306163021868788, 'mean_word_length': 3.2452531645569622, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1271043', 'n_tokens_mistral': 2844, 'n_tokens_neox': 2537, 'n_words': 783}