parse_string readability

This commit is contained in:
William Knauss
2014-05-09 21:54:09 -04:00
parent 28967dea20
commit 8c300568bb

View File

@@ -617,64 +617,73 @@ class parseCSV {
* @param data CSV string * @param data CSV string
* @return 2D array with CSV data, or false on failure * @return 2D array with CSV data, or false on failure
*/ */
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 return false; }
else {
return false;
}
} }
$white_spaces = str_replace($this->delimiter, '', " \t\x0B\0"); $white_spaces = str_replace($this->delimiter, '', " \t\x0B\0");
$rows = array(); $rows = array();
$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;
$strlen = strlen($data); $strlen = strlen($data);
// walk through each character // walk through each character
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/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.',
'row' => $error_row, 'row' => $error_row,
'field' => $error_col, 'field' => $error_col,
'field_name' => (!empty($head[$col])) ? $head[$col] : null, 'field_name' => (!empty($head[$col])) ? $head[$col] : null,
); );
} }
$current .= $ch; $current .= $ch;
} }
} elseif ($nch == $this->enclosure) { }
elseif ($nch == $this->enclosure) {
$current .= $ch; $current .= $ch;
$i++; $i++;
} elseif ( $nch != $this->delimiter && $nch != "\r" && $nch != "\n" ) { }
for ( $x=($i+1); isset($data{$x}) && ltrim($data{$x}, $white_spaces) == ''; $x++ ) {} elseif ( $nch != $this->delimiter && $nch != "\r" && $nch != "\n" ) {
#for ( $x=($i+1); isset($data{$x}) && ltrim($data{$x}, $white_spaces) == ''; $x++ ) {} //this line does nothing?
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]) ) {
@@ -684,24 +693,27 @@ class parseCSV {
'Syntax error found on row '.(count($rows) + 1).'. '. 'Syntax error found on row '.(count($rows) + 1).'. '.
'A single double-quote was found within an enclosed string. '. 'A single double-quote was found within an enclosed string. '.
'Enclosed double-quotes must be escaped with a second double-quote.', 'Enclosed double-quotes must be escaped with a second double-quote.',
'row' => count($rows) + 1, 'row' => count($rows) + 1,
'field' => $col + 1, 'field' => $col + 1,
'field_name' => (!empty($head[$col])) ? $head[$col] : null, 'field_name' => (!empty($head[$col])) ? $head[$col] : null,
); );
} }
$current .= $ch; $current .= $ch;
$enclosed = false; $enclosed = false;
} }
} else { }
else {
$enclosed = false; $enclosed = false;
} }
// end of field/row // end of field/row
} elseif ( ($ch == $this->delimiter || $ch == "\n" || $ch == "\r") && !$enclosed ) { }
$key = ( !empty($head[$col]) ) ? $head[$col] : $col ; elseif ( ($ch == $this->delimiter || $ch == "\n" || $ch == "\r") && !$enclosed ) {
$row[$key] = ( $was_enclosed ) ? $current : trim($current) ; $key = ( !empty($head[$col]) ) ? $head[$col] : $col ;
$current = ''; $row[$key] = ( $was_enclosed ) ? $current : trim($current) ;
$was_enclosed = false; $current = '';
$was_enclosed = false;
$col++; $col++;
// end of row // end of row
@@ -709,47 +721,64 @@ class parseCSV {
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++ ) {} //this line does nothing
$rows[$row[$this->sort_by].'_'.$sn] = $row; $rows[$row[$this->sort_by].'_'.$sn] = $row;
} else $rows[$row[$this->sort_by]] = $row; }
} else $rows[] = $row; else $rows[$row[$this->sort_by]] = $row;
}
else {
$rows[] = $row;
}
} }
} }
$row = array(); $row = array();
$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" ) $i++;
if ( $ch == "\r" && $nch == "\n" ) {
$i++;
}
} }
// append character to current field // append character to current field
} else { }
else {
$current .= $ch; $current .= $ch;
} }
} }
$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;
} }
return $rows; return $rows;
} }