mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
Merge branch 'introduce-namespaces' into test-helper-for-itexia-with-namespaces
This commit is contained in:
@@ -12,42 +12,6 @@ trait DatatypeTrait {
|
||||
*/
|
||||
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]*(e[+-]?[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.
|
||||
@@ -59,9 +23,15 @@ trait DatatypeTrait {
|
||||
* @return string|false
|
||||
*/
|
||||
private function getMostFrequentDataypeForColumn($datatypes) {
|
||||
unset($datatypes['unknown']);
|
||||
array_filter($datatypes);
|
||||
|
||||
$typesFreq = array_count_values($datatypes);
|
||||
// workaround because array_count_values($datatypes) does not work anymore :-(
|
||||
foreach ($datatypes as $type) {
|
||||
$ids = array_keys($datatypes, $type);
|
||||
$typesFreq[$type] = count($ids);
|
||||
|
||||
$datatypes = array_diff_key($datatypes, array_flip($ids));
|
||||
}
|
||||
arsort($typesFreq);
|
||||
reset($typesFreq);
|
||||
|
||||
@@ -90,13 +60,14 @@ trait DatatypeTrait {
|
||||
$result = [];
|
||||
foreach ($this->titles as $cName) {
|
||||
$column = array_column($this->data, $cName);
|
||||
$cDatatypes = array_map([$this, 'getDatatypeFromString'], $column);
|
||||
|
||||
$cDatatypes = array_map('ParseCsv\enums\DatatypeEnum::getValidTypeFromSample', $column);
|
||||
|
||||
$result[$cName] = $this->getMostFrequentDataypeForColumn($cDatatypes);
|
||||
}
|
||||
|
||||
$this->data_types = $result;
|
||||
|
||||
return !empty($this->data_types) ? $this->data_types : false;
|
||||
return !empty($this->data_types) ? $this->data_types : [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user