corrected datatype count

This commit is contained in:
Susann Sgorzaly
2018-02-19 12:02:48 +01:00
parent 48cec20f4f
commit 52e87953a4
2 changed files with 33 additions and 19 deletions

View File

@@ -5,8 +5,11 @@ namespace ParseCsv\enums;
* Class DatatypeEnum
*
* @package ParseCsv\enums
*
* todo: needs a basic parent enum class for error handling.
*/
class DatatypeEnum extends SplEnum {
class DatatypeEnum
{
const __DEFAULT = self::TYPE_STRING;
@@ -33,16 +36,16 @@ class DatatypeEnum extends SplEnum {
*/
private static $validators = array(
self::TYPE_STRING => null,
self::TYPE_FLOAT => 'isValidFloat',
self::TYPE_INT => 'isValidInteger',
self::TYPE_BOOL => 'isValidBoolean',
self::TYPE_FLOAT => 'isValidFloat',
self::TYPE_DATE => 'isValidDate'
);
/**
* Checks data type for given string.
*
* @param $value
* @param string $value
*
* @return bool|string
*/
@@ -58,9 +61,10 @@ class DatatypeEnum extends SplEnum {
continue;
}
if (method_exists(self, $validator)){
call_user_func($validator($value));
return $type;
if (method_exists(__CLASS__, $validator)){
if (get_class()::$validator($value)) {
return $type;
}
}
return self::__DEFAULT;
@@ -70,42 +74,42 @@ class DatatypeEnum extends SplEnum {
/**
* Check if string is float value.
*
* @param $value
* @param string $value
*
* @return false|int
* @return bool
*/
private static function isValidFloat($value) {
return preg_match(self::REGEX_FLOAT, $value);
return (bool) preg_match(self::REGEX_FLOAT, $value);
}
/**
* Check if string is integer value.
*
* @param $value
* @param string $value
*
* @return false|int
* @return bool
*/
private static function isValidInteger($value) {
return preg_match(self::REGEX_INT, $value);
return (bool) preg_match(self::REGEX_INT, $value);
}
/**
* Check if string is boolean.
*
* @param $value
* @param string $value
*
* @return false|int
* @return bool
*/
private static function isValidBoolean($value) {
return preg_match(self::REGEX_BOOL, $value);
return (bool) preg_match(self::REGEX_BOOL, $value);
}
/**
* Check if string is date.
*
* @param $value
* @param string $value
*
* @return false|int
* @return bool
*/
private static function isValidDate($value) {
return (bool) strtotime($value);

View File

@@ -25,7 +25,17 @@ trait DatatypeTrait {
private function getMostFrequentDataypeForColumn($datatypes) {
array_filter($datatypes);
$typesFreq = array_count_values($datatypes);
foreach ($datatypes as $value) {
echo gettype($value), "\n";
}
// 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);
@@ -50,7 +60,7 @@ trait DatatypeTrait {
foreach ($this->titles as $cName) {
$column = array_column($this->data, $cName);
$cDatatypes = array_map('CSV\enums\DatatypeEnum::getValidTypeFromSample', $column);
$cDatatypes = array_map('ParseCsv\enums\DatatypeEnum::getValidTypeFromSample', $column);
$result[$cName] = $this->getMostFrequentDataypeForColumn($cDatatypes);
}