<commit_before><?php
namespace Sy\Component\Html;

use Sy\Component\Html\Table;

class DataTable extends Table {

	private $options;

	/**
	 * Return if DataTable must align numeric value
	 *
	 * @return bool
	 */
	private function hasAlign() {
		return isset($this->options['align']);
	}

	/**
	 * Return if DataTable must align numeric value
	 *
	 * @return bool
	 */
	private function hasNumAlign() {
		return isset($this->options['num_align']);
	}

	/**
	 * Return if DataTable must format numeric value
	 *
	 * @return bool
	 */
	private function hasNumFormat() {
		return isset($this->options['num_decimals']);
	}

	/**
	 * Set value alignment
	 *
	 * @param string $align Alignement available: 'left', 'center', 'right', 'justify', 'char'
	 */
	public function setAlign($align) {
		switch ($align) {
			case 'left':
			case 'center':
			case 'right':
			case 'justify':
			case 'char':
				$this->options['align'] = $align; break;
		}
	}

	/**
	 * Set numeric value alignment
	 *
	 * @param string $align Alignement available: 'left', 'center', 'right', 'justify', 'char'
	 */
	public function setNumAlign($align) {
		switch ($align) {
			case 'left':
			case 'center':
			case 'right':
			case 'justify':
			case 'char':
				$this->options['num_align'] = $align; break;
		}
	}

	/**
	 * Set numeric value format
	 *
	 * @param int $decimals Sets the number of decimal points.
	 * @param string $decPoint Sets the separator for the decimal point.
	 * @param string $thousandsSep Sets the thousands separator.
	 */
	public function setNumFormat($decimals = 0, $decPoint = '.', $thousandsSep = ',') {
		$this->options['num_decimals']      = $decimals;
		$this->options['num_dec_point']     = $decPoint;
		$this->options['num_thousands_sep'] = $thousandsSep;
	}

	/**
	 * Set head row
	 *
	 * @param array $heads Array of head data
	 */
	public function setHeads(array $heads) {
		$header = $this->getTHead();
		$header->setElements(array());
		$tr = $header->addTr();
		foreach ($heads as $head) {
			$tr->addTh($head);
		}
	}

	/**
	 * Set foot row
	 *
	 * @param array $foots Array of foot data
	 */
	public function setFoots(array $foots) {
		$footer = $this->getTFoot();
		$footer->setElements(array());
		$tr = $footer->addTr();
		foreach ($foots as $foot) {
			$tr->addTd($foot);
		}
	}

	/**
	 * Add a row in the table
	 *
	 * @param array $datas Array of data
	 */
	public function addRow(array $datas) {
		$tr = $this->getTBody()->addTr();
		if ($this->hasAlign())
			$tr->setAttribute('align', $this->options['align']);
		foreach ($datas as $data) {
			$isNumeric = is_numeric($data);
			if ($this->hasNumFormat())
				$data = $isNumeric ? number_format($data, $this->options['num_decimals'], $this->options['num_dec_point'], $this->options['num_thousands_sep']) : $data;
			$td = $tr->addTd($data);
			if ($this->hasNumAlign() and $isNumeric)
				$td->setAttribute('align', $this->options['num_align']);
		}
	}

	/**<commit_msg>[DataTable] Add new method: addPregReplace<commit_after><?php
namespace Sy\Component\Html;

use Sy\Component\Html\Table;

class DataTable extends Table {

	private $options;

	private $replaces;

	/**
	 * Return if DataTable must align numeric value
	 *
	 * @return bool
	 */
	private function hasAlign() {
		return isset($this->options['align']);
	}

	/**
	 * Return if DataTable must align numeric value
	 *
	 * @return bool
	 */
	private function hasNumAlign() {
		return isset($this->options['num_align']);
	}

	/**
	 * Return if DataTable must format numeric value
	 *
	 * @return bool
	 */
	private function hasNumFormat() {
		return isset($this->options['num_decimals']);
	}

	/**
	 * Return if DataTable must perform a regular expression search and replace
	 *
	 * @return bool
	 */
	private function hasPregReplace() {
		return isset($this->replaces);
	}

	/**
	 * Set value alignment
	 *
	 * @param string $align Alignement available: 'left', 'center', 'right', 'justify', 'char'
	 */
	public function setAlign($align) {
		switch ($align) {
			case 'left':
			case 'center':
			case 'right':
			case 'justify':
			case 'char':
				$this->options['align'] = $align; break;
		}
	}

	/**
	 * Set numeric value alignment
	 *
	 * @param string $align Alignement available: 'left', 'center', 'right', 'justify', 'char'
	 */
	public function setNumAlign($align) {
		switch ($align) {
			case 'left':
			case 'center':
			case 'right':
			case 'justify':
			case 'char':
				$this->options['num_align'] = $align; break;
		}
	}

	/**
	 * Set numeric value format
	 *
	 * @param int $decimals Sets the number of decimal points.
	 * @param string $decPoint Sets the separator for the decimal point.
	 * @param string $thousandsSep Sets the thousands separator.
	 */
	public function setNumFormat($decimals = 0, $decPoint = '.', $thousandsSep = ',') {
		$this->options['num_decimals']      = $decimals;
		$this->options['num_dec_point']     = $decPoint;
		$this->options['num_thousands_sep'] = $thousandsSep;
	}

	/**
	 * Perform a regular expression search and replace
	 *
	 * @param mixed $pattern
	 * @param miced $replacement
	 */
	public function addPregReplace($pattern, $replacement) {
		$this->replaces[] = array('pattern' => $pattern, 'replacement' => $replacement);
	}

	/**
	 * Set head row
	 *
	 * @param array $heads Array of head data
	 */
	public function setHeads(array $heads) {
		$header = $this->getTHead();
		$header->setElements(array());
		$tr = $header->addTr();
		foreach ($heads as $head) {
			$tr->addTh($head);
		}
	}

	/**
	 * Set foot row
	 *
	 * @param array $foots Array of foot data
	 */
	public function setFoots(array $foots) {
		$footer = $this->getTFoot();
		$footer->setElements(array());
		$tr = $footer->addTr();
		foreach ($foots as $foot) {
			$tr->addTd($foot);
		}
	}

	/**
	 * Add a row in the table
	 *
	 * @param array $datas Array of data
	 */
	public function addRow(array $datas) {
		$tr = $this->getTBody()->addTr();
		if ($this->hasAlign())
			$tr->setAttribute('align', $this->options['align']);
		foreach ($datas as $data) {
			$isNumeric = is_numeric($data);
			if ($this->hasNumFormat())
				$data = $isNumeric ? number_format($data, $this->options['num_decimals'], $this->options['num_dec_point'], $this->options['num_thousands_sep']) : $data;
			if ($this->hasPregReplace()) {
				foreach ($this->replaces as $replace)
					$data = preg_replace($replace['pattern'], $replace['replacement'], $data);
			}
			$td = $tr->addTd($data);
			if ($this->hasNumAlign() and $isNumeric)
				$td->setAttribute('align', $this->options['num_align']);
		}
	}

	/**
{'fraction_non_alphanumeric': 0.17241905925473427, 'fraction_numerical': 0.00030543677458766036, 'mean_word_length': 3.5957894736842104, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, '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': '4105926', 'n_tokens_mistral': 2430, 'n_tokens_neox': 2130, 'n_words': 558}