<commit_before><?php

/**
 * Tableau
 *
 * A Kohana 3.1 module for quick table making.
 *
 *
 * @package Tableau
 * @author  <NAME>
 */
class Kohana_Tableau {

	/**
	 * Holds a list of column definitions
	 *
	 * @var array
	 */
	protected $columns = array();

	/**
	 * The data to make rows with
	 *
	 * @var array
	 */
	protected $data = array();

	/**
	 * An array of callbacks to be called on each row
	 *
	 * @var array
	 */
	protected $row_callbacks = array();

	/**
	 * An array of titles for rows
	 *
	 * @var array
	 */
	protected $row_titles = array();

	/**
	 * Should thead be rendered?
	 *
	 * @var bool
	 */
	protected $render_thead = true;

	/**
	 * Should tfoot be rendered?
	 *
	 * @var bool
	 */
	protected $render_tfoot = true;

	/**
	 * Additional rows to prepend to the regular row
	 *
	 * @var array
	 */
	protected $thead_rows_prepend = array();

	/**
	 * Additional rows to append to the regular row
	 *
	 * @var array
	 */
	protected $thead_rows_append = array();


	/*******************
	 **  Constructor  **
	 *******************/

	/**
	 * Create a new table with the (optional) given data and columns list.
	 *
	 * For the format of the columns array, see {@see addColumnsArray()}
	 *
	 * @param array|Database_Result $data
	 * @param array $columns
	 */
	public function __construct($data = array(), array $columns=array()) {
		$this->setData($data);
		$this->addColumnsArray($columns);
	}

	/**
	 * Factory that returns a new Tableau
	 *
	 * @see __construct()
	 * @param array|Database_Result $data
	 * @param array $columns
	 * @return Tableau
	 */
	public static function factory($data = array(), array $columns = array()) {
		return new Tableau($data, $columns);
	}


	/***************
	 **  Columns  **
	 ***************/

	/**
	 * Add a column to the end of the list.
	 *
	 * The key parameter relates to the value to be used in the data array.
	 *
	 * If the title or class is not set (ie: is null), they will
	 * be automatically filled in by the given key.
	 *
	 * An object of Tableau_Column is returned for further modifications (ex: adding callbacks)
	 *
	 * @param string $key
	 * @param string $title
	 * @param string $class
	 * @return Tableau_Column
	 */
	public function addColumn($key, $title=null, $class=null) {
		$column = new Tableau_Column($this, $key, $title, $class);
		$this->columns[$key] = $column;
		return $column;
	}

	/**
	 * Quickly add a handful of columns by passing in an array.
	 *
	 * Each entry in the array can either be a string, representing
	 * the columns key (with automatic title and class), or an array
	 * mimicking the arguments for {@see addColumn()}
	 *
	 * ex:
	 *  array(
	 *    'id',   // specify the key only, same as array('id')
	 *    array('user_name', 'name'),   // specify the key and title, class is still automatic
	 *    array('actions', ''),   // a column without a title
	 *    array('25', null, 'col_25')   // title remains automatic, however a class is specified
	 *  )
	 *
	 * @param array $columns
	 * @return Kohana_Tableau
	 */
	public function addColumnsArray(array $columns) {
		foreach ($columns as $col) {
			call_user_func_array(array($this, 'addColumn'), (array)$col);
		}
		return $this;
	}

	/**
	 * Simply removes the column of the given key and returns it
	 *
	 * @param $key
	 * @return Tableau_Column
	 */
	public function removeColumn($key) {
		$column = $this->columns[$key];
		unset($this->columns[$key]);
		return $column;
	}

	/**
	 * Returns the column of the given key
	 *
	 * @param $key
	 * @return Tableau_Column
	 */
	public function getColumn($key) {
		return $this->columns[$key];
	}


	/************
	 **  Rows  **
	 ************/

	/**
	 * Add callback which will be called on each row rendering, with optional
	 * arguments passed along.
	 *
	 * The callback should return an object of type Tableau_HTML_Tr
	 * and will be called in the following manner:
	 *
	 *  callback_func(Tableau_HTML_Tr $tr, $arguments[0], $arguments[1], etc...)
	 *
	 * @param callable $callback
	 * @param array $arguments
	 * @return Kohana_Tableau
	 */
	public function addRowCallback($callback, array $arguments = array( )) {
		$this->row_callbacks[] = array( $callback, $arguments );
		return $this;
	}

	/**
	 * Set the first column to be the given titles, rendered as THs
	 *
	 * @param array $titles<commit_msg>Added Tableau->addCallback() shortcut for Tableau->getColumn()->addCallback()<commit_after><?php

/**
 * Tableau
 *
 * A Kohana 3.1 module for quick table making.
 *
 *
 * @package Tableau
 * @author  <NAME>
 */
class Kohana_Tableau {

	/**
	 * Holds a list of column definitions
	 *
	 * @var array
	 */
	protected $columns = array();

