From 301f6da3e6efb50727d0a95fb97b285ee8a7398f Mon Sep 17 00:00:00 2001 From: Susann Sgorzaly Date: Tue, 13 Feb 2018 17:18:01 +0100 Subject: [PATCH] init new functionality to identify datatypes of columns --- parsecsv.lib.php | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/parsecsv.lib.php b/parsecsv.lib.php index beffd1e..7ade228 100644 --- a/parsecsv.lib.php +++ b/parsecsv.lib.php @@ -322,6 +322,15 @@ class parseCSV { */ public $data = array(); + /** + * Datatypes + * Datatypes of CSV data-columns + * + * @access public + * @var array + */ + public $data_types = array(); + /** * Constructor * Class constructor @@ -535,6 +544,90 @@ class parseCSV { 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 ] --------------------- // ==============================================