diff --git a/composer.json b/composer.json index dc32b9e..ec01fc3 100644 --- a/composer.json +++ b/composer.json @@ -12,11 +12,12 @@ "email": "will.knauss@gmail.com" } ], - "autoload": { - "classmap": [ - ".", - "./extensions" - ] + "autoload":{ + "psr-4":{ + "ParseCsv\\": "src", + "ParseCsv\\extensions\\": "src\\extensions", + "ParseCsv\\tests\\": "tests" + } }, "require-dev": { "phpunit/phpunit": "4.1.*" 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 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..74381e4 100644 --- a/parsecsv.lib.php +++ b/src/Csv.php @@ -1,6 +1,9 @@ null, + self::TYPE_INT => 'isValidInteger', + self::TYPE_BOOL => 'isValidBoolean', + self::TYPE_FLOAT => 'isValidFloat', + self::TYPE_DATE => 'isValidDate' + ); + + /** + * Checks data type for given string. + * + * @param string $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(__CLASS__, $validator)){ + if (get_class()::$validator($value)) { + return $type; + } + } + + return self::__DEFAULT; + } + } + + /** + * Check if string is float value. + * + * @param string $value + * + * @return bool + */ + private static function isValidFloat($value) { + return (bool) preg_match(self::REGEX_FLOAT, $value); + } + + /** + * Check if string is integer value. + * + * @param string $value + * + * @return bool + */ + private static function isValidInteger($value) { + return (bool) preg_match(self::REGEX_INT, $value); + } + + /** + * Check if string is boolean. + * + * @param string $value + * + * @return bool + */ + private static function isValidBoolean($value) { + return (bool) preg_match(self::REGEX_BOOL, $value); + } + + /** + * Check if string is date. + * + * @param string $value + * + * @return bool + */ + private static function isValidDate($value) { + return (bool) strtotime($value); + } +} diff --git a/extensions/DatatypeTrait.php b/src/extensions/DatatypeTrait.php similarity index 53% rename from extensions/DatatypeTrait.php rename to src/extensions/DatatypeTrait.php index 6a242b1..6ffd9e2 100644 --- a/extensions/DatatypeTrait.php +++ b/src/extensions/DatatypeTrait.php @@ -1,4 +1,5 @@ data)) { $this->data = $this->parse_string(); } + if (!is_array($this->data)) { + throw new \Exception('No data set yet.'); + } $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 : []; } } diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 64ff13b..797e5b4 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -1,22 +1,13 @@ isFile() && $dir->getExtension() === 'php') { - require_once $dir->getPathname(); - } - } -} diff --git a/tests/methods/ConstructTest.php b/tests/methods/ConstructTest.php index 0a3c310..a170310 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..2ea3f2e 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() { @@ -132,7 +137,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', @@ -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..0918b69 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 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 diff --git a/tests/properties/default_values_test.php b/tests/properties/default_values_test.php index 2117094..ab803b9 100644 --- a/tests/properties/default_values_test.php +++ b/tests/properties/default_values_test.php @@ -1,13 +1,17 @@ csv = new parseCSV(); + $this->csv = new Csv(); } /** diff --git a/tests/properties/worthless_test.php b/tests/properties/worthless_test.php index 5459b59..c41c8df 100644 --- a/tests/properties/worthless_test.php +++ b/tests/properties/worthless_test.php @@ -1,13 +1,17 @@ csv = new parseCSV(); + $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(); @@ -65,7 +70,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); } /** @@ -112,7 +117,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase { ); // Find our real properties - $real_properties = array_map(function (ReflectionProperty $property) { + $real_properties = array_map(function (\ReflectionProperty $property) { return $property->getName(); }, $this->properties);