diff --git a/backend/components/parsers/CsvParser.php b/backend/components/parsers/CsvParser.php new file mode 100644 index 0000000..07e0335 --- /dev/null +++ b/backend/components/parsers/CsvParser.php @@ -0,0 +1,160 @@ +in_charset = 'windows-1251'; + $this->filePath = $filePath; + $this->hasHeaderRow = $hasHeaderRow; + $this->first_line = $first_line; + + } + +// public function encodeFile( $in_charset, $out_charset, $filePath ){ +// +// $old_content = file_get_contents( $filePath ); +// $encode_content = iconv( $in_charset, $out_charset, $old_content ); +// $file = @fopen( $filePath, "w" ); +// fwrite( $file, $encode_content ); +// @fclose( $file ); +// +// } + + + /** + * @param string $delimiter + * @param string $enclosure + * @param string $escape + * @return $this + */ + public function setup($delimiter = ';', $enclosure = '"', $escape = '\\') + { + $this->handler->setCsvControl($delimiter); + } + + public function getIterator() + { + return new \ArrayIterator($this->read()); + } + + /** + * @return array + * @throws InvalidFileException + * @deprecated Use ::read instead. + */ + public function parseAll() + { + return $this->read(); + } + + /** + * @return array + * @throws InvalidFileException + */ + public function read() + { + $return = []; + + $this->openHandler(); + + $line = 0; + $keys = NULL; + + while (($row = $this->readRow()) !== FALSE) { + $line++; + + if ($this->hasHeaderRow) { + if ($keys === NULL) { + $keys = array_values($row); + } else { + + if (count($keys) !== count($row)) { +// + Yii::warning("Invalid columns detected on line #$line ."); + return $return; + } + + $return[] = array_combine($keys, $row); + } + } else { + $return[] = $row; + } + } + + $this->closeHandler(); + + return $return; + } + + private function openHandler() + { + try { + $this->handler = new \SplFileObject( $this->filePath , 'r' );; + } catch (ErrorException $e) { + Yii::warning("Ошибка открытия файла {$this->filePath}"); + } + $this->handler->setFlags(\SplFileObject::READ_CSV);// | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE); + $this->handler->fseek( $this->first_line ); + $this->setup(); + } + private function closeHandler() + { + $this->handler = NULL; + } + + private function readRow() + { + $dirt_value_arr = $this->handler->fgetcsv( ); + return $this->encodeArray( $dirt_value_arr ); + + } + + private function encodeArray( $array_to_encode ) + { + return array_map(function($array_to_encode) { + return iconv( $this->in_charset, $this->out_charset, $array_to_encode ); + }, $array_to_encode); + + } + + +} \ No newline at end of file diff --git a/backend/components/parsers/ParserHandler.php b/backend/components/parsers/ParserHandler.php index faefd66..dcf9969 100644 --- a/backend/components/parsers/ParserHandler.php +++ b/backend/components/parsers/ParserHandler.php @@ -1,6 +1,7 @@ extension = '.csv'){ - $csvParser = new Reader( $this->filePath ); + + $csvParser = new CsvParser( $this->filePath ); return $csvParser->read(); }; } -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index 7e9ebdb..22993b0 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,7 @@ "php": ">=5.4.0", "yiisoft/yii2": ">=2.0.6", "yiisoft/yii2-bootstrap": "*", - "yiisoft/yii2-swiftmailer": "*", - "hranicka/csv": "*" + "yiisoft/yii2-swiftmailer": "*" }, "require-dev": { "yiisoft/yii2-codeception": "*", diff --git a/composer.lock b/composer.lock index 814680e..b0a1f37 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "69b51af17d3c1da8d44e1285a6e5c914", + "hash": "8580bd82955b1fbb80d47024e184056e", "packages": [ { "name": "bower-asset/bootstrap", @@ -317,46 +317,6 @@ "time": "2013-11-30 08:25:19" }, { - "name": "hranicka/csv", - "version": "1.0.0", - "source": { - "type": "git", - "url": "https://bitbucket.org/hranicka/csv.git", - "reference": "8dcb7436990d979861bb7708377bf9f85e3316ed" - }, - "dist": { - "type": "zip", - "url": "https://bitbucket.org/hranicka/csv/get/8dcb7436990d979861bb7708377bf9f85e3316ed.zip", - "reference": "8dcb7436990d979861bb7708377bf9f85e3316ed", - "shasum": "" - }, - "require": { - "php": ">=5.4.0" - }, - "require-dev": { - "mikey179/vfsstream": "~1.5.0", - "phpunit/phpunit": "~4.3" - }, - "type": "library", - "autoload": { - "psr-4": { - "Hranicka\\Csv\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jaroslav Hranička", - "email": "hranicka@outlook.com" - } - ], - "description": "CSV parser", - "time": "2015-03-31 09:57:29" - }, - { "name": "swiftmailer/swiftmailer", "version": "v5.4.1", "source": { -- libgit2 0.21.4