From a7a0bc34a43fd301b5ca4e798470a735a8a3521d Mon Sep 17 00:00:00 2001 From: Susann Sgorzaly Date: Sat, 17 Feb 2018 23:41:03 +0100 Subject: [PATCH 01/16] init introduce namespaces --- composer.json | 11 +++++---- parsecsv.lib.php => src/Csv.php | 5 +++- .../extensions}/DatatypeTrait.php | 1 + tests/Bootstrap.php | 16 +------------ tests/methods/ConstructTest.php | 23 +++++++++++-------- tests/methods/ParseTest.php | 15 ++++++++---- tests/methods/SaveTest.php | 11 ++++++--- 7 files changed, 44 insertions(+), 38 deletions(-) rename parsecsv.lib.php => src/Csv.php (99%) rename {extensions => src/extensions}/DatatypeTrait.php (98%) diff --git a/composer.json b/composer.json index dc32b9e..fa0286e 100644 --- a/composer.json +++ b/composer.json @@ -12,11 +12,12 @@ "email": "will.knauss@gmail.com" } ], - "autoload": { - "classmap": [ - ".", - "./extensions" - ] + "autoload":{ + "psr-4":{ + "CSV\\": "src", + "CSV\\extensions\\": "src\\extensions", + "CSV\\tests\\": "tests" + } }, "require-dev": { "phpunit/phpunit": "4.1.*" diff --git a/parsecsv.lib.php b/src/Csv.php similarity index 99% rename from parsecsv.lib.php rename to src/Csv.php index abf343e..7fe2fb0 100644 --- a/parsecsv.lib.php +++ b/src/Csv.php @@ -1,6 +1,9 @@ isFile() && $dir->getExtension() === 'php') { - require_once $dir->getPathname(); - } - } -} diff --git a/tests/methods/ConstructTest.php b/tests/methods/ConstructTest.php index 0a3c310..2ebf221 100644 --- a/tests/methods/ConstructTest.php +++ b/tests/methods/ConstructTest.php @@ -1,13 +1,18 @@ csv = new parseCSV(); + #$this->csv = new Csv(); } /** @@ -34,35 +39,35 @@ class ConstructTest extends PHPUnit\Framework\TestCase { public function test_offset_param() { $offset = 10; - $this->csv = new parseCSV(null, $offset); + $this->csv = new Csv(null, $offset); $this->assertTrue(is_numeric($this->csv->offset)); $this->assertEquals($offset, $this->csv->offset); } public function test_limit_param() { $limit = 10; - $this->csv = new parseCSV(null, null, $limit); + $this->csv = new Csv(null, null, $limit); $this->assertTrue(is_numeric($this->csv->limit)); $this->assertEquals($limit, $this->csv->limit); } public function test_conditions_param() { $conditions = 'some column NOT value'; - $this->csv = new parseCSV(null, null, null, $conditions); + $this->csv = new Csv(null, null, null, $conditions); $this->assertTrue(is_string($this->csv->conditions)); $this->assertEquals($conditions, $this->csv->conditions); } public function test_keep_file_data_param() { $keep = true; - $this->csv = new parseCSV(null, null, null, null, $keep); + $this->csv = new Csv(null, null, null, null, $keep); $this->assertTrue(is_bool($this->csv->keep_file_data)); $this->assertEquals($keep, $this->csv->keep_file_data); } public function test_input_param() { $csv = "col1,col2,col3\r\nval1,val2,val3\r\nval1A,val2A,val3A\r\n"; - $this->csv = new parseCSV($csv, null, null, null, true); + $this->csv = new Csv($csv, null, null, null, true); $this->assertTrue(is_string($this->csv->file_data)); $this->assertEquals($csv, $this->csv->file_data); } diff --git a/tests/methods/ParseTest.php b/tests/methods/ParseTest.php index 291f81e..e3c4ed0 100644 --- a/tests/methods/ParseTest.php +++ b/tests/methods/ParseTest.php @@ -1,13 +1,18 @@ csv = new parseCSV(); + $this->csv = new Csv(); } public function test_parse() { @@ -182,7 +187,7 @@ class ParseTest extends PHPUnit\Framework\TestCase { * @param string $enclosure */ public function testAutoQuotes($file, $enclosure) { - $csv = new parseCSV(); + $csv = new Csv(); $csv->auto($file, true, null, null, $enclosure); $this->assertArrayHasKey('column1', $csv->data[0], 'Data parsed incorrectly with enclosure ' . $enclosure); $this->assertEquals('value1', $csv->data[0]['column1'], 'Data parsed incorrectly with enclosure ' . $enclosure); diff --git a/tests/methods/SaveTest.php b/tests/methods/SaveTest.php index de66395..644ac01 100644 --- a/tests/methods/SaveTest.php +++ b/tests/methods/SaveTest.php @@ -1,8 +1,13 @@ csv = new parseCSV(); + $this->csv = new Csv(); $this->csv->auto(__DIR__ . '/../example_files/single_column.csv'); // Remove last 2 lines to simplify comparison From f6bd414ae7b5dc1904f5bea9aa2ec89ac052179c Mon Sep 17 00:00:00 2001 From: Susann Sgorzaly Date: Sun, 18 Feb 2018 00:09:22 +0100 Subject: [PATCH 02/16] init enum for data type --- src/enums/DatatypeEnum.php | 113 +++++++++++++++++++++++++++++++ src/extensions/DatatypeTrait.php | 43 ++---------- 2 files changed, 117 insertions(+), 39 deletions(-) create mode 100644 src/enums/DatatypeEnum.php diff --git a/src/enums/DatatypeEnum.php b/src/enums/DatatypeEnum.php new file mode 100644 index 0000000..83d64d1 --- /dev/null +++ b/src/enums/DatatypeEnum.php @@ -0,0 +1,113 @@ + 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); + } +} diff --git a/src/extensions/DatatypeTrait.php b/src/extensions/DatatypeTrait.php index e6b1246..aff0c9c 100644 --- a/src/extensions/DatatypeTrait.php +++ b/src/extensions/DatatypeTrait.php @@ -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 : []; } } From 17ac1dee1a54dba6ca165348ce7b48a2f387de2a Mon Sep 17 00:00:00 2001 From: Susann Sgorzaly Date: Sun, 18 Feb 2018 12:42:53 +0100 Subject: [PATCH 03/16] renamed namespace --- composer.json | 6 +++--- src/Csv.php | 4 ++-- src/extensions/DatatypeTrait.php | 2 +- tests/methods/ConstructTest.php | 4 ++-- tests/methods/ParseTest.php | 4 ++-- tests/methods/SaveTest.php | 4 ++-- tests/properties/default_values_test.php | 6 +++++- tests/properties/worthless_test.php | 8 ++++++-- 8 files changed, 23 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index fa0286e..ec01fc3 100644 --- a/composer.json +++ b/composer.json @@ -14,9 +14,9 @@ ], "autoload":{ "psr-4":{ - "CSV\\": "src", - "CSV\\extensions\\": "src\\extensions", - "CSV\\tests\\": "tests" + "ParseCsv\\": "src", + "ParseCsv\\extensions\\": "src\\extensions", + "ParseCsv\\tests\\": "tests" } }, "require-dev": { diff --git a/src/Csv.php b/src/Csv.php index 7fe2fb0..74381e4 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -1,7 +1,7 @@ csv = new parseCSV(); + $this->csv = new Csv(); //setup the reflection class $this->reflection = new ReflectionClass($this->csv); From 76a0406effea7ddb6074d6022b073105e7e53c63 Mon Sep 17 00:00:00 2001 From: Susann Sgorzaly Date: Sun, 18 Feb 2018 12:43:19 +0100 Subject: [PATCH 04/16] updated examples for using new classname --- examples/basic.php | 4 ++-- examples/conditions.php | 4 ++-- examples/download.php | 6 +++--- examples/limit.php | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/basic.php b/examples/basic.php index 5744525..beec5cb 100644 --- a/examples/basic.php +++ b/examples/basic.php @@ -3,11 +3,11 @@ # include parseCSV class. -require_once('../parsecsv.lib.php'); +use ParseCsv\Csv; # create new parseCSV object. -$csv = new parseCSV(); +$csv = new Csv(); # Parse '_books.csv' using automatic delimiter detection... diff --git a/examples/conditions.php b/examples/conditions.php index c778b1d..c806428 100644 --- a/examples/conditions.php +++ b/examples/conditions.php @@ -3,11 +3,11 @@ # include parseCSV class. -require_once('../parsecsv.lib.php'); +use ParseCsv\Csv; # create new parseCSV object. -$csv = new parseCSV(); +$csv = new Csv(); # Example conditions: diff --git a/examples/download.php b/examples/download.php index bc4177f..19b46e1 100644 --- a/examples/download.php +++ b/examples/download.php @@ -2,11 +2,11 @@ # include parseCSV class. -require_once('../parsecsv.lib.php'); +use ParseCsv\Csv; # create new parseCSV object. -$csv = new parseCSV(); +$csv = new Csv(); # Parse '_books.csv' using automatic delimiter detection... @@ -31,4 +31,4 @@ $csv->output('books.csv'); # or is set to null, output will only return the generated CSV # output data, and will not output to the browser itself. -?> \ No newline at end of file +?> diff --git a/examples/limit.php b/examples/limit.php index a018f59..d0635b3 100644 --- a/examples/limit.php +++ b/examples/limit.php @@ -3,11 +3,11 @@ # include parseCSV class. -require_once('../parsecsv.lib.php'); +use ParseCsv\Csv; # create new parseCSV object. -$csv = new parseCSV(); +$csv = new Csv(); # if sorting is enabled, the whole CSV file From 48cec20f4f5ef010f3b73385cd32dbaa3dda23c3 Mon Sep 17 00:00:00 2001 From: Susann Sgorzaly Date: Sun, 18 Feb 2018 12:45:33 +0100 Subject: [PATCH 05/16] namespace change --- src/enums/DatatypeEnum.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/enums/DatatypeEnum.php b/src/enums/DatatypeEnum.php index 83d64d1..f389a5e 100644 --- a/src/enums/DatatypeEnum.php +++ b/src/enums/DatatypeEnum.php @@ -1,10 +1,10 @@ Date: Sun, 18 Feb 2018 19:00:24 +0100 Subject: [PATCH 06/16] Float regex: Don't allow double dot; do allow 'e' for exponentials --- extensions/DatatypeTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/DatatypeTrait.php b/extensions/DatatypeTrait.php index 6a242b1..7d5359f 100644 --- a/extensions/DatatypeTrait.php +++ b/extensions/DatatypeTrait.php @@ -36,7 +36,7 @@ trait DatatypeTrait { return 'integer'; } - if (preg_match('/^[+-]?([0-9]*[.])?([0-9]|[.][0-9])+$/', $value)) { + if (preg_match('/(^[+-]?$)|(^[+-]?[0-9]+([,.][0-9])?[0-9]*(e[+-]?[0-9]+)?$)/', $value)) { return 'float'; } From 736bc489e64e58ed205d2d1e60411a4b54b005c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 18 Feb 2018 19:01:47 +0100 Subject: [PATCH 07/16] getDatatypes: throw more meaningful exception if no data is present --- extensions/DatatypeTrait.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/extensions/DatatypeTrait.php b/extensions/DatatypeTrait.php index 7d5359f..cf48489 100644 --- a/extensions/DatatypeTrait.php +++ b/extensions/DatatypeTrait.php @@ -74,12 +74,17 @@ trait DatatypeTrait { * * @access public * + * @uses getDatatypeFromString + * * @return array|bool */ public function getDatatypes() { if (empty($this->data)) { $this->data = $this->parse_string(); } + if (!is_array($this->data)) { + throw new \Exception('No data set yet.'); + } $result = []; foreach ($this->titles as $cName) { From 9551862a95a563b6f4a59fcafe857cde70d868a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 18 Feb 2018 19:02:37 +0100 Subject: [PATCH 08/16] Allow current working dir to be different when running tests ...By using an absolute path --- tests/methods/ParseTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/methods/ParseTest.php b/tests/methods/ParseTest.php index 291f81e..ddbdc5e 100644 --- a/tests/methods/ParseTest.php +++ b/tests/methods/ParseTest.php @@ -132,7 +132,7 @@ class ParseTest extends PHPUnit\Framework\TestCase { * @depends testSepRowAutoDetection */ public function testGetColumnDatatypes() { - $this->csv->auto('tests/methods/fixtures/datatype.csv'); + $this->csv->auto(__DIR__ . '/fixtures/datatype.csv'); $this->csv->getDatatypes(); $expected = [ 'title' => 'string', From 2b17f01a0a8064135d89213189823c7a7eb9d822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 18 Feb 2018 19:03:23 +0100 Subject: [PATCH 09/16] Removed additional column to make expected data fit to fixture --- tests/methods/fixtures/datatype.csv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/methods/fixtures/datatype.csv b/tests/methods/fixtures/datatype.csv index ef367b0..83e6019 100644 --- a/tests/methods/fixtures/datatype.csv +++ b/tests/methods/fixtures/datatype.csv @@ -1,5 +1,5 @@ sep=; -title;isbn;publishedAt;published;count;price; +title;isbn;publishedAt;published;count;price Красивая кулинария;5454-5587-3210;21.05.2011;true;1;10.99 The Wine Connoisseurs;2547-8548-2541;12.12.2011;TRUE;;20,33 Weißwein;1313-4545-8875;23.02.2012;false;10;10 From d4f9c9f020018e3c74edb619e95a531af34e3f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 18 Feb 2018 19:04:16 +0100 Subject: [PATCH 10/16] Updated worthless_test to allow for more fields --- tests/properties/worthless_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/properties/worthless_test.php b/tests/properties/worthless_test.php index 5459b59..32e94d7 100644 --- a/tests/properties/worthless_test.php +++ b/tests/properties/worthless_test.php @@ -65,7 +65,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase { * @access public */ public function test_propertiesCount() { - $this->assertCount(28, $this->properties); + $this->assertCount(29, $this->properties); } /** From 92653c99c5fb709505e2e31567f1fa4e346b6fc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 18 Feb 2018 19:31:24 +0100 Subject: [PATCH 11/16] Force predictable working dir; create autoloader file if it doesn't exist --- tests/Bootstrap.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index eb6dd93..88df893 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -1,4 +1,9 @@ Date: Sun, 18 Feb 2018 19:34:06 +0100 Subject: [PATCH 12/16] Absolute path in require do stop PhpStorm from nagging (PhpStorm couldn't find the file) --- tests/Bootstrap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 88df893..797e5b4 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -4,7 +4,7 @@ if (!file_exists('vendor/autoload.php')) { `composer dump-autoload`; } -require './vendor/autoload.php'; +require __DIR__ . '/../vendor/autoload.php'; if (!class_exists('PHPUnit\Framework\TestCase')) { // we run on an older PHPUnit version without namespaces. From af7368378c12cdd1a70e6dc64cb090349b0718de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 18 Feb 2018 19:40:21 +0100 Subject: [PATCH 13/16] Changed tests to work now that we have a namespace --- tests/properties/default_values_test.php | 4 ++-- tests/properties/worthless_test.php | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/properties/default_values_test.php b/tests/properties/default_values_test.php index 74077ca..ab803b9 100644 --- a/tests/properties/default_values_test.php +++ b/tests/properties/default_values_test.php @@ -11,7 +11,7 @@ class default_values_properties_Test extends TestCase { * The parseCSV object * * @access protected - * @var [parseCSV] + * @var Csv */ protected $csv = null; @@ -23,7 +23,7 @@ class default_values_properties_Test extends TestCase { */ public function setUp() { //setup parse CSV - $this->csv = new parseCSV(); + $this->csv = new Csv(); } /** diff --git a/tests/properties/worthless_test.php b/tests/properties/worthless_test.php index 7a9b993..36f036b 100644 --- a/tests/properties/worthless_test.php +++ b/tests/properties/worthless_test.php @@ -11,7 +11,7 @@ class DefaultValuesTest extends TestCase { * The parseCSV object * * @access protected - * @var [parseCSV] + * @var Csv */ protected $csv = null; @@ -20,7 +20,7 @@ class DefaultValuesTest extends TestCase { * The reflection class object * * @access protected - * @var [ReflectionClass] + * @var \ReflectionClass */ protected $reflection = null; @@ -28,6 +28,7 @@ class DefaultValuesTest extends TestCase { * Reflection Properties * The reflected class properties * + * @var \ReflectionProperty[] * @access protected */ protected $properties = null; @@ -43,7 +44,7 @@ class DefaultValuesTest extends TestCase { $this->csv = new Csv(); //setup the reflection class - $this->reflection = new ReflectionClass($this->csv); + $this->reflection = new \ReflectionClass($this->csv); //setup the reflected class properties $this->properties = $this->reflection->getProperties(); @@ -116,7 +117,7 @@ class DefaultValuesTest extends TestCase { ); // Find our real properties - $real_properties = array_map(function (ReflectionProperty $property) { + $real_properties = array_map(function (\ReflectionProperty $property) { return $property->getName(); }, $this->properties); From 52e87953a4a1c0da277c8e1ff27605439f7d01fe Mon Sep 17 00:00:00 2001 From: Susann Sgorzaly Date: Mon, 19 Feb 2018 12:02:48 +0100 Subject: [PATCH 14/16] corrected datatype count --- src/enums/DatatypeEnum.php | 38 ++++++++++++++++++-------------- src/extensions/DatatypeTrait.php | 14 ++++++++++-- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/enums/DatatypeEnum.php b/src/enums/DatatypeEnum.php index f389a5e..77ab684 100644 --- a/src/enums/DatatypeEnum.php +++ b/src/enums/DatatypeEnum.php @@ -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); diff --git a/src/extensions/DatatypeTrait.php b/src/extensions/DatatypeTrait.php index 61464f7..8c9ed37 100644 --- a/src/extensions/DatatypeTrait.php +++ b/src/extensions/DatatypeTrait.php @@ -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); } From 2c7c5525153f88e4de2a748add52da60a1ac637d Mon Sep 17 00:00:00 2001 From: susgo Date: Mon, 19 Feb 2018 12:07:32 +0100 Subject: [PATCH 15/16] Update DatatypeTrait.php --- src/extensions/DatatypeTrait.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/extensions/DatatypeTrait.php b/src/extensions/DatatypeTrait.php index 8c9ed37..50527e1 100644 --- a/src/extensions/DatatypeTrait.php +++ b/src/extensions/DatatypeTrait.php @@ -23,11 +23,7 @@ trait DatatypeTrait { * @return string|false */ private function getMostFrequentDataypeForColumn($datatypes) { - array_filter($datatypes); - - foreach ($datatypes as $value) { - echo gettype($value), "\n"; - } + array_filter($datatypes); // workaround because array_count_values($datatypes) does not work anymore :-( foreach ($datatypes as $type) { From f1a89127c21af99efdb5834c88eff739584b9311 Mon Sep 17 00:00:00 2001 From: Susann Sgorzaly Date: Mon, 19 Feb 2018 14:09:30 +0100 Subject: [PATCH 16/16] regex updated according to merge conflict --- src/enums/DatatypeEnum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/enums/DatatypeEnum.php b/src/enums/DatatypeEnum.php index 77ab684..45c7f5d 100644 --- a/src/enums/DatatypeEnum.php +++ b/src/enums/DatatypeEnum.php @@ -23,7 +23,7 @@ class DatatypeEnum const TYPE_DATE = 'date'; - const REGEX_FLOAT = '/^[+-]?([0-9]*[.,])?([0-9]|[.,][0-9])+$/'; + const REGEX_FLOAT = '/(^[+-]?$)|(^[+-]?[0-9]+([,.][0-9])?[0-9]*(e[+-]?[0-9]+)?$)/'; const REGEX_INT = '/^[-+]?[0-9]\d*$/';