updated properties to "public"

removed "var" and set to "public"
This commit is contained in:
William Knauss
2014-05-09 21:30:47 -04:00
parent 0323d5108b
commit ba1d462044

View File

@@ -1,26 +1,26 @@
<?php <?php
class parseCSV { class parseCSV {
/* /*
Class: parseCSV v0.4.3 beta Class: parseCSV v0.4.3 beta
https://github.com/jimeh/php-parsecsv https://github.com/jimeh/php-parsecsv
Fully conforms to the specifications lined out on wikipedia: Fully conforms to the specifications lined out on wikipedia:
- http://en.wikipedia.org/wiki/Comma-separated_values - http://en.wikipedia.org/wiki/Comma-separated_values
Based on the concept of Ming Hong Ng's CsvFileParser class: Based on the concept of Ming Hong Ng's CsvFileParser class:
- -
Copyright (c) 2007 Jim Myhrberg (jim@zydev.info). Copyright (c) 2007 Jim Myhrberg (jim@zydev.info).
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -40,9 +40,9 @@ class parseCSV {
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
Code Examples Code Examples
---------------- ----------------
# general usage # general usage
@@ -79,7 +79,7 @@ class parseCSV {
$csv = new parseCSV(); $csv = new parseCSV();
$csv->output (true, 'movies.csv', $array); $csv->output (true, 'movies.csv', $array);
---------------- ----------------
*/ */
@@ -88,71 +88,71 @@ class parseCSV {
* Configuration * Configuration
* - set these options with $object->var_name = 'value'; * - set these options with $object->var_name = 'value';
*/ */
# use first line/entry as field names # use first line/entry as field names
var $heading = true; public $heading = true;
# override field names # override field names
var $fields = array(); public $fields = array();
# sort entries by this field # sort entries by this field
var $sort_by = null; public $sort_by = null;
var $sort_reverse = false; public $sort_reverse = false;
# sort behavior passed to ksort/krsort functions # sort behavior passed to ksort/krsort functions
# regular = SORT_REGULAR # regular = SORT_REGULAR
# numeric = SORT_NUMERIC # numeric = SORT_NUMERIC
# string = SORT_STRING # string = SORT_STRING
var $sort_type = null; public $sort_type = null;
# delimiter (comma) and enclosure (double quote) # delimiter (comma) and enclosure (double quote)
var $delimiter = ','; public $delimiter = ',';
var $enclosure = '"'; public $enclosure = '"';
# basic SQL-like conditions for row matching # basic SQL-like conditions for row matching
var $conditions = null; public $conditions = null;
# number of rows to ignore from beginning of data # number of rows to ignore from beginning of data
var $offset = null; public $offset = null;
# limits the number of returned rows to specified amount # limits the number of returned rows to specified amount
var $limit = null; public $limit = null;
# number of rows to analyze when attempting to auto-detect delimiter # number of rows to analyze when attempting to auto-detect delimiter
var $auto_depth = 15; public $auto_depth = 15;
# characters to ignore when attempting to auto-detect delimiter # characters to ignore when attempting to auto-detect delimiter
var $auto_non_chars = "a-zA-Z0-9\n\r"; public $auto_non_chars = "a-zA-Z0-9\n\r";
# preferred delimiter characters, only used when all filtering method # preferred delimiter characters, only used when all filtering method
# returns multiple possible delimiters (happens very rarely) # returns multiple possible delimiters (happens very rarely)
var $auto_preferred = ",;\t.:|"; public $auto_preferred = ",;\t.:|";
# character encoding options # character encoding options
var $convert_encoding = false; public $convert_encoding = false;
var $input_encoding = 'ISO-8859-1'; public $input_encoding = 'ISO-8859-1';
var $output_encoding = 'ISO-8859-1'; public $output_encoding = 'ISO-8859-1';
# used by unparse(), save(), and output() functions # used by unparse(), save(), and output() functions
var $linefeed = "\r\n"; public $linefeed = "\r\n";
# only used by output() function # only used by output() function
var $output_delimiter = ','; public $output_delimiter = ',';
var $output_filename = 'data.csv'; public $output_filename = 'data.csv';
# keep raw file data in memory after successful parsing (useful for debugging) # keep raw file data in memory after successful parsing (useful for debugging)
var $keep_file_data = false; public $keep_file_data = false;
/** /**
* Internal variables * Internal variables
*/ */
# current file # current file
var $file; public $file;
# loaded file contents # loaded file contents
var $file_data; public $file_data;
# error while parsing input data # error while parsing input data
# 0 = No errors found. Everything should be fine :) # 0 = No errors found. Everything should be fine :)
# 1 = Hopefully correctable syntax error was found. # 1 = Hopefully correctable syntax error was found.
@@ -161,18 +161,18 @@ class parseCSV {
# the file is either corrupt, or does not # the file is either corrupt, or does not
# standard CSV formatting. Please validate # standard CSV formatting. Please validate
# the parsed data yourself. # the parsed data yourself.
var $error = 0; public $error = 0;
# detailed error info # detailed error info
var $error_info = array(); public $error_info = array();
# array of field values in data parsed # array of field values in data parsed
var $titles = array(); public $titles = array();
# two dimentional array of CSV data # two dimentional array of CSV data
var $data = array(); public $data = array();
/** /**
* Constructor * Constructor
* Class constructor * Class constructor
@@ -189,24 +189,24 @@ class parseCSV {
} }
if ( $limit !== null ) { if ( $limit !== null ) {
$this->limit = $limit; $this->limit = $limit;
} }
if ( count($conditions) > 0 ) { if ( count($conditions) > 0 ) {
$this->conditions = $conditions; $this->conditions = $conditions;
} }
if ( !empty($input) ) { if ( !empty($input) ) {
$this->parse($input); $this->parse($input);
} }
} }
// ============================================== // ==============================================
// ----- [ Main Functions ] --------------------- // ----- [ Main Functions ] ---------------------
// ============================================== // ==============================================
/** /**
* Parse * Parse
* Parse a CSV file or string * Parse a CSV file or string
@@ -220,16 +220,16 @@ class parseCSV {
*/ */
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 ( $input === null ) {
$input = $this->file; $input = $this->file;
} }
if ( !empty($input) ) { if ( !empty($input) ) {
if ( $offset !== null ) { if ( $offset !== null ) {
$this->offset = $offset; $this->offset = $offset;
} }
if ($limit !== null ) { if ($limit !== null ) {
$this->limit = $limit; $this->limit = $limit;
} }
if ( count($conditions) > 0 ) { if ( count($conditions) > 0 ) {
@@ -238,20 +238,20 @@ class parseCSV {
if ( is_readable($input) ) { if ( is_readable($input) ) {
$this->data = $this->parse_file($input); $this->data = $this->parse_file($input);
} }
else { else {
$this->file_data = &$input; $this->file_data = &$input;
$this->data = $this->parse_string(); $this->data = $this->parse_string();
} }
if ( $this->data === false ) { if ( $this->data === false ) {
return false; return false;
} }
} }
return true; return true;
} }
/** /**
* Save * Save
* Save changes, or write a new file and/or data * Save changes, or write a new file and/or data
@@ -273,7 +273,7 @@ class parseCSV {
return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode); return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode);
} }
/** /**
* Output * Output
* Generate a CSV based string for output. * Generate a CSV based string for output.
@@ -304,7 +304,7 @@ class parseCSV {
return $data; return $data;
} }
/** /**
* Encoding * Encoding
* Convert character encoding * Convert character encoding
@@ -323,7 +323,7 @@ class parseCSV {
$this->output_encoding = $output; $this->output_encoding = $output;
} }
} }
/** /**
* Auto * Auto
* Auto-Detect Delimiter: Find delimiter by analyzing a specific number of * Auto-Detect Delimiter: Find delimiter by analyzing a specific number of
@@ -338,7 +338,7 @@ 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 ( $file === null ) {
$file = $this->file; $file = $this->file;
} }
@@ -350,69 +350,69 @@ class parseCSV {
if ( $enclosure === null ) { if ( $enclosure === null ) {
$enclosure = $this->enclosure; $enclosure = $this->enclosure;
} }
if ( $preferred === null ) { if ( $preferred === null ) {
$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 {
return false; return false;
} }
} }
else { else {
$data = &$this->file_data; $data = &$this->file_data;
} }
$chars = array(); $chars = array();
$strlen = strlen($data); $strlen = strlen($data);
$enclosed = false; $enclosed = false;
$n = 1; $n = 1;
$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;
} }
else { else {
$n++; $n++;
} }
// 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 {
$chars[$ch][$n]++; $chars[$ch][$n]++;
} }
} }
} }
} }
// filtering // filtering
$depth = ( $to_end ) ? $n-1 : $n ; $depth = ( $to_end ) ? $n-1 : $n ;
$filtered = array(); $filtered = array();
@@ -421,24 +421,24 @@ class parseCSV {
$filtered[$match] = $char; $filtered[$match] = $char;
} }
} }
// capture most probable delimiter // capture most probable delimiter
ksort($filtered); ksort($filtered);
$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();
} }
return $this->delimiter; return $this->delimiter;
} }
// ============================================== // ==============================================
// ----- [ Core Functions ] --------------------- // ----- [ Core Functions ] ---------------------
// ============================================== // ==============================================
/** /**
* Parse File * Parse File
* Read file to string and call parse_string() * Read file to string and call parse_string()
@@ -458,7 +458,7 @@ class parseCSV {
return ( !empty($this->file_data) ) ? $this->parse_string() : false ; return ( !empty($this->file_data) ) ? $this->parse_string() : false ;
} }
/** /**
* Parse CSV strings to arrays * Parse CSV strings to arrays
* @param data CSV string * @param data CSV string
@@ -470,9 +470,9 @@ class parseCSV {
$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;
@@ -482,13 +482,13 @@ class parseCSV {
$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 ) {
@@ -542,7 +542,7 @@ class parseCSV {
} else { } else {
$enclosed = false; $enclosed = false;
} }
// end of field/row // end of field/row
} elseif ( ($ch == $this->delimiter || $ch == "\n" || $ch == "\r") && !$enclosed ) { } elseif ( ($ch == $this->delimiter || $ch == "\n" || $ch == "\r") && !$enclosed ) {
$key = ( !empty($head[$col]) ) ? $head[$col] : $col ; $key = ( !empty($head[$col]) ) ? $head[$col] : $col ;
@@ -550,7 +550,7 @@ class parseCSV {
$current = ''; $current = '';
$was_enclosed = false; $was_enclosed = false;
$col++; $col++;
// end of row // end of row
if ( $ch == "\n" || $ch == "\r" ) { if ( $ch == "\n" || $ch == "\r" ) {
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) ) {
@@ -575,7 +575,7 @@ class parseCSV {
} }
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;
@@ -599,7 +599,7 @@ class parseCSV {
} }
return $rows; return $rows;
} }
/** /**
* Create CSV data from array * Create CSV data from array
* @param data 2D array with data * @param data 2D array with data
@@ -614,10 +614,10 @@ class parseCSV {
if ( !is_array($data) || empty($data) ) $data = &$this->data; if ( !is_array($data) || empty($data) ) $data = &$this->data;
if ( !is_array($fields) || empty($fields) ) $fields = &$this->titles; if ( !is_array($fields) || empty($fields) ) $fields = &$this->titles;
if ( $delimiter === null ) $delimiter = $this->delimiter; if ( $delimiter === null ) $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 ) {
@@ -626,7 +626,7 @@ class parseCSV {
$string .= implode($delimiter, $entry).$this->linefeed; $string .= implode($delimiter, $entry).$this->linefeed;
$entry = array(); $entry = array();
} }
// create data // create data
foreach( $data as $key => $row ) { foreach( $data as $key => $row ) {
foreach( $row as $field => $value ) { foreach( $row as $field => $value ) {
@@ -635,10 +635,10 @@ class parseCSV {
$string .= implode($delimiter, $entry).$this->linefeed; $string .= implode($delimiter, $entry).$this->linefeed;
$entry = array(); $entry = array();
} }
return $string; return $string;
} }
/** /**
* Load local file or string * Load local file or string
* @param input local CSV file * @param input local CSV file
@@ -666,16 +666,16 @@ class parseCSV {
} }
return false; return false;
} }
// ============================================== // ==============================================
// ----- [ Internal Functions ] ----------------- // ----- [ Internal Functions ] -----------------
// ============================================== // ==============================================
/** /**
* Validate a row against specified conditions * Validate a row against specified conditions
* @param row array with values from a row * @param row array with values from a row
* @param conditions specified conditions that the row must match * @param conditions specified conditions that the row must match
* @return true of false * @return true of false
*/ */
function _validate_row_conditions ($row = array(), $conditions = null) { function _validate_row_conditions ($row = array(), $conditions = null) {
@@ -701,11 +701,11 @@ class parseCSV {
} }
return false; return false;
} }
/** /**
* Validate a row against a single condition * Validate a row against a single condition
* @param row array with values from a row * @param row array with values from a row
* @param condition specified condition that the row must match * @param condition specified condition that the row must match
* @return true of false * @return true of false
*/ */
function _validate_row_condition ($row, $condition) { function _validate_row_condition ($row, $condition) {
@@ -761,7 +761,7 @@ class parseCSV {
} }
return '1'; return '1';
} }
/** /**
* Validates if the row is within the offset or not if sorting is disabled * Validates if the row is within the offset or not if sorting is disabled
* @param current_row the current row number being processed * @param current_row the current row number being processed
@@ -771,7 +771,7 @@ class parseCSV {
if ( $this->sort_by === null && $this->offset !== null && $current_row < $this->offset ) return false; if ( $this->sort_by === null && $this->offset !== null && $current_row < $this->offset ) return false;
return true; return true;
} }
/** /**
* Enclose values if needed * Enclose values if needed
* - only used by unparse() * - only used by unparse()
@@ -789,7 +789,7 @@ class parseCSV {
} }
return $value; return $value;
} }
/** /**
* Check file data * Check file data
* @param file local filename * @param file local filename
@@ -802,8 +802,8 @@ class parseCSV {
} }
return true; return true;
} }
/** /**
* Check if passed info might be delimiter * Check if passed info might be delimiter
* - only used by find_delimiter() * - only used by find_delimiter()
@@ -834,7 +834,7 @@ class parseCSV {
} else return false; } else return false;
} }
} }
/** /**
* Read local file * Read local file
* @param file local filename * @param file local filename
@@ -867,7 +867,7 @@ class parseCSV {
} }
return false; return false;
} }
} }
?> ?>