<commit_before><?php
/**
 * @version		$Id$
 * @category	Koowa
 * @package     Koowa_Database
 * @subpackage  Rowset
 * @copyright	Copyright (C) 2007 - 2010 <NAME>. All rights reserved.
 * @license		GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
 * @link     	http://www.koowa.org
 */

/**
 * Table Rowset Class
 *
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_Database
 * @subpackage  Rowset
 * @uses 		KMixinClass
 */
class KDatabaseRowsetTable extends KDatabaseRowsetAbstract
{
	/**
	 * Table object or identifier (APP::com.COMPONENT.table.NAME)
	 *
	 * @var	string|object
	 */
	protected $_table;

	/**
	 * Constructor
	 *
	 * @param 	object 	An optional KConfig object with configuration options.
	 */
	public function __construct(KConfig $config = null)
	{
		parent::__construct($config);
		
		// Set the table indentifier.
		if(isset($config->table)) {
			$this->setTable($config->table);
		}
	}

	/**
	 * Initializes the options for the object
	 *
	 * Called from {@link __construct()} as a first step of object instantiation.
	 *
	 * @param 	object 	An optional KConfig object with configuration options.
	 * @return void
	 */
	protected function _initialize(KConfig $config)
	{
		$config->append(array(
			'table'	=> null
		));

		parent::_initialize($config);
	}

	/**
	 * Get the identifier for the table with the same name
	 *
	 * @return	KIdentifierInterface
	 */
	public function getTable()
	{
		if(!$this->_table)
		{
			$identifier 		= clone $this->_identifier;
			$identifier->name	= KInflector::tableize($identifier->name);
			$identifier->path	= array('database', 'table');

			$this->_table = KFactory::get($identifier);
		}

		return $this->_table;
	}

	/**
	 * Method to set a table object attached to the rowset
	 *
	 * @param	mixed	An object that implements KObjectIdentifiable, an object that
	 *                  implements KIndentifierInterface or valid identifier string
	 * @throws	KDatabaseRowsetException	If the identifier is not a table identifier
	 * @return	KDatabaseRowsetAbstract
	 */
	public function setTable($table)
	{
		if(!($table instanceof KDatabaseTableAbstract))
		{
			$identifier = KFactory::identify($table);

			if($identifier->path[0] != 'table') {
				throw new KModelException('Identifier: '.$identifier.' is not a table identifier');
			}

			$table = KFactory::get($identifier);
		}

		$this->_table = $table;

		return $this;
	}

	/**
	 * Reset the rowset
	 *
	 * @return boolean	If successfull return TRUE, otherwise FALSE
	 */
	public function reset()
	{
		$result = parent::reset();
		
	    $this->_columns	   = array_keys($this->getTable()->getColumns());
	
		return $result;
	}

	/**
	 * Get an empty row
	 *
	 * @return	object	A KDatabaseRow object.
	 */
	public function getRow() 
	{
		return $this->getTable()->getRow();
	}

	/**
	 * Forward the call to each row
	 * 
	 * This functions overloads KDatabaseRowsetAbstract::__call and implements 
	 * a just in time mixin strategy. Available table behaviors are only mixed 
	 * when needed.
	 *
	 * @param  string 	The function name
	 * @param  array  	The function arguments
	 * @throws BadMethodCallException 	If method could not be found
	 * @return mixed The result of the function
	 */
	public function __call($method, array $arguments)
	{
	    // If the method hasn't been mixed yet, load all the behaviors.
		if(!isset($this->_mixed_methods[$method]))
		{
			foreach($this->getTable()->getBehaviors() as $behavior) {
				$this->mixin($behavior);
			}
		}

		return parent::__call($method, $arguments);
	}
}<commit_msg>- Added new isConnected() function. Will return TRUE if the row is connected to a live table. - Refactored setTable() and getTable() functions. Implemented a default and forced contructor to call setTable() - Fixed issue with getTable() called before the constructor of KDatabaseRowsetTable call ran through the reset() function. Solved by checking if the table is connected and added extra reset() code to the constructor of KDatabaseTableRow - Added isConnected checks to all functions to prevent operations on the table is the row object is not connected.<commit_after><?php
/**
 * @version		$Id$
 * @category	Koowa
 * @package     Koowa_Database
 * @subpackage  Rowset
 * @copyright	Copyright (C) 2007 - 2010 <NAME>. All rights reserved.
 * @license		GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
 * @link     	http://www.koowa.org
 */

/**
 * Table Rowset Class
 *
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_Database
 * @subpackage  Rowset
 * @uses 		KMixinClass
 */
