Commit d0d39eafffb0a854c0abe3b7a9cef00bec504dce
1 parent
48829d22
csv parser with encoding
Showing
4 changed files
with
167 additions
and
46 deletions
Show diff stats
1 | +<?php | |
2 | +/** | |
3 | + * Created by PhpStorm. | |
4 | + * User: Cibermag | |
5 | + * Date: 26.08.2015 | |
6 | + * Time: 17:00 | |
7 | + */ | |
8 | + | |
9 | +namespace app\components\parsers; | |
10 | + | |
11 | +use Yii; | |
12 | +use yii\base\ErrorException; | |
13 | + | |
14 | +class CsvParser implements \IteratorAggregate { | |
15 | + | |
16 | + /** @var string */ | |
17 | + private $filePath; | |
18 | + | |
19 | + /** @var bool */ | |
20 | + private $hasHeaderRow; | |
21 | + | |
22 | + /** @var string */ | |
23 | + private $delimiter; | |
24 | + | |
25 | + /** @var string */ | |
26 | + private $enclosure; | |
27 | + | |
28 | + /** @var string */ | |
29 | + private $escape; | |
30 | + | |
31 | + /** @var resource */ | |
32 | + private $handler; | |
33 | + | |
34 | + /** @var out encoding charset */ | |
35 | + private $out_charset = 'UTF-8'; | |
36 | + /** @var out encoding charset */ | |
37 | + public $in_charset; | |
38 | + /** @var out encoding charset */ | |
39 | + public $first_line; | |
40 | + | |
41 | + | |
42 | + public function __construct($filePath, $first_line = 0,$hasHeaderRow = TRUE) | |
43 | + { | |
44 | + | |
45 | + $this->in_charset = 'windows-1251'; | |
46 | + $this->filePath = $filePath; | |
47 | + $this->hasHeaderRow = $hasHeaderRow; | |
48 | + $this->first_line = $first_line; | |
49 | + | |
50 | + } | |
51 | + | |
52 | +// public function encodeFile( $in_charset, $out_charset, $filePath ){ | |
53 | +// | |
54 | +// $old_content = file_get_contents( $filePath ); | |
55 | +// $encode_content = iconv( $in_charset, $out_charset, $old_content ); | |
56 | +// $file = @fopen( $filePath, "w" ); | |
57 | +// fwrite( $file, $encode_content ); | |
58 | +// @fclose( $file ); | |
59 | +// | |
60 | +// } | |
61 | + | |
62 | + | |
63 | + /** | |
64 | + * @param string $delimiter | |
65 | + * @param string $enclosure | |
66 | + * @param string $escape | |
67 | + * @return $this | |
68 | + */ | |
69 | + public function setup($delimiter = ';', $enclosure = '"', $escape = '\\') | |
70 | + { | |
71 | + $this->handler->setCsvControl($delimiter); | |
72 | + } | |
73 | + | |
74 | + public function getIterator() | |
75 | + { | |
76 | + return new \ArrayIterator($this->read()); | |
77 | + } | |
78 | + | |
79 | + /** | |
80 | + * @return array | |
81 | + * @throws InvalidFileException | |
82 | + * @deprecated Use ::read instead. | |
83 | + */ | |
84 | + public function parseAll() | |
85 | + { | |
86 | + return $this->read(); | |
87 | + } | |
88 | + | |
89 | + /** | |
90 | + * @return array | |
91 | + * @throws InvalidFileException | |
92 | + */ | |
93 | + public function read() | |
94 | + { | |
95 | + $return = []; | |
96 | + | |
97 | + $this->openHandler(); | |
98 | + | |
99 | + $line = 0; | |
100 | + $keys = NULL; | |
101 | + | |
102 | + while (($row = $this->readRow()) !== FALSE) { | |
103 | + $line++; | |
104 | + | |
105 | + if ($this->hasHeaderRow) { | |
106 | + if ($keys === NULL) { | |
107 | + $keys = array_values($row); | |
108 | + } else { | |
109 | + | |
110 | + if (count($keys) !== count($row)) { | |
111 | +// | |
112 | + Yii::warning("Invalid columns detected on line #$line ."); | |
113 | + return $return; | |
114 | + } | |
115 | + | |
116 | + $return[] = array_combine($keys, $row); | |
117 | + } | |
118 | + } else { | |
119 | + $return[] = $row; | |
120 | + } | |
121 | + } | |
122 | + | |
123 | + $this->closeHandler(); | |
124 | + | |
125 | + return $return; | |
126 | + } | |
127 | + | |
128 | + private function openHandler() | |
129 | + { | |
130 | + try { | |
131 | + $this->handler = new \SplFileObject( $this->filePath , 'r' );; | |
132 | + } catch (ErrorException $e) { | |
133 | + Yii::warning("Ошибка открытия файла {$this->filePath}"); | |
134 | + } | |
135 | + $this->handler->setFlags(\SplFileObject::READ_CSV);// | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE); | |
136 | + $this->handler->fseek( $this->first_line ); | |
137 | + $this->setup(); | |
138 | + } | |
139 | + private function closeHandler() | |
140 | + { | |
141 | + $this->handler = NULL; | |
142 | + } | |
143 | + | |
144 | + private function readRow() | |
145 | + { | |
146 | + $dirt_value_arr = $this->handler->fgetcsv( ); | |
147 | + return $this->encodeArray( $dirt_value_arr ); | |
148 | + | |
149 | + } | |
150 | + | |
151 | + private function encodeArray( $array_to_encode ) | |
152 | + { | |
153 | + return array_map(function($array_to_encode) { | |
154 | + return iconv( $this->in_charset, $this->out_charset, $array_to_encode ); | |
155 | + }, $array_to_encode); | |
156 | + | |
157 | + } | |
158 | + | |
159 | + | |
160 | +} | |
0 | 161 | \ No newline at end of file | ... | ... |
backend/components/parsers/ParserHandler.php
1 | 1 | <?php |
2 | 2 | namespace app\components\parsers; |
3 | -use Hranicka\Csv\Reader; | |
3 | + | |
4 | +use app\components\parsers\CsvParser; | |
4 | 5 | |
5 | 6 | class ParserHandler { |
6 | 7 | |
... | ... | @@ -24,8 +25,9 @@ class ParserHandler { |
24 | 25 | |
25 | 26 | public function run(){ |
26 | 27 | if ($this->extension = '.csv'){ |
27 | - $csvParser = new Reader( $this->filePath ); | |
28 | + | |
29 | + $csvParser = new CsvParser( $this->filePath ); | |
28 | 30 | return $csvParser->read(); |
29 | 31 | }; |
30 | 32 | } |
31 | -} | |
32 | 33 | \ No newline at end of file |
34 | +} | ... | ... |
composer.json
composer.lock
... | ... | @@ -4,7 +4,7 @@ |
4 | 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", |
5 | 5 | "This file is @generated automatically" |
6 | 6 | ], |
7 | - "hash": "69b51af17d3c1da8d44e1285a6e5c914", | |
7 | + "hash": "8580bd82955b1fbb80d47024e184056e", | |
8 | 8 | "packages": [ |
9 | 9 | { |
10 | 10 | "name": "bower-asset/bootstrap", |
... | ... | @@ -317,46 +317,6 @@ |
317 | 317 | "time": "2013-11-30 08:25:19" |
318 | 318 | }, |
319 | 319 | { |
320 | - "name": "hranicka/csv", | |
321 | - "version": "1.0.0", | |
322 | - "source": { | |
323 | - "type": "git", | |
324 | - "url": "https://bitbucket.org/hranicka/csv.git", | |
325 | - "reference": "8dcb7436990d979861bb7708377bf9f85e3316ed" | |
326 | - }, | |
327 | - "dist": { | |
328 | - "type": "zip", | |
329 | - "url": "https://bitbucket.org/hranicka/csv/get/8dcb7436990d979861bb7708377bf9f85e3316ed.zip", | |
330 | - "reference": "8dcb7436990d979861bb7708377bf9f85e3316ed", | |
331 | - "shasum": "" | |
332 | - }, | |
333 | - "require": { | |
334 | - "php": ">=5.4.0" | |
335 | - }, | |
336 | - "require-dev": { | |
337 | - "mikey179/vfsstream": "~1.5.0", | |
338 | - "phpunit/phpunit": "~4.3" | |
339 | - }, | |
340 | - "type": "library", | |
341 | - "autoload": { | |
342 | - "psr-4": { | |
343 | - "Hranicka\\Csv\\": "src/" | |
344 | - } | |
345 | - }, | |
346 | - "notification-url": "https://packagist.org/downloads/", | |
347 | - "license": [ | |
348 | - "MIT" | |
349 | - ], | |
350 | - "authors": [ | |
351 | - { | |
352 | - "name": "Jaroslav Hranička", | |
353 | - "email": "hranicka@outlook.com" | |
354 | - } | |
355 | - ], | |
356 | - "description": "CSV parser", | |
357 | - "time": "2015-03-31 09:57:29" | |
358 | - }, | |
359 | - { | |
360 | 320 | "name": "swiftmailer/swiftmailer", |
361 | 321 | "version": "v5.4.1", |
362 | 322 | "source": { | ... | ... |