diff --git a/composer.json b/composer.json index 82a9d31..dc32b9e 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ ], "autoload": { "classmap": [ - "." + ".", + "./extensions" ] }, "require-dev": { diff --git a/extensions/Datatype.php b/extensions/Datatype.php new file mode 100644 index 0000000..e7a9f6a --- /dev/null +++ b/extensions/Datatype.php @@ -0,0 +1,98 @@ +data)) { + $this->data = $this->parse_string(); + } + + $result = []; + foreach ($this->titles as $cName) { + $column = array_column($this->data, $cName); + $cDatatypes = array_map([$this, 'getDatatypeFromString'], $column); + + $result[$cName] = $this->getMostFrequentDataypeForColumn($cDatatypes); + } + + $this->data_types = $result; + + return !empty($this->data_types) ? $this->data_types : FALSE; + } +} diff --git a/parsecsv.lib.php b/parsecsv.lib.php index 7ade228..cee9370 100644 --- a/parsecsv.lib.php +++ b/parsecsv.lib.php @@ -322,14 +322,7 @@ class parseCSV { */ public $data = array(); - /** - * Datatypes - * Datatypes of CSV data-columns - * - * @access public - * @var array - */ - public $data_types = array(); + use Datatype; /** * Constructor @@ -544,90 +537,6 @@ 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 ] --------------------- // ==============================================