mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
exclude datatype detection in its own trait
This commit is contained in:
@@ -14,7 +14,8 @@
|
|||||||
],
|
],
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"classmap": [
|
"classmap": [
|
||||||
"."
|
".",
|
||||||
|
"./extensions"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
|||||||
98
extensions/Datatype.php
Normal file
98
extensions/Datatype.php
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
trait Datatype {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Datatypes
|
||||||
|
* Datatypes of CSV data-columns
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $data_types = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 getMostFrequentDataypeForColumn($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([$this, 'getDatatypeFromString'], $column);
|
||||||
|
|
||||||
|
$result[$cName] = $this->getMostFrequentDataypeForColumn($cDatatypes);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->data_types = $result;
|
||||||
|
|
||||||
|
return !empty($this->data_types) ? $this->data_types : FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -322,14 +322,7 @@ class parseCSV {
|
|||||||
*/
|
*/
|
||||||
public $data = array();
|
public $data = array();
|
||||||
|
|
||||||
/**
|
use Datatype;
|
||||||
* Datatypes
|
|
||||||
* Datatypes of CSV data-columns
|
|
||||||
*
|
|
||||||
* @access public
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
public $data_types = array();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
@@ -544,90 +537,6 @@ 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