	/**
	 * The data to make rows with
	 *
	 * @var array
	 */
	protected $data = array();

	/**
	 * An array of callbacks to be called on each row
	 *
	 * @var array
	 */
	protected $row_callbacks = array();

	/**
	 * An array of titles for rows
	 *
	 * @var array
	 */
	protected $row_titles = array();

	/**
	 * Should thead be rendered?
	 *
	 * @var bool
	 */
	protected $render_thead = true;

	/**
	 * Should tfoot be rendered?
	 *
	 * @var bool
	 */
	protected $render_tfoot = true;

	/**
	 * Additional rows to prepend to the regular row
	 *
	 * @var array
	 */
	protected $thead_rows_prepend = array();

	/**
	 * Additional rows to append to the regular row
	 *
	 * @var array
	 */
	protected $thead_rows_append = array();


	/*******************
	 **  Constructor  **
	 *******************/

	/**
	 * Create a new table with the (optional) given data and columns list.
	 *
	 * For the format of the columns array, see {@see addColumnsArray()}
	 *
	 * @param array|Database_Result $data
	 * @param array $columns
	 */
	public function __construct($data = array(), array $columns=array()) {
		$this->setData($data);
		$this->addColumnsArray($columns);
	}

	/**
	 * Factory that returns a new Tableau
	 *
	 * @see __construct()
	 * @param array|Database_Result $data
	 * @param array $columns
	 * @return Tableau
	 */
	public static function factory($data = array(), array $columns = array()) {
		return new Tableau($data, $columns);
	}


	/***************
	 **  Columns  **
	 ***************/

	/**
	 * Add a column to the end of the list.
	 *
	 * The key parameter relates to the value to be used in the data array.
	 *
	 * If the title or class is not set (ie: is null), they will
	 * be automatically filled in by the given key.
	 *
	 * An object of Tableau_Column is returned for further modifications (ex: adding callbacks)
	 *
	 * @param string $key
	 * @param string $title
	 * @param string $class
	 * @return Tableau_Column
	 */
	public function addColumn($key, $title=null, $class=null) {
		$column = new Tableau_Column($this, $key, $title, $class);
		$this->columns[$key] = $column;
		return $column;
	}

	/**
	 * Quickly add a handful of columns by passing in an array.
	 *
	 * Each entry in the array can either be a string, representing
	 * the columns key (with automatic title and class), or an array
	 * mimicking the arguments for {@see addColumn()}
	 *
	 * ex:
	 *  array(
	 *    'id',   // specify the key only, same as array('id')
	 *    array('user_name', 'name'),   // specify the key and title, class is still automatic
	 *    array('actions', ''),   // a column without a title
	 *    array('25', null, 'col_25')   // title remains automatic, however a class is specified
	 *  )
	 *
	 * @param array $columns
	 * @return Kohana_Tableau
	 */
	public function addColumnsArray(array $columns) {
		foreach ($columns as $col) {
			call_user_func_array(array($this, 'addColumn'), (array)$col);
		}
		return $this;
	}

	/**
	 * Simply removes the column of the given key and returns it
	 *
	 * @param $key
	 * @return Tableau_Column
	 */
	public function removeColumn($key) {
		$column = $this->columns[$key];
		unset($this->columns[$key]);
		return $column;
	}

	/**
	 * Returns the column of the given key
	 *
	 * @param $key
	 * @return Tableau_Column
	 */
	public function getColumn($key) {
		return $this->columns[$key];
	}
	
	/**
	 * Add a callback to a column, shortcut for getColumn()->addCallback()
	 *
	 * @param string $column
	 * @param callable $callback
	 * @param mixed $arguments,...
	 * @return Tableau
	 */
	public function addCallback($column, $callback, $arguments=null) {
		$callback_and_arguments = func_get_args();
		array_shift($callback_and_arguments);
		
		call_user_func_array(array($this->getColumn($column), 'addCallback'), $callback_and_arguments);
		
		return $this;
	}


	/************
	 **  Rows  **
	 ************/

	/**
	 * Add callback which will be called on each row rendering, with optional
	 * arguments passed along.
	 *
	 * The callback should return an object of type Tableau_HTML_Tr
	 * and will be called in the following manner:
	 *
	 *  callback_func(Tableau_HTML_Tr $tr, $arguments[0], $arguments[1], etc...)
	 *
	 * @param callable $callback
	 * @param array $arguments
	 * @return Kohana_Tableau
	 */
	public function addRowCallback($callback, array $arguments = array( )) {
		$this->row_callbacks[] = array( $callback, $arguments );
		return $this;
	}

	/**
	 * Set the first column to be the given titles, rendered as THs
	 *
	 * @param array $titles
{'fraction_non_alphanumeric': 0.14830139372822299, 'fraction_numerical': 0.0017421602787456446, 'mean_word_length': 3.159873188405797, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 24, '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': '783615', 'n_tokens_mistral': 3063, 'n_tokens_neox': 2730, 'n_words': 962}