mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
new feature: auto detect if parsed file has heading
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace ParseCsv\extensions;
|
||||
|
||||
use ParseCsv\enums\DatatypeEnum;
|
||||
|
||||
trait DatatypeTrait {
|
||||
|
||||
/**
|
||||
@@ -47,7 +49,7 @@ trait DatatypeTrait {
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @uses getDatatypeFromString
|
||||
* @uses DatatypeEnum::getValidTypeFromSample
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
@@ -71,4 +73,40 @@ trait DatatypeTrait {
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
if (empty(array_filter($firstRow))){
|
||||
return false;
|
||||
}
|
||||
|
||||
$firstRowDatatype = array_map('ParseCsv\enums\DatatypeEnum::getValidTypeFromSample', $firstRow);
|
||||
|
||||
if ($this->getMostFrequentDatatypeForColumn($firstRowDatatype) !== DatatypeEnum::TYPE_STRING){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user