class KDatabaseRowsetTable extends KDatabaseRowsetAbstract
{
	/**
	 * Table object or identifier (APP::com.COMPONENT.table.NAME)
	 *
	 * @var	string|object
	 */
	protected $_table = false;

	/**
	 * Constructor
	 *
	 * @param 	object 	An optional KConfig object with configuration options.
	 */
	public function __construct(KConfig $config = null)
	{
		parent::__construct($config);
		
		$this->setTable($config->table);
			    
		// Reset the rowset
        $this->reset();
	    
        // Insert the data, if exists        
        if(!empty($config->data)) {
	        $this->addData($config->data->toArray(), $config->new);	
        }
	}

	/**
	 * Initializes the options for the object
	 *
	 * Called from {@link __construct()} as a first step of object instantiation.
	 *
	 * @param 	object 	An optional KConfig object with configuration options.
	 * @return void
	 */
	protected function _initialize(KConfig $config)
	{
		$config->append(array(
			'table'	=> $this->_identifier->name
		));

		parent::_initialize($config);
	}

	/**
     * Method to get a table object
     * 
     * Function catches KDatabaseTableExceptions that are thrown for tables that 
     * don't exist. If no table object can be created the function will return FALSE.
     *
     * @return KDatabaseTableAbstract
     */
     public function getTable()
    {
        if($this->_table !== false)
        {
            if(!($this->_table instanceof KDatabaseTableAbstract))
		    {   		        
		        try {
		            $this->_table = KFactory::get($this->_table);
                } catch (KDatabaseTableException $e) {
                    $this->_table = false;
                }
            }
        }

        return $this->_table;
    }

	/**
	 * Method to set a table object attached to the rowset
	 *
	 * @param	mixed	An object that implements KObjectIdentifiable, an object that
	 *                  implements KIndentifierInterface or valid identifier string
	 * @throws	KDatabaseRowsetException	If the identifier is not a table identifier
	 * @return	KDatabaseRowsetAbstract
	 */
	public function setTable($table)
	{
		if(!($table instanceof KDatabaseTableAbstract))
		{
			if(is_string($table) && strpos($table, '.') === false ) 
		    {
		        $identifier         = clone $this->_identifier;
		        $identifier->path   = array('database', 'table');
		        $identifier->name   = KInflector::tableize($table);
		    }
		    else  $identifier = KFactory::identify($table);
		    
			if($identifier->path[1] != 'table') {
				throw new KDatabaseRowsetException('Identifier: '.$identifier.' is not a table identifier');
			}

			$table = $identifier;
		}

		$this->_table = $table;

		return $this;
	}
	
	/**
	 * Test the connected status of the row.
	 *
	 * @return	boolean	Returns TRUE if we have a reference to a live KDatabaseTableAbstract object.
	 */
    public function isConnected()
	{
	    return (bool) $this->getTable();
	}

	/**
	 * Reset the rowset
	 *
	 * @return boolean	If successfull return TRUE, otherwise FALSE
	 */
	public function reset()
	{
		$result = parent::reset();
		
		if($this->isConnected()) {
	        $this->_columns	= array_keys($this->getTable()->getColumns());
		}
	
		return $result;
	}

	/**
	 * Get an empty row
	 *
	 * @return	object	A KDatabaseRow object.
	 */
	public function getRow() 
	{
		$result = null;
		
	    if($this->isConnected()) {
		    $result = $this->getTable()->getRow();
		}
	    
	    return $result;
	}

	/**
	 * Forward the call to each row
	 * 
	 * This functions overloads KDatabaseRowsetAbstract::__call and implements 
	 * a just in time mixin strategy. Available table behaviors are only mixed 
	 * when needed.
	 *
	 * @param  string 	The function name
	 * @param  array  	The function arguments
	 * @throws BadMethodCallException 	If method could not be found
	 * @return mixed The result of the function
	 */
	public function __call($method, array $arguments)
	{
	    // If the method hasn't been mixed yet, load all the behaviors.
		if($this->isConnected() && !isset($this->_mixed_methods[$method]))
		{
			foreach($this->getTable()->getBehaviors() as $behavior) {
				$this->mixin($behavior);
			}
		}

		return parent::__call($method, $arguments);
	}
}
{'fraction_non_alphanumeric': 0.1224929178470255, 'fraction_numerical': 0.0027195467422096316, 'mean_word_length': 3.0027210884353743, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5208453', 'n_tokens_mistral': 2883, 'n_tokens_neox': 2563, 'n_words': 776}