Merge remote-tracking branch 'itexia/refactoring-constants'

This commit is contained in:
Christian Bläul
2018-03-07 09:43:22 +01:00
7 changed files with 160 additions and 40 deletions

View File

@@ -2,6 +2,7 @@
namespace ParseCsv; namespace ParseCsv;
use ParseCsv\enums\SortEnum;
use ParseCsv\enums\FileProcessingModeEnum; use ParseCsv\enums\FileProcessingModeEnum;
use ParseCsv\extensions\DatatypeTrait; use ParseCsv\extensions\DatatypeTrait;
@@ -90,7 +91,7 @@ class Csv {
* *
* @var string|null * @var string|null
*/ */
public $sort_type = null; public $sort_type = SortEnum::SORT_TYPE_REGULAR;
/** /**
* Delimiter * Delimiter
@@ -298,12 +299,34 @@ class Csv {
* Class constructor * Class constructor
* *
* @param string|null $input The CSV string or a direct filepath * @param string|null $input The CSV string or a direct filepath
* @param integer|null $offset Number of rows to ignore from the beginning of the data * @param integer|null $offset Number of rows to ignore from the beginning
* @param integer|null $limit Limits the number of returned rows to specified amount * of the data
* @param string|null $conditions Basic SQL-like conditions for row matching * @param integer|null $limit Limits the number of returned rows to
* @param null|true $keep_file_data Keep raw file data in memory after successful parsing (useful for debugging) * specified amount
* @param string|null $conditions Basic SQL-like conditions for row
* matching
* @param null|true $keep_file_data Keep raw file data in memory after
* successful parsing (useful for debugging)
*/ */
public function __construct($input = null, $offset = null, $limit = null, $conditions = null, $keep_file_data = null) { public function __construct($input = null, $offset = null, $limit = null, $conditions = null, $keep_file_data = null) {
$this->init($offset, $limit, $conditions, $keep_file_data);
if (!empty($input)) {
$this->parse($input);
}
}
/**
* @param integer|null $offset Number of rows to ignore from the beginning
* of the data
* @param integer|null $limit Limits the number of returned rows to
* specified amount
* @param string|null $conditions Basic SQL-like conditions for row
* matching
* @param null|true $keep_file_data Keep raw file data in memory after
* successful parsing (useful for debugging)
*/
public function init($offset = null, $limit = null, $conditions = null, $keep_file_data = null) {
if (!is_null($offset)) { if (!is_null($offset)) {
$this->offset = $offset; $this->offset = $offset;
} }
@@ -319,10 +342,6 @@ class Csv {
if (!is_null($keep_file_data)) { if (!is_null($keep_file_data)) {
$this->keep_file_data = $keep_file_data; $this->keep_file_data = $keep_file_data;
} }
if (!empty($input)) {
$this->parse($input);
}
} }
// ============================================== // ==============================================
@@ -345,32 +364,29 @@ class Csv {
$input = $this->file; $input = $this->file;
} }
if (!empty($input)) { if (empty($input)) {
if (!is_null($offset)) { // todo: but why true?
$this->offset = $offset; return true;
} }
if (!is_null($limit)) { $this->init($offset, $limit, $conditions);
$this->limit = $limit;
}
if (!is_null($conditions)) {
$this->conditions = $conditions;
}
if (strlen($input) <= PHP_MAXPATHLEN && is_readable($input)) { if (strlen($input) <= PHP_MAXPATHLEN && is_readable($input)) {
$this->data = $this->parse_file($input); $this->file = $input;
} else { $this->data = $this->parse_file();
$this->file_data = &$input; } else {
$this->data = $this->parse_string(); $this->file = null;
} $this->file_data = &$input;
$this->data = $this->parse_string();
}
if ($this->data === false) { if ($this->data === false) {
return false; return false;
}
} }
return true; return true;
} }
/** /**
@@ -559,7 +575,7 @@ class Csv {
* *
* @return array|bool * @return array|bool
*/ */
public function parse_file($file = null) { protected function parse_file($file = null) {
if (is_null($file)) { if (is_null($file)) {
$file = $this->file; $file = $this->file;
} }
@@ -582,7 +598,7 @@ class Csv {
* *
* @return array|false - 2D array with CSV data, or false on failure * @return array|false - 2D array with CSV data, or false on failure
*/ */
public function parse_string($data = null) { protected function parse_string($data = null) {
if (empty($data)) { if (empty($data)) {
if ($this->_check_data()) { if ($this->_check_data()) {
$data = &$this->file_data; $data = &$this->file_data;
@@ -733,13 +749,7 @@ class Csv {
$this->titles = $head; $this->titles = $head;
if (!empty($this->sort_by)) { if (!empty($this->sort_by)) {
$sort_type = SORT_REGULAR; $sort_type = SortEnum::getSorting($this->sort_type);
if ($this->sort_type == 'numeric') {
$sort_type = SORT_NUMERIC;
} elseif ($this->sort_type == 'string') {
$sort_type = SORT_STRING;
}
$this->sort_reverse ? krsort($rows, $sort_type) : ksort($rows, $sort_type); $this->sort_reverse ? krsort($rows, $sort_type) : ksort($rows, $sort_type);
if ($this->offset !== null || $this->limit !== null) { if ($this->offset !== null || $this->limit !== null) {

View File

@@ -0,0 +1,40 @@
<?php
namespace ParseCsv\enums;
use ReflectionClass;
abstract class AbstractEnum {
/**
* Creates a new value of some type
*
* @param mixed $value
*
* @throws \UnexpectedValueException if incompatible type is given.
*/
public function __construct($value)
{
if (!$this->isValid($value)) {
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
}
$this->value = $value;
}
public static function getConstants(){
$class = get_called_class();
$reflection = new \ReflectionClass($class);
return $reflection->getConstants();
}
/**
* Check if enum value is valid
*
* @param $value
*
* @return bool
*/
public static function isValid($value)
{
return in_array($value, static::getConstants(), true);
}
}

View File

@@ -9,7 +9,7 @@ namespace ParseCsv\enums;
* *
* todo: needs a basic parent enum class for error handling. * todo: needs a basic parent enum class for error handling.
*/ */
class DatatypeEnum { class DatatypeEnum extends AbstractEnum {
const __DEFAULT = self::TYPE_STRING; const __DEFAULT = self::TYPE_STRING;

28
src/enums/SortEnum.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
namespace ParseCsv\enums;
class SortEnum extends AbstractEnum {
const __DEFAULT = self::SORT_TYPE_REGULAR;
const SORT_TYPE_REGULAR = 'regular';
const SORT_TYPE_NUMERIC = 'numeric';
const SORT_TYPE_STRING = 'string';
private static $sorting = array(
self::SORT_TYPE_REGULAR => SORT_REGULAR,
self::SORT_TYPE_STRING => SORT_STRING,
self::SORT_TYPE_NUMERIC => SORT_NUMERIC
);
public static function getSorting($type){
if (array_key_exists($type, self::$sorting)){
return self::$sorting[$type];
}
return self::$sorting[self::__DEFAULT];
}
}

View File

@@ -85,7 +85,8 @@ class ParseTest extends TestCase {
$this->csv->enclosure = '"'; $this->csv->enclosure = '"';
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\""; $sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\"";
$expected_data = [86545235689, 34365587654, 13469874576]; $expected_data = [86545235689, 34365587654, 13469874576];
$actual_data = $this->csv->parse_string($sInput);
$actual_data = $this->invokeMethod($this->csv, 'parse_string', array($sInput));
$actual_column = array_map('reset', $actual_data); $actual_column = array_map('reset', $actual_data);
$this->assertEquals($expected_data, $actual_column); $this->assertEquals($expected_data, $actual_column);
$this->assertEquals([ $this->assertEquals([
@@ -219,4 +220,22 @@ class ParseTest extends TestCase {
$this->assertArrayHasKey('column1', $csv->data[0], 'Data parsed incorrectly with enclosure ' . $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); $this->assertEquals('value1', $csv->data[0]['column1'], 'Data parsed incorrectly with enclosure ' . $enclosure);
} }
/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
private function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
} }

View File

@@ -57,7 +57,7 @@ class DefaultValuesPropertiesTest extends TestCase {
} }
public function test_sort_type_default() { public function test_sort_type_default() {
$this->assertNull($this->csv->sort_type); $this->assertEquals('regular', $this->csv->sort_type);
} }
public function test_delimiter_default() { public function test_delimiter_default() {

View File

@@ -3,6 +3,7 @@
namespace ParseCsv\tests\properties; namespace ParseCsv\tests\properties;
use ParseCsv\Csv; use ParseCsv\Csv;
use ParseCsv\enums\SortEnum;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class PublicPropertiesTest extends TestCase { class PublicPropertiesTest extends TestCase {
@@ -145,4 +146,26 @@ class PublicPropertiesTest extends TestCase {
$this->assertCount($counter, $this->properties); $this->assertCount($counter, $this->properties);
} }
public function testDefaultSortTypeIsRegular(){
$this->assertEquals(SortEnum::SORT_TYPE_REGULAR, $this->csv->sort_type);
}
public function testSetSortType(){
$this->csv->sort_type = 'numeric';
$this->assertEquals(SortEnum::SORT_TYPE_NUMERIC, $this->csv->sort_type);
$this->csv->sort_type = 'string';
$this->assertEquals(SortEnum::SORT_TYPE_STRING, $this->csv->sort_type);
}
public function testGetSorting(){
$this->csv->sort_type = 'numeric';
$sorting = SortEnum::getSorting($this->csv->sort_type);
$this->assertEquals(SORT_NUMERIC, $sorting);
$this->csv->sort_type = 'string';
$sorting = SortEnum::getSorting($this->csv->sort_type);
$this->assertEquals(SORT_STRING, $sorting);
}
} }