From 8fb172f5168baaad55ad9754f983c97b70c598ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Oliveira=20de=20S=C3=A3o=20Jos=C3=A9?= Date: Thu, 6 Oct 2016 16:01:00 -0300 Subject: [PATCH 1/3] Update CsvImport.php --- src/RdnCsv/Controller/Plugin/CsvImport.php | 33 ++++++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/RdnCsv/Controller/Plugin/CsvImport.php b/src/RdnCsv/Controller/Plugin/CsvImport.php index d7b1029..c59db3a 100755 --- a/src/RdnCsv/Controller/Plugin/CsvImport.php +++ b/src/RdnCsv/Controller/Plugin/CsvImport.php @@ -36,7 +36,7 @@ class CsvImport extends AbstractPlugin implements \Countable, \Iterator * * @return CsvImport */ - public function __invoke($filepath, $useFirstRecordAsHeader = true, $delimiter = ',', $enclosure = '"', $escape = '\\') + public function __invoke($filepath, $useFirstRecordAsHeader = true, $delimiter = ',', $enclosure = '"', $escape = '\\', $checkHeader=false) { $this->file = new SplFileObject($filepath); $this->file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE); @@ -45,7 +45,10 @@ public function __invoke($filepath, $useFirstRecordAsHeader = true, $delimiter = $this->useFirstRecordAsHeader = $useFirstRecordAsHeader; + $this->checkHeader = $checkHeader; + return $this; + } /** @@ -69,12 +72,15 @@ public function rewind() $this->file->rewind(); if ($this->useFirstRecordAsHeader) { + $this->checkHeader(); if (!$this->file->valid()) { throw new \RuntimeException('Expected first row to be header, but reached EOF instead'); } + $this->header = $this->file->current(); $this->file->next(); + } } @@ -108,6 +114,7 @@ public function current() return array_combine($header, $line); } return $line; + } /** @@ -131,9 +138,9 @@ public function valid() } /** - * Count number of records in file (excluding header if first record is used as header). + * Count elements of an object * - * @return int + * @return int The custom count as an integer. */ public function count() { @@ -148,4 +155,24 @@ public function count() } return $total; } + /** + * check for the header in the file + * + * @throws \RuntimeException + */ + public function checkHeader() + { + $checkHeader = $this->checkHeader; + if ($checkHeader) { + foreach ($this->file as $line){ + $data[] = $line; + } + $headerFile = $data[0]; + $check = array_values(array_diff($headerFile, $checkHeader)); + if ($check) { + throw new \RuntimeException(sprintf('Not Found Header %s',$check[0])); + } + } + } + } From 6c1e30348ba3578352c02727eddd90afa79df2fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Oliveira=20de=20S=C3=A3o=20Jos=C3=A9?= Date: Thu, 6 Oct 2016 16:33:38 -0300 Subject: [PATCH 2/3] Update CsvImport.php --- src/RdnCsv/Controller/Plugin/CsvImport.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RdnCsv/Controller/Plugin/CsvImport.php b/src/RdnCsv/Controller/Plugin/CsvImport.php index c59db3a..5f79940 100755 --- a/src/RdnCsv/Controller/Plugin/CsvImport.php +++ b/src/RdnCsv/Controller/Plugin/CsvImport.php @@ -72,12 +72,12 @@ public function rewind() $this->file->rewind(); if ($this->useFirstRecordAsHeader) { - $this->checkHeader(); + if (!$this->file->valid()) { throw new \RuntimeException('Expected first row to be header, but reached EOF instead'); } - + $this->checkHeader(); $this->header = $this->file->current(); $this->file->next(); From 0cadd137abb1070a95e0049de8ab418dc4b3591d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Oliveira=20de=20S=C3=A3o=20Jos=C3=A9?= Date: Fri, 7 Oct 2016 20:43:02 -0300 Subject: [PATCH 3/3] Update CsvImport.php --- src/RdnCsv/Controller/Plugin/CsvImport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RdnCsv/Controller/Plugin/CsvImport.php b/src/RdnCsv/Controller/Plugin/CsvImport.php index 5f79940..8e63ef3 100755 --- a/src/RdnCsv/Controller/Plugin/CsvImport.php +++ b/src/RdnCsv/Controller/Plugin/CsvImport.php @@ -69,6 +69,7 @@ public function getFile() */ public function rewind() { + $this->checkHeader(); $this->file->rewind(); if ($this->useFirstRecordAsHeader) { @@ -77,7 +78,6 @@ public function rewind() { throw new \RuntimeException('Expected first row to be header, but reached EOF instead'); } - $this->checkHeader(); $this->header = $this->file->current(); $this->file->next();