mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 00:36:38 +00:00
init enum for data type
This commit is contained in:
113
src/enums/DatatypeEnum.php
Normal file
113
src/enums/DatatypeEnum.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
namespace CSV\enums;
|
||||
|
||||
/**
|
||||
* Class DatatypeEnum
|
||||
*
|
||||
* @package CSV\enums
|
||||
*/
|
||||
class DatatypeEnum extends SplEnum {
|
||||
|
||||
const __DEFAULT = self::TYPE_STRING;
|
||||
|
||||
const TYPE_STRING = 'string';
|
||||
|
||||
const TYPE_FLOAT = 'float';
|
||||
|
||||
const TYPE_INT = 'integer';
|
||||
|
||||
const TYPE_BOOL = 'boolean';
|
||||
|
||||
const TYPE_DATE = 'date';
|
||||
|
||||
const REGEX_FLOAT = '/^[+-]?([0-9]*[.,])?([0-9]|[.,][0-9])+$/';
|
||||
|
||||
const REGEX_INT = '/^[-+]?[0-9]\d*$/';
|
||||
|
||||
const REGEX_BOOL = '/^(?i:true|false)$/';
|
||||
|
||||
/**
|
||||
* Define validator functions here.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $validators = array(
|
||||
self::TYPE_STRING => null,
|
||||
self::TYPE_FLOAT => 'isValidFloat',
|
||||
self::TYPE_INT => 'isValidInteger',
|
||||
self::TYPE_BOOL => 'isValidBoolean',
|
||||
self::TYPE_DATE => 'isValidDate'
|
||||
);
|
||||
|
||||
/**
|
||||
* Checks data type for given string.
|
||||
*
|
||||
* @param $value
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
public static function getValidTypeFromSample($value){
|
||||
$value = trim((string) $value);
|
||||
|
||||
if (empty($value)){
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (self::$validators as $type => $validator){
|
||||
if ($validator === null){
|
||||
continue;
|
||||
}
|
||||
|
||||
if (method_exists(self, $validator)){
|
||||
call_user_func($validator($value));
|
||||
return $type;
|
||||
}
|
||||
|
||||
return self::__DEFAULT;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is float value.
|
||||
*
|
||||
* @param $value
|
||||
*
|
||||
* @return false|int
|
||||
*/
|
||||
private static function isValidFloat($value) {
|
||||
return preg_match(self::REGEX_FLOAT, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is integer value.
|
||||
*
|
||||
* @param $value
|
||||
*
|
||||
* @return false|int
|
||||
*/
|
||||
private static function isValidInteger($value) {
|
||||
return preg_match(self::REGEX_INT, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is boolean.
|
||||
*
|
||||
* @param $value
|
||||
*
|
||||
* @return false|int
|
||||
*/
|
||||
private static function isValidBoolean($value) {
|
||||
return preg_match(self::REGEX_BOOL, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is date.
|
||||
*
|
||||
* @param $value
|
||||
*
|
||||
* @return false|int
|
||||
*/
|
||||
private static function isValidDate($value) {
|
||||
return (bool) strtotime($value);
|
||||
}
|
||||
}
|
||||
@@ -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])+$/', $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,7 +23,7 @@ trait DatatypeTrait {
|
||||
* @return string|false
|
||||
*/
|
||||
private function getMostFrequentDataypeForColumn($datatypes) {
|
||||
unset($datatypes['unknown']);
|
||||
array_filter($datatypes);
|
||||
|
||||
$typesFreq = array_count_values($datatypes);
|
||||
arsort($typesFreq);
|
||||
@@ -85,13 +49,14 @@ trait DatatypeTrait {
|
||||
$result = [];
|
||||
foreach ($this->titles as $cName) {
|
||||
$column = array_column($this->data, $cName);
|
||||
$cDatatypes = array_map([$this, 'getDatatypeFromString'], $column);
|
||||
|
||||
$cDatatypes = array_map('CSV\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