<commit_before><?php

/**
 * @package         Billing
 * @copyright       Copyright (C) 2012-2018 S.D.O.C. LTD. All rights reserved.
 * @license         GNU Affero General Public License Version 3; see LICENSE.txt
 */

/**
 * Billing exporter to CSV
 *
 * @package  Billing
 * @since    5.9
 */
class Billrun_Exporter_Csv extends Billrun_Exporter_File {
	
	static protected $type = 'csv';
	
	/**
	 * CSV delimiter
	 * 
	 * @var string
	 */
	protected $delimiter = '';
	
	protected $fixedWidth = false;
	
	public function __construct($options = array()) {
		parent::__construct($options);
		$this->fixedWidth = Billrun_Util::getIn($this->config, 'exporter.format.type', 'delimiter') === 'fixed_width';
		$this->delimiter = $this->getDelimiter();
	}
	
	/**
	 * get delimiter character for 1 row in exported file
	 * 
	 * @return string
	 */
	protected function getDelimiter() {
		if ($this->fixedWidth) {
			return '';
		}
		return Billrun_Util::getIn($this->config, 'format.delimiter', ',');
	}
	
	/**
	 * see parent::formatValue
	 */
	protected function formatData($row, $type = 'data') {
		if (!$this->fixedWidth) {
			return $row;
		}

		switch ($type) {
			case 'header':
				$widthMappingField = 'header_mapping';
				break;
			case 'footer':
				$widthMappingField = 'footer_mapping';
				break;
			case 'data':
			default:
				$widthMappingField = 'fields_mapping';
				break;
		}
		foreach ($row as $field => $value) {
			$width = Billrun_Util::getIn($this->config, array('exporter', 'format', 'widths', $widthMappingField, $field), strlen($value));
			$row[$field] = str_pad($value, $width, ' ', STR_PAD_LEFT);
		}
		return $row;
	}

	/**
	 * see parent::exportRowToFile
	 */
	protected function exportRowToFile($fp, $row, $type = 'data') {
		$rowToExport = $this->formatData($row, $type);
		fputs($fp, implode($rowToExport, $this->delimiter) . PHP_EOL);
	}

}
<commit_msg>limit field number of characters according to fixed_width<commit_after><?php

/**
 * @package         Billing
 * @copyright       Copyright (C) 2012-2018 S.D.O.C. LTD. All rights reserved.
 * @license         GNU Affero General Public License Version 3; see LICENSE.txt
 */

/**
 * Billing exporter to CSV
 *
 * @package  Billing
 * @since    5.9
 */
class Billrun_Exporter_Csv extends Billrun_Exporter_File {
	
	static protected $type = 'csv';
	
	/**
	 * CSV delimiter
	 * 
	 * @var string
	 */
	protected $delimiter = '';
	
	protected $fixedWidth = false;
	
	public function __construct($options = array()) {
		parent::__construct($options);
		$this->fixedWidth = Billrun_Util::getIn($this->config, 'exporter.format.type', 'delimiter') === 'fixed_width';
		$this->delimiter = $this->getDelimiter();
	}
	
	/**
	 * get delimiter character for 1 row in exported file
	 * 
	 * @return string
	 */
	protected function getDelimiter() {
		if ($this->fixedWidth) {
			return '';
		}
		return Billrun_Util::getIn($this->config, 'format.delimiter', ',');
	}
	
	/**
	 * see parent::formatValue
	 */
	protected function formatData($row, $type = 'data') {
		if (!$this->fixedWidth) {
			return $row;
		}

		switch ($type) {
			case 'header':
				$widthMappingField = 'header_mapping';
				break;
			case 'footer':
				$widthMappingField = 'footer_mapping';
				break;
			case 'data':
			default:
				$widthMappingField = 'fields_mapping';
				break;
		}
		foreach ($row as $field => $value) {
			$width = Billrun_Util::getIn($this->config, array('exporter', 'format', 'widths', $widthMappingField, $field), strlen($value));
			$row[$field] = str_pad($value, $width, ' ', STR_PAD_LEFT);
                        if (strlen($row[$field]) > $width){
                            $row[$field] = substr($row[$field], 0, $width);
                        }
		}
		return $row;
	}

	/**
	 * see parent::exportRowToFile
	 */
	protected function exportRowToFile($fp, $row, $type = 'data') {
		$rowToExport = $this->formatData($row, $type);
		fputs($fp, implode($rowToExport, $this->delimiter) . PHP_EOL);
	}

}

{'fraction_non_alphanumeric': 0.16355846774193547, 'fraction_numerical': 0.006300403225806451, 'mean_word_length': 3.2954545454545454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, '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': '7369265', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1291, 'n_words': 298}