Merge pull request #33 from williamknauss/code-cleanup2

Code cleanup2
This commit is contained in:
2014-06-06 09:35:40 +01:00

View File

@@ -351,23 +351,23 @@ class parseCSV {
* @param [string] conditions Basic SQL-like conditions for row matching * @param [string] conditions Basic SQL-like conditions for row matching
*/ */
public function __construct ($input = null, $offset = null, $limit = null, $conditions = null, $keep_file_data = null) { public function __construct ($input = null, $offset = null, $limit = null, $conditions = null, $keep_file_data = null) {
if ( $offset !== null ) { if (!is_null($offset)) {
$this->offset = $offset; $this->offset = $offset;
} }
if ( $limit !== null ) { if (!is_null($limit)) {
$this->limit = $limit; $this->limit = $limit;
} }
if ( !is_null($conditions) ) { if (!is_null($conditions)) {
$this->conditions = $conditions; $this->conditions = $conditions;
} }
if ( !is_null($keep_file_data) ) { if (!is_null($keep_file_data)) {
$this->keep_file_data = $keep_file_data; $this->keep_file_data = $keep_file_data;
} }
if ( !empty($input) ) { if (!empty($input)) {
$this->parse($input); $this->parse($input);
} }
} }
@@ -391,24 +391,24 @@ class parseCSV {
* @return [bool] * @return [bool]
*/ */
public function parse ($input = null, $offset = null, $limit = null, $conditions = null) { public function parse ($input = null, $offset = null, $limit = null, $conditions = null) {
if ( $input === null ) { if (is_null($input)) {
$input = $this->file; $input = $this->file;
} }
if ( !empty($input) ) { if (!empty($input)) {
if ( $offset !== null ) { if (!is_null($offset)) {
$this->offset = $offset; $this->offset = $offset;
} }
if ($limit !== null ) { if (!is_null($limit)) {
$this->limit = $limit; $this->limit = $limit;
} }
if ( !is_null($conditions) ) { if (!is_null($conditions)) {
$this->conditions = $conditions; $this->conditions = $conditions;
} }
if ( is_readable($input) ) { if (is_readable($input)) {
$this->data = $this->parse_file($input); $this->data = $this->parse_file($input);
} }
else { else {
@@ -416,7 +416,7 @@ class parseCSV {
$this->data = $this->parse_string(); $this->data = $this->parse_string();
} }
if ( $this->data === false ) { if ($this->data === false) {
return false; return false;
} }
} }
@@ -437,7 +437,7 @@ class parseCSV {
* @return [bool] * @return [bool]
*/ */
public function save ($file = null, $data = array(), $append = false, $fields = array()) { public function save ($file = null, $data = array(), $append = false, $fields = array()) {
if ( empty($file) ) { if (empty($file)) {
$file = &$this->file; $file = &$this->file;
} }
@@ -460,17 +460,17 @@ class parseCSV {
* @return [string] * @return [string]
*/ */
public function output ($filename = null, $data = array(), $fields = array(), $delimiter = null) { public function output ($filename = null, $data = array(), $fields = array(), $delimiter = null) {
if ( empty($filename) ) { if (empty($filename)) {
$filename = $this->output_filename; $filename = $this->output_filename;
} }
if ( $delimiter === null ) { if ($delimiter === null) {
$delimiter = $this->output_delimiter; $delimiter = $this->output_delimiter;
} }
$data = $this->unparse($data, $fields, null, null, $delimiter); $data = $this->unparse($data, $fields, null, null, $delimiter);
if ( $filename !== null ) { if (!is_null($filename)) {
header('Content-type: application/csv'); header('Content-type: application/csv');
header('Content-Length: '.strlen($data)); header('Content-Length: '.strlen($data));
header('Cache-Control: no-cache, must-revalidate'); header('Cache-Control: no-cache, must-revalidate');
@@ -494,11 +494,11 @@ class parseCSV {
*/ */
public function encoding ($input = null, $output = null) { public function encoding ($input = null, $output = null) {
$this->convert_encoding = true; $this->convert_encoding = true;
if ( $input !== null ) { if (!is_null($input)) {
$this->input_encoding = $input; $this->input_encoding = $input;
} }
if ( $output !== null ) { if (!is_null($output)) {
$this->output_encoding = $output; $this->output_encoding = $output;
} }
} }
@@ -518,24 +518,24 @@ class parseCSV {
* @return [string] * @return [string]
*/ */
public function auto ($file = null, $parse = true, $search_depth = null, $preferred = null, $enclosure = null) { public function auto ($file = null, $parse = true, $search_depth = null, $preferred = null, $enclosure = null) {
if ( $file === null ) { if (is_null($file)) {
$file = $this->file; $file = $this->file;
} }
if ( empty($search_depth) ) { if (empty($search_depth)) {
$search_depth = $this->auto_depth; $search_depth = $this->auto_depth;
} }
if ( $enclosure === null ) { if (is_null($enclosure)) {
$enclosure = $this->enclosure; $enclosure = $this->enclosure;
} }
if ( $preferred === null ) { if (is_null($preferred)) {
$preferred = $this->auto_preferred; $preferred = $this->auto_preferred;
} }
if ( empty($this->file_data) ) { if (empty($this->file_data)) {
if ( $this->_check_data($file) ) { if ($this->_check_data($file)) {
$data = &$this->file_data; $data = &$this->file_data;
} }
else { else {
@@ -553,24 +553,24 @@ class parseCSV {
$to_end = true; $to_end = true;
// walk specific depth finding posssible delimiter characters // walk specific depth finding posssible delimiter characters
for ( $i=0; $i < $strlen; $i++ ) { for ($i=0; $i < $strlen; $i++) {
$ch = $data{$i}; $ch = $data{$i};
$nch = ( isset($data{$i+1}) ) ? $data{$i+1} : false ; $nch = (isset($data{$i+1})) ? $data{$i+1} : false ;
$pch = ( isset($data{$i-1}) ) ? $data{$i-1} : false ; $pch = (isset($data{$i-1})) ? $data{$i-1} : false ;
// open and closing quotes // open and closing quotes
if ( $ch == $enclosure ) { if ($ch == $enclosure) {
if ( !$enclosed || $nch != $enclosure ) { if (!$enclosed || $nch != $enclosure) {
$enclosed = ( $enclosed ) ? false : true ; $enclosed = ($enclosed) ? false : true ;
} }
elseif ( $enclosed ) { elseif ($enclosed) {
$i++; $i++;
} }
// end of row // end of row
} }
elseif ( ($ch == "\n" && $pch != "\r" || $ch == "\r") && !$enclosed ) { elseif (($ch == "\n" && $pch != "\r" || $ch == "\r") && !$enclosed) {
if ( $n >= $search_depth ) { if ($n >= $search_depth) {
$strlen = 0; $strlen = 0;
$to_end = false; $to_end = false;
} }
@@ -581,8 +581,8 @@ class parseCSV {
// count character // count character
} }
elseif (!$enclosed) { elseif (!$enclosed) {
if ( !preg_match('/['.preg_quote($this->auto_non_chars, '/').']/i', $ch) ) { if (!preg_match('/['.preg_quote($this->auto_non_chars, '/').']/i', $ch)) {
if ( !isset($chars[$ch][$n]) ) { if (!isset($chars[$ch][$n])) {
$chars[$ch][$n] = 1; $chars[$ch][$n] = 1;
} }
else { else {
@@ -593,10 +593,10 @@ class parseCSV {
} }
// filtering // filtering
$depth = ( $to_end ) ? $n-1 : $n ; $depth = ($to_end) ? $n-1 : $n;
$filtered = array(); $filtered = array();
foreach( $chars as $char => $value ) { foreach ($chars as $char => $value) {
if ( $match = $this->_check_count($char, $value, $depth, $preferred) ) { if ($match = $this->_check_count($char, $value, $depth, $preferred)) {
$filtered[$match] = $char; $filtered[$match] = $char;
} }
} }
@@ -606,7 +606,7 @@ class parseCSV {
$this->delimiter = reset($filtered); $this->delimiter = reset($filtered);
// parse data // parse data
if ( $parse ) { if ($parse) {
$this->data = $this->parse_string(); $this->data = $this->parse_string();
} }
@@ -629,15 +629,15 @@ class parseCSV {
* @return [array|bool] * @return [array|bool]
*/ */
public function parse_file ($file = null) { public function parse_file ($file = null) {
if ( $file === null ) { if (is_null($file)) {
$file = $this->file; $file = $this->file;
} }
if ( empty($this->file_data) ) { if (empty($this->file_data)) {
$this->load_data($file); $this->load_data($file);
} }
return ( !empty($this->file_data) ) ? $this->parse_string() : false ; return (!empty($this->file_data)) ? $this->parse_string() : false;
} }
/** /**
@@ -649,8 +649,8 @@ class parseCSV {
* @return 2D array with CSV data, or false on failure * @return 2D array with CSV data, or false on failure
*/ */
public function parse_string ($data = null) { public function parse_string ($data = null) {
if ( empty($data) ) { if (empty($data)) {
if ( $this->_check_data() ) { if ($this->_check_data()) {
$data = &$this->file_data; $data = &$this->file_data;
} }
else { else {
@@ -664,7 +664,7 @@ class parseCSV {
$row = array(); $row = array();
$row_count = 0; $row_count = 0;
$current = ''; $current = '';
$head = ( !empty($this->fields) ) ? $this->fields : array() ; $head = (!empty($this->fields)) ? $this->fields : array();
$col = 0; $col = 0;
$enclosed = false; $enclosed = false;
$was_enclosed = false; $was_enclosed = false;
@@ -678,23 +678,23 @@ class parseCSV {
} }
// walk through each character // walk through each character
for ( $i=0; $i < $strlen; $i++ ) { for ($i=0; $i < $strlen; $i++) {
$ch = ( isset($data{$i}) ) ? $data{$i} : false ; $ch = (isset($data{$i})) ? $data{$i} : false;
$nch = ( isset($data{$i+1}) ) ? $data{$i+1} : false ; $nch = (isset($data{$i+1})) ? $data{$i+1} : false;
$pch = ( isset($data{$i-1}) ) ? $data{$i-1} : false ; $pch = (isset($data{$i-1})) ? $data{$i-1} : false;
// open/close quotes, and inline quotes // open/close quotes, and inline quotes
if ( $ch == $this->enclosure ) { if ($ch == $this->enclosure) {
if ( !$enclosed ) { if (!$enclosed) {
if ( ltrim($current,$white_spaces) == '' ) { if (ltrim($current,$white_spaces) == '') {
$enclosed = true; $enclosed = true;
$was_enclosed = true; $was_enclosed = true;
} }
else { else {
$this->error = 2; $this->error = 2;
$error_row = count($rows) + 1; $error_row = count($rows) + 1;
$error_col = $col + 1; $error_col = $col + 1;
if ( !isset($this->error_info[$error_row.'-'.$error_col]) ) { if (!isset($this->error_info[$error_row.'-'.$error_col])) {
$this->error_info[$error_row.'-'.$error_col] = array( $this->error_info[$error_row.'-'.$error_col] = array(
'type' => 2, 'type' => 2,
'info' => 'Syntax error found on row '.$error_row.'. Non-enclosed fields can not contain double-quotes.', 'info' => 'Syntax error found on row '.$error_row.'. Non-enclosed fields can not contain double-quotes.',
@@ -711,20 +711,20 @@ class parseCSV {
$current .= $ch; $current .= $ch;
$i++; $i++;
} }
elseif ( $nch != $this->delimiter && $nch != "\r" && $nch != "\n" ) { elseif ($nch != $this->delimiter && $nch != "\r" && $nch != "\n") {
for ( $x=($i+1); isset($data{$x}) && ltrim($data{$x}, $white_spaces) == ''; $x++ ) {} for ($x=($i+1); isset($data{$x}) && ltrim($data{$x}, $white_spaces) == ''; $x++) {}
if ( $data{$x} == $this->delimiter ) { if ($data{$x} == $this->delimiter) {
$enclosed = false; $enclosed = false;
$i = $x; $i = $x;
} }
else { else {
if ( $this->error < 1 ) { if ($this->error < 1) {
$this->error = 1; $this->error = 1;
} }
$error_row = count($rows) + 1; $error_row = count($rows) + 1;
$error_col = $col + 1; $error_col = $col + 1;
if ( !isset($this->error_info[$error_row.'-'.$error_col]) ) { if (!isset($this->error_info[$error_row.'-'.$error_col])) {
$this->error_info[$error_row.'-'.$error_col] = array( $this->error_info[$error_row.'-'.$error_col] = array(
'type' => 1, 'type' => 1,
'info' => 'info' =>
@@ -748,24 +748,24 @@ class parseCSV {
// end of field/row/csv // end of field/row/csv
} }
elseif ( ($ch == $this->delimiter || $ch == "\n" || $ch == "\r" || $ch === false) && !$enclosed ) { elseif ( ($ch == $this->delimiter || $ch == "\n" || $ch == "\r" || $ch === false) && !$enclosed ) {
$key = ( !empty($head[$col]) ) ? $head[$col] : $col ; $key = (!empty($head[$col])) ? $head[$col] : $col;
$row[$key] = ( $was_enclosed ) ? $current : trim($current) ; $row[$key] = ($was_enclosed) ? $current : trim($current);
$current = ''; $current = '';
$was_enclosed = false; $was_enclosed = false;
$col++; $col++;
// end of row // end of row
if ( $ch == "\n" || $ch == "\r" || $ch === false ) { if ($ch == "\n" || $ch == "\r" || $ch === false) {
if ( $this->_validate_offset($row_count) && $this->_validate_row_conditions($row, $this->conditions) ) { if ($this->_validate_offset($row_count) && $this->_validate_row_conditions($row, $this->conditions)) {
if ( $this->heading && empty($head) ) { if ($this->heading && empty($head)) {
$head = $row; $head = $row;
} }
elseif ( empty($this->fields) || (!empty($this->fields) && (($this->heading && $row_count > 0) || !$this->heading)) ) { elseif (empty($this->fields) || (!empty($this->fields) && (($this->heading && $row_count > 0) || !$this->heading))) {
if ( !empty($this->sort_by) && !empty($row[$this->sort_by]) ) { if (!empty($this->sort_by) && !empty($row[$this->sort_by])) {
if ( isset($rows[$row[$this->sort_by]]) ) { if (isset($rows[$row[$this->sort_by]])) {
$rows[$row[$this->sort_by].'_0'] = &$rows[$row[$this->sort_by]]; $rows[$row[$this->sort_by].'_0'] = &$rows[$row[$this->sort_by]];
unset($rows[$row[$this->sort_by]]); unset($rows[$row[$this->sort_by]]);
for ( $sn=1; isset($rows[$row[$this->sort_by].'_'.$sn]); $sn++ ) {} for ($sn=1; isset($rows[$row[$this->sort_by].'_'.$sn]); $sn++) {}
$rows[$row[$this->sort_by].'_'.$sn] = $row; $rows[$row[$this->sort_by].'_'.$sn] = $row;
} }
else $rows[$row[$this->sort_by]] = $row; else $rows[$row[$this->sort_by]] = $row;
@@ -780,11 +780,11 @@ class parseCSV {
$col = 0; $col = 0;
$row_count++; $row_count++;
if ( $this->sort_by === null && $this->limit !== null && count($rows) == $this->limit ) { if ($this->sort_by === null && $this->limit !== null && count($rows) == $this->limit) {
$i = $strlen; $i = $strlen;
} }
if ( $ch == "\r" && $nch == "\n" ) { if ($ch == "\r" && $nch == "\n") {
$i++; $i++;
} }
} }
@@ -797,23 +797,23 @@ class parseCSV {
} }
$this->titles = $head; $this->titles = $head;
if ( !empty($this->sort_by) ) { if (!empty($this->sort_by)) {
$sort_type = SORT_REGULAR; $sort_type = SORT_REGULAR;
if ( $this->sort_type == 'numeric' ) { if ($this->sort_type == 'numeric') {
$sort_type = SORT_NUMERIC; $sort_type = SORT_NUMERIC;
} }
elseif ( $this->sort_type == 'string' ) { elseif ($this->sort_type == 'string') {
$sort_type = SORT_STRING; $sort_type = SORT_STRING;
} }
( $this->sort_reverse ) ? krsort($rows, $sort_type) : ksort($rows, $sort_type) ; ($this->sort_reverse) ? krsort($rows, $sort_type) : ksort($rows, $sort_type);
if ( $this->offset !== null || $this->limit !== null ) { if ($this->offset !== null || $this->limit !== null) {
$rows = array_slice($rows, ($this->offset === null ? 0 : $this->offset) , $this->limit, true); $rows = array_slice($rows, ($this->offset === null ? 0 : $this->offset) , $this->limit, true);
} }
} }
if ( !$this->keep_file_data ) { if (!$this->keep_file_data) {
$this->file_data = null; $this->file_data = null;
} }
@@ -833,25 +833,25 @@ class parseCSV {
* *
* @return CSV data (text string) * @return CSV data (text string)
*/ */
public function unparse ( $data = array(), $fields = array(), $append = false , $is_php = false, $delimiter = null) { public function unparse ($data = array(), $fields = array(), $append = false , $is_php = false, $delimiter = null) {
if ( !is_array($data) || empty($data) ) { if (!is_array($data) || empty($data)) {
$data = &$this->data; $data = &$this->data;
} }
if ( !is_array($fields) || empty($fields) ) { if (!is_array($fields) || empty($fields)) {
$fields = &$this->titles; $fields = &$this->titles;
} }
if ( $delimiter === null ) { if ($delimiter === null) {
$delimiter = $this->delimiter; $delimiter = $this->delimiter;
} }
$string = ( $is_php ) ? "<?php header('Status: 403'); die(' '); ?>".$this->linefeed : '' ; $string = ($is_php) ? "<?php header('Status: 403'); die(' '); ?>".$this->linefeed : '';
$entry = array(); $entry = array();
// create heading // create heading
if ( $this->heading && !$append && !empty($fields) ) { if ($this->heading && !$append && !empty($fields)) {
foreach( $fields as $key => $value ) { foreach ($fields as $key => $value) {
$entry[] = $this->_enclose_value($value, $delimiter); $entry[] = $this->_enclose_value($value, $delimiter);
} }
@@ -860,8 +860,8 @@ class parseCSV {
} }
// create data // create data
foreach( $data as $key => $row ) { foreach ($data as $key => $row) {
foreach( $row as $field => $value ) { foreach ($row as $field => $value) {
$entry[] = $this->_enclose_value($value, $delimiter); $entry[] = $this->_enclose_value($value, $delimiter);
} }
@@ -884,30 +884,30 @@ class parseCSV {
$data = null; $data = null;
$file = null; $file = null;
if ( $input === null ) { if (is_null($input)) {
$file = $this->file; $file = $this->file;
} }
elseif ( file_exists($input) ) { elseif (file_exists($input)) {
$file = $input; $file = $input;
} }
else { else {
$data = $input; $data = $input;
} }
if ( !empty($data) || $data = $this->_rfile($file) ) { if (!empty($data) || $data = $this->_rfile($file)) {
if ( $this->file != $file ) { if ($this->file != $file) {
$this->file = $file; $this->file = $file;
} }
if ( preg_match('/\.php$/i', $file) && preg_match('/<\?.*?\?>(.*)/ims', $data, $strip) ) { if (preg_match('/\.php$/i', $file) && preg_match('/<\?.*?\?>(.*)/ims', $data, $strip)) {
$data = ltrim($strip[1]); $data = ltrim($strip[1]);
} }
if ( $this->convert_encoding ) { if ($this->convert_encoding) {
$data = iconv($this->input_encoding, $this->output_encoding, $data); $data = iconv($this->input_encoding, $this->output_encoding, $data);
} }
if ( substr($data, -1) != "\n" ) { if (substr($data, -1) != "\n") {
$data .= "\n"; $data .= "\n";
} }
@@ -933,27 +933,27 @@ class parseCSV {
* @return true of false * @return true of false
*/ */
protected function _validate_row_conditions ($row = array(), $conditions = null) { protected function _validate_row_conditions ($row = array(), $conditions = null) {
if ( !empty($row) ) { if (!empty($row)) {
if ( !empty($conditions) ) { if (!empty($conditions)) {
$conditions = (strpos($conditions, ' OR ') !== false) ? explode(' OR ', $conditions) : array($conditions) ; $conditions = (strpos($conditions, ' OR ') !== false) ? explode(' OR ', $conditions) : array($conditions);
$or = ''; $or = '';
foreach( $conditions as $key => $value ) { foreach ($conditions as $key => $value) {
if ( strpos($value, ' AND ') !== false ) { if (strpos($value, ' AND ') !== false) {
$value = explode(' AND ', $value); $value = explode(' AND ', $value);
$and = ''; $and = '';
foreach( $value as $k => $v ) { foreach ($value as $k => $v) {
$and .= $this->_validate_row_condition($row, $v); $and .= $this->_validate_row_condition($row, $v);
} }
$or .= (strpos($and, '0') !== false) ? '0' : '1' ; $or .= (strpos($and, '0') !== false) ? '0' : '1';
} }
else { else {
$or .= $this->_validate_row_condition($row, $value); $or .= $this->_validate_row_condition($row, $value);
} }
} }
return (strpos($or, '1') !== false) ? true : false ; return (strpos($or, '1') !== false) ? true : false;
} }
return true; return true;
@@ -985,19 +985,19 @@ class parseCSV {
$operators_regex = array(); $operators_regex = array();
foreach( $operators as $value ) { foreach ($operators as $value) {
$operators_regex[] = preg_quote($value, '/'); $operators_regex[] = preg_quote($value, '/');
} }
$operators_regex = implode('|', $operators_regex); $operators_regex = implode('|', $operators_regex);
if ( preg_match('/^(.+) ('.$operators_regex.') (.+)$/i', trim($condition), $capture) ) { if (preg_match('/^(.+) ('.$operators_regex.') (.+)$/i', trim($condition), $capture)) {
$field = $capture[1]; $field = $capture[1];
$op = $capture[2]; $op = $capture[2];
$value = $capture[3]; $value = $capture[3];
if ( preg_match('/^([\'\"]{1})(.*)([\'\"]{1})$/i', $value, $capture) ) { if (preg_match('/^([\'\"]{1})(.*)([\'\"]{1})$/i', $value, $capture)) {
if ( $capture[1] == $capture[3] ) { if ($capture[1] == $capture[3]) {
$value = $capture[2]; $value = $capture[2];
$value = str_replace("\\n", "\n", $value); $value = str_replace("\\n", "\n", $value);
$value = str_replace("\\r", "\r", $value); $value = str_replace("\\r", "\r", $value);
@@ -1006,29 +1006,29 @@ class parseCSV {
} }
} }
if ( array_key_exists($field, $row) ) { if (array_key_exists($field, $row)) {
if ( ($op == '=' || $op == 'equals' || $op == 'is') && $row[$field] == $value ) { if (($op == '=' || $op == 'equals' || $op == 'is') && $row[$field] == $value) {
return '1'; return '1';
} }
elseif ( ($op == '!=' || $op == 'is not') && $row[$field] != $value ) { elseif (($op == '!=' || $op == 'is not') && $row[$field] != $value) {
return '1'; return '1';
} }
elseif ( ($op == '<' || $op == 'is less than' ) && $row[$field] < $value ) { elseif (($op == '<' || $op == 'is less than' ) && $row[$field] < $value) {
return '1'; return '1';
} }
elseif ( ($op == '>' || $op == 'is greater than') && $row[$field] > $value ) { elseif (($op == '>' || $op == 'is greater than') && $row[$field] > $value) {
return '1'; return '1';
} }
elseif ( ($op == '<=' || $op == 'is less than or equals' ) && $row[$field] <= $value ) { elseif (($op == '<=' || $op == 'is less than or equals' ) && $row[$field] <= $value) {
return '1'; return '1';
} }
elseif ( ($op == '>=' || $op == 'is greater than or equals') && $row[$field] >= $value ) { elseif (($op == '>=' || $op == 'is greater than or equals') && $row[$field] >= $value) {
return '1'; return '1';
} }
elseif ( $op == 'contains' && preg_match('/'.preg_quote($value, '/').'/i', $row[$field]) ) { elseif ($op == 'contains' && preg_match('/'.preg_quote($value, '/').'/i', $row[$field])) {
return '1'; return '1';
} }
elseif ( $op == 'does not contain' && !preg_match('/'.preg_quote($value, '/').'/i', $row[$field]) ) { elseif ($op == 'does not contain' && !preg_match('/'.preg_quote($value, '/').'/i', $row[$field])) {
return '1'; return '1';
} }
else { else {
@@ -1049,7 +1049,7 @@ class parseCSV {
* @return true of false * @return true of false
*/ */
protected function _validate_offset ($current_row) { protected function _validate_offset ($current_row) {
if ( $this->sort_by === null && $this->offset !== null && $current_row < $this->offset ) { if ($this->sort_by === null && $this->offset !== null && $current_row < $this->offset) {
return false; return false;
} }
@@ -1066,13 +1066,13 @@ class parseCSV {
* @return Processed value * @return Processed value
*/ */
protected function _enclose_value ($value = null, $delimiter = null) { protected function _enclose_value ($value = null, $delimiter = null) {
if ( $delimiter === null ) { if (is_null($delimiter)) {
$delimiter = $this->delimiter; $delimiter = $this->delimiter;
} }
if ( $value !== null && $value != '' ) { if ($value !== null && $value != '') {
$delimiter_quoted = preg_quote($delimiter, '/'); $delimiter_quoted = preg_quote($delimiter, '/');
$enclosure_quoted = preg_quote($this->enclosure, '/'); $enclosure_quoted = preg_quote($this->enclosure, '/');
if ( preg_match("/".$delimiter_quoted."|".$enclosure_quoted."|\n|\r/i", $value) || ($value{0} == ' ' || substr($value, -1) == ' ') || $this->enclose_all ) { if (preg_match("/".$delimiter_quoted."|".$enclosure_quoted."|\n|\r/i", $value) || ($value{0} == ' ' || substr($value, -1) == ' ') || $this->enclose_all) {
$value = str_replace($this->enclosure, $this->enclosure.$this->enclosure, $value); $value = str_replace($this->enclosure, $this->enclosure.$this->enclosure, $value);
$value = $this->enclosure.$value.$this->enclosure; $value = $this->enclosure.$value.$this->enclosure;
} }
@@ -1090,8 +1090,8 @@ class parseCSV {
* @return true or false * @return true or false
*/ */
protected function _check_data ($file = null) { protected function _check_data ($file = null) {
if ( empty($this->file_data) ) { if (empty($this->file_data)) {
if ( $file === null ) $file = $this->file; if (is_null($file)) $file = $this->file;
return $this->load_data($file); return $this->load_data($file);
} }
@@ -1112,19 +1112,19 @@ class parseCSV {
* @return special string used for delimiter selection, or false * @return special string used for delimiter selection, or false
*/ */
protected function _check_count ($char, $array, $depth, $preferred) { protected function _check_count ($char, $array, $depth, $preferred) {
if ( $depth == count($array) ) { if ($depth == count($array)) {
$first = null; $first = null;
$equal = null; $equal = null;
$almost = false; $almost = false;
foreach( $array as $key => $value ) { foreach ($array as $key => $value) {
if ( $first == null ) { if ($first == null) {
$first = $value; $first = $value;
} }
elseif ( $value == $first && $equal !== false) { elseif ($value == $first && $equal !== false) {
$equal = true; $equal = true;
} }
elseif ( $value == $first+1 && $equal !== false ) { elseif ($value == $first+1 && $equal !== false) {
$equal = true; $equal = true;
$almost = true; $almost = true;
} }
else { else {
@@ -1132,10 +1132,10 @@ class parseCSV {
} }
} }
if ( $equal ) { if ($equal) {
$match = ( $almost ) ? 2 : 1 ; $match = ($almost) ? 2 : 1;
$pref = strpos($preferred, $char); $pref = strpos($preferred, $char);
$pref = ( $pref !== false ) ? str_pad($pref, 3, '0', STR_PAD_LEFT) : '999' ; $pref = ($pref !== false) ? str_pad($pref, 3, '0', STR_PAD_LEFT) : '999';
return $pref.$match.'.'.(99999 - str_pad($first, 5, '0', STR_PAD_LEFT)); return $pref.$match.'.'.(99999 - str_pad($first, 5, '0', STR_PAD_LEFT));
} }
@@ -1154,8 +1154,8 @@ class parseCSV {
* @return Data from file, or false on failure * @return Data from file, or false on failure
*/ */
protected function _rfile ($file = null) { protected function _rfile ($file = null) {
if ( is_readable($file) ) { if (is_readable($file)) {
if ( !($fh = fopen($file, 'r')) ) { if (!($fh = fopen($file, 'r'))) {
return false; return false;
} }
@@ -1179,11 +1179,11 @@ class parseCSV {
* @return true or false * @return true or false
*/ */
protected function _wfile ($file, $string = '', $mode = 'wb', $lock = 2) { protected function _wfile ($file, $string = '', $mode = 'wb', $lock = 2) {
if ( $fp = fopen($file, $mode) ) { if ($fp = fopen($file, $mode)) {
flock($fp, $lock); flock($fp, $lock);
$re = fwrite($fp, $string); $re = fwrite($fp, $string);
$re2 = fclose($fp); $re2 = fclose($fp);
if ( $re != false && $re2 != false ) { if ($re != false && $re2 != false) {
return true; return true;
} }
} }