Merge pull request #1 from itexia/introduce-namespaces

init introduce namespaces and enums
This commit is contained in:
susgo
2018-02-19 14:22:46 +01:00
committed by GitHub
15 changed files with 211 additions and 98 deletions

View File

@@ -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.*"

View File

@@ -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...

View File

@@ -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:

View File

@@ -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.
?>
?>

View File

@@ -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

View File

@@ -1,6 +1,9 @@
<?php
namespace ParseCsv;
class parseCSV {
use ParseCsv\extensions\DatatypeTrait;
class Csv {
/*
Class: parseCSV v0.4.3 beta

117
src/enums/DatatypeEnum.php Normal file
View File

@@ -0,0 +1,117 @@
<?php
namespace ParseCsv\enums;
/**
* Class DatatypeEnum
*
* @package ParseCsv\enums
*
* todo: needs a basic parent enum class for error handling.
*/
class DatatypeEnum
{
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]*(e[+-]?[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_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);
}
}

View File

@@ -1,4 +1,5 @@
<?php
namespace ParseCsv\extensions;
trait DatatypeTrait {
@@ -11,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.
@@ -58,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);
@@ -74,23 +45,29 @@ 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) {
$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 : [];
}
}

View File

@@ -1,22 +1,13 @@
<?php
chdir(__DIR__ . '/..');
if (!file_exists('vendor/autoload.php')) {
`composer dump-autoload`;
}
$dir = realpath(__DIR__);
defined('BASE') OR define('BASE', dirname($dir) . '/');
recursivelyIncludeFiles(BASE . 'extensions');
require_once BASE . 'parsecsv.lib.php';
require __DIR__ . '/../vendor/autoload.php';
if (!class_exists('PHPUnit\Framework\TestCase')) {
// we run on an older PHPUnit version without namespaces.
require_once __DIR__ . '/PHPUnit_Framework_TestCase.inc.php';
}
function recursivelyIncludeFiles($pathName){
$dirIterator = new RecursiveDirectoryIterator($pathName);
foreach ($dirIterator as $dir) {
if ($dir->isFile() && $dir->getExtension() === 'php') {
require_once $dir->getPathname();
}
}
}

View File

@@ -1,13 +1,18 @@
<?php
namespace ParseCsv\tests\methods;
class ConstructTest extends PHPUnit\Framework\TestCase {
use ParseCsv\Csv;
use PHPUnit\Framework\TestCase;
class ConstructTest extends TestCase
{
/**
* CSV
* The parseCSV object
* The Csv object
*
* @access protected
* @var [parseCSV]
* @var [Csv]
*/
protected $csv = null;
@@ -19,7 +24,7 @@ class ConstructTest extends PHPUnit\Framework\TestCase {
*/
public function setUp() {
//setup parse CSV
#$this->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);
}

View File

@@ -1,13 +1,18 @@
<?php
namespace ParseCsv\tests\methods;
class ParseTest extends PHPUnit\Framework\TestCase {
use ParseCsv\Csv;
use PHPUnit_Framework_TestCase as TestCase;
class ParseTest extends TestCase
{
/**
* CSV
* The parseCSV object
* The CSV object
*
* @access protected
* @var parseCSV
* @var Csv
*/
protected $csv;
@@ -18,7 +23,7 @@ class ParseTest extends PHPUnit\Framework\TestCase {
* @access public
*/
public function setUp() {
$this->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);

View File

@@ -1,8 +1,13 @@
<?php
namespace ParseCsv\tests\methods;
class SaveTest extends PHPUnit\Framework\TestCase {
use ParseCsv\Csv;
use PHPUnit_Framework_TestCase as TestCase;
/** @var parseCSV */
class SaveTest extends TestCase
{
/** @var Csv */
private $csv;
private $temp_filename;
@@ -11,7 +16,7 @@ class SaveTest extends PHPUnit\Framework\TestCase {
* Setup our test environment objects; will be called before each test.
*/
public function setUp() {
$this->csv = new parseCSV();
$this->csv = new Csv();
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
// Remove last 2 lines to simplify comparison

View File

@@ -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
1 sep=;
2 title;isbn;publishedAt;published;count;price; title;isbn;publishedAt;published;count;price
3 Красивая кулинария;5454-5587-3210;21.05.2011;true;1;10.99
4 The Wine Connoisseurs;2547-8548-2541;12.12.2011;TRUE;;20,33
5 Weißwein;1313-4545-8875;23.02.2012;false;10;10

View File

@@ -1,13 +1,17 @@
<?php
namespace ParseCsv\tests\properties;
class default_values_properties_Test extends PHPUnit\Framework\TestCase {
use ParseCsv\Csv;
use PHPUnit_Framework_TestCase as TestCase;
class default_values_properties_Test extends TestCase {
/**
* CSV
* The parseCSV object
*
* @access protected
* @var [parseCSV]
* @var Csv
*/
protected $csv = null;
@@ -19,7 +23,7 @@ class default_values_properties_Test extends PHPUnit\Framework\TestCase {
*/
public function setUp() {
//setup parse CSV
$this->csv = new parseCSV();
$this->csv = new Csv();
}
/**

View File

@@ -1,13 +1,17 @@
<?php
namespace ParseCsv\tests\properties;
class worthless_properties_Test extends PHPUnit\Framework\TestCase {
use ParseCsv\Csv;
use PHPUnit_Framework_TestCase as TestCase;
class DefaultValuesTest extends TestCase {
/**
* CSV
* The parseCSV object
*
* @access protected
* @var [parseCSV]
* @var Csv
*/
protected $csv = null;
@@ -16,7 +20,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
* The reflection class object
*
* @access protected
* @var [ReflectionClass]
* @var \ReflectionClass
*/
protected $reflection = null;
@@ -24,6 +28,7 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
* Reflection Properties
* The reflected class properties
*
* @var \ReflectionProperty[]
* @access protected
*/
protected $properties = null;
@@ -36,10 +41,10 @@ class worthless_properties_Test extends PHPUnit\Framework\TestCase {
*/
public function setUp() {
//setup parse CSV
$this->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);