mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
Merge remote-tracking branch 'itexia/auto-detect-file-has-heading'
# Conflicts: # tests/methods/ParseTest.php
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace ParseCsv\extensions;
|
namespace ParseCsv\extensions;
|
||||||
|
|
||||||
|
use ParseCsv\enums\DatatypeEnum;
|
||||||
|
|
||||||
trait DatatypeTrait {
|
trait DatatypeTrait {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,7 +49,7 @@ trait DatatypeTrait {
|
|||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
*
|
*
|
||||||
* @uses getDatatypeFromString
|
* @uses DatatypeEnum::getValidTypeFromSample
|
||||||
*
|
*
|
||||||
* @return array|bool
|
* @return array|bool
|
||||||
*/
|
*/
|
||||||
@@ -62,7 +64,7 @@ trait DatatypeTrait {
|
|||||||
$result = [];
|
$result = [];
|
||||||
foreach ($this->titles as $cName) {
|
foreach ($this->titles as $cName) {
|
||||||
$column = array_column($this->data, $cName);
|
$column = array_column($this->data, $cName);
|
||||||
$cDatatypes = array_map('ParseCsv\enums\DatatypeEnum::getValidTypeFromSample', $column);
|
$cDatatypes = array_map(DatatypeEnum::class . '::getValidTypeFromSample', $column);
|
||||||
|
|
||||||
$result[$cName] = $this->getMostFrequentDatatypeForColumn($cDatatypes);
|
$result[$cName] = $this->getMostFrequentDatatypeForColumn($cDatatypes);
|
||||||
}
|
}
|
||||||
@@ -71,4 +73,41 @@ trait DatatypeTrait {
|
|||||||
|
|
||||||
return !empty($this->data_types) ? $this->data_types : [];
|
return !empty($this->data_types) ? $this->data_types : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check data type of titles / first row for auto detecting if this could be
|
||||||
|
* a heading line.
|
||||||
|
*
|
||||||
|
* Requires PHP >= 5.5
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*
|
||||||
|
* @uses DatatypeEnum::getValidTypeFromSample
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function autoDetectFileHasHeading(){
|
||||||
|
if (empty($this->data)){
|
||||||
|
throw new \UnexpectedValueException('No data set yet.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->heading){
|
||||||
|
$firstRow = $this->titles;
|
||||||
|
} else {
|
||||||
|
$firstRow = $this->data[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
$firstRow = array_filter($firstRow);
|
||||||
|
if (empty($firstRow)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$firstRowDatatype = array_map(DatatypeEnum::class . '::getValidTypeFromSample', $firstRow);
|
||||||
|
|
||||||
|
if ($this->getMostFrequentDatatypeForColumn($firstRowDatatype) !== DatatypeEnum::TYPE_STRING){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -179,6 +179,35 @@ class ParseTest extends TestCase {
|
|||||||
$this->assertEquals($expected, array_keys($this->csv->data[0]));
|
$this->assertEquals($expected, array_keys($this->csv->data[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testSepRowAutoDetection
|
||||||
|
*/
|
||||||
|
public function testAutoDetectFileHasHeading(){
|
||||||
|
if (!function_exists('array_column')) {
|
||||||
|
// getDatatypes requires array_column, but that
|
||||||
|
// function is only available in PHP >= 5.5
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||||
|
$this->assertTrue($this->csv->autoDetectFileHasHeading());
|
||||||
|
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||||
|
$this->assertTrue($this->csv->autoDetectFileHasHeading());
|
||||||
|
|
||||||
|
$this->csv->heading = false;
|
||||||
|
$sInput = "86545235689\r\n34365587654\r\n13469874576";
|
||||||
|
$this->csv->auto($sInput);
|
||||||
|
$this->assertFalse($this->csv->autoDetectFileHasHeading());
|
||||||
|
|
||||||
|
$this->csv->heading = true;
|
||||||
|
$sInput = "86545235689\r\n34365587654\r\n13469874576";
|
||||||
|
$this->csv->auto($sInput);
|
||||||
|
$this->assertFalse($this->csv->autoDetectFileHasHeading());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
protected function _get_magazines_data() {
|
protected function _get_magazines_data() {
|
||||||
return [
|
return [
|
||||||
[
|
[
|
||||||
|
|||||||
Reference in New Issue
Block a user