mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
init new functionality to identify datatypes of columns
This commit is contained in:
@@ -322,6 +322,15 @@ class parseCSV {
|
|||||||
*/
|
*/
|
||||||
public $data = array();
|
public $data = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Datatypes
|
||||||
|
* Datatypes of CSV data-columns
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $data_types = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* Class constructor
|
* Class constructor
|
||||||
@@ -535,6 +544,90 @@ class parseCSV {
|
|||||||
return $this->delimiter;
|
return $this->delimiter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check data type
|
||||||
|
* Check for possible data types for one field value string.
|
||||||
|
*
|
||||||
|
* @access private
|
||||||
|
*
|
||||||
|
* @param [string] $value cell value
|
||||||
|
*
|
||||||
|
* @return [string]
|
||||||
|
*/
|
||||||
|
private function getDatatypeFromString($value){
|
||||||
|
$value = trim((string) $value);
|
||||||
|
|
||||||
|
if (empty($value)){
|
||||||
|
return 'unknown';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match('/^(?i:true|false)$/', $value)){
|
||||||
|
return 'boolean';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match('/^[-+]?[0-9]\d*$/', $value)){
|
||||||
|
return 'integer';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match('/^[+-]?([0-9]*[.])?([0-9]|[.][0-9])+$/', $value)){
|
||||||
|
return 'float';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((bool)strtotime($value)){
|
||||||
|
return 'date';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'string';
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Check data type for one column.
|
||||||
|
* Check for most commonly data type for one column.
|
||||||
|
*
|
||||||
|
* @access private
|
||||||
|
*
|
||||||
|
* @param [array] $datatypes
|
||||||
|
*
|
||||||
|
* @return [string|false]
|
||||||
|
*/
|
||||||
|
private function getMostCommonlyDataypeForColumn($datatypes){
|
||||||
|
if (isset($datatypes['unknown'])){
|
||||||
|
unset($datatypes['unknown']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$typesFreq = array_count_values($datatypes);
|
||||||
|
arsort($typesFreq);
|
||||||
|
reset($typesFreq);
|
||||||
|
|
||||||
|
return key($typesFreq);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check data type foreach Column
|
||||||
|
* Check data type for each column and returns the most commonly.
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*
|
||||||
|
* @return [array|bool]
|
||||||
|
*/
|
||||||
|
public function getDatatypes(){
|
||||||
|
if (empty($this->data)){
|
||||||
|
$this->data = $this->parse_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = [];
|
||||||
|
foreach ($this->titles as $cName){
|
||||||
|
$column = array_column($this->data, $cName);
|
||||||
|
$cDatatypes = array_map(array($this,'getDatatypeFromString'), $column);
|
||||||
|
|
||||||
|
$result[$cName] = $this->getMostCommonlyDataypeForColumn($cDatatypes);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->data_types = $result;
|
||||||
|
|
||||||
|
return !empty($this->data_types) ? $this->data_types : false;
|
||||||
|
}
|
||||||
|
|
||||||
// ==============================================
|
// ==============================================
|
||||||
// ----- [ Core Functions ] ---------------------
|
// ----- [ Core Functions ] ---------------------
|
||||||
// ==============================================
|
// ==============================================
|
||||||
|
|||||||
Reference in New Issue
Block a user