Commit 095562a12e66d53eb4da6e4b77ff34674f960180
1 parent
d0d39eaf
csv parser - add fist line parametr
Showing
3 changed files
with
42 additions
and
48 deletions
Show diff stats
backend/components/parsers/CsvParser.php
... | ... | @@ -13,41 +13,22 @@ use yii\base\ErrorException; |
13 | 13 | |
14 | 14 | class CsvParser implements \IteratorAggregate { |
15 | 15 | |
16 | - /** @var string */ | |
17 | - private $filePath; | |
18 | 16 | |
19 | 17 | /** @var bool */ |
20 | 18 | private $hasHeaderRow; |
21 | 19 | |
22 | - /** @var string */ | |
23 | - private $delimiter; | |
24 | - | |
25 | - /** @var string */ | |
26 | - private $enclosure; | |
27 | - | |
28 | - /** @var string */ | |
29 | - private $escape; | |
30 | - | |
31 | 20 | /** @var resource */ |
32 | - private $handler; | |
21 | + private $file; | |
33 | 22 | |
34 | 23 | /** @var out encoding charset */ |
35 | 24 | private $out_charset = 'UTF-8'; |
36 | 25 | /** @var out encoding charset */ |
37 | - public $in_charset; | |
26 | + private $in_charset; | |
38 | 27 | /** @var out encoding charset */ |
39 | - public $first_line; | |
28 | + private $first_line; | |
40 | 29 | |
41 | 30 | |
42 | - public function __construct($filePath, $first_line = 0,$hasHeaderRow = TRUE) | |
43 | - { | |
44 | 31 | |
45 | - $this->in_charset = 'windows-1251'; | |
46 | - $this->filePath = $filePath; | |
47 | - $this->hasHeaderRow = $hasHeaderRow; | |
48 | - $this->first_line = $first_line; | |
49 | - | |
50 | - } | |
51 | 32 | |
52 | 33 | // public function encodeFile( $in_charset, $out_charset, $filePath ){ |
53 | 34 | // |
... | ... | @@ -66,9 +47,18 @@ class CsvParser implements \IteratorAggregate { |
66 | 47 | * @param string $escape |
67 | 48 | * @return $this |
68 | 49 | */ |
69 | - public function setup($delimiter = ';', $enclosure = '"', $escape = '\\') | |
50 | + public function setup( $file, $first_line = 1, $hasHeaderRow = TRUE, $delimiter = ';') | |
70 | 51 | { |
71 | - $this->handler->setCsvControl($delimiter); | |
52 | + | |
53 | + $this->file = $file; | |
54 | + | |
55 | + $this->file->setCsvControl($delimiter); | |
56 | + $this->file->setFlags(\SplFileObject::READ_CSV); | |
57 | + $this->file->seek( $this->first_line ); | |
58 | + | |
59 | + $this->first_line = $first_line; | |
60 | + $this->in_charset = 'windows-1251'; | |
61 | + $this->hasHeaderRow = $hasHeaderRow; | |
72 | 62 | } |
73 | 63 | |
74 | 64 | public function getIterator() |
... | ... | @@ -94,8 +84,6 @@ class CsvParser implements \IteratorAggregate { |
94 | 84 | { |
95 | 85 | $return = []; |
96 | 86 | |
97 | - $this->openHandler(); | |
98 | - | |
99 | 87 | $line = 0; |
100 | 88 | $keys = NULL; |
101 | 89 | |
... | ... | @@ -125,26 +113,18 @@ class CsvParser implements \IteratorAggregate { |
125 | 113 | return $return; |
126 | 114 | } |
127 | 115 | |
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 | - } | |
116 | + | |
139 | 117 | private function closeHandler() |
140 | 118 | { |
141 | - $this->handler = NULL; | |
119 | + $this->file = NULL; | |
142 | 120 | } |
143 | 121 | |
144 | 122 | private function readRow() |
145 | 123 | { |
146 | - $dirt_value_arr = $this->handler->fgetcsv( ); | |
147 | - return $this->encodeArray( $dirt_value_arr ); | |
124 | + $dirt_value_arr = $this->file->fgetcsv( ); | |
125 | + $encode_arr = $this->encodeArray( $dirt_value_arr ); | |
126 | + | |
127 | + return $encode_arr; | |
148 | 128 | |
149 | 129 | } |
150 | 130 | ... | ... |
backend/components/parsers/ParserHandler.php
... | ... | @@ -8,25 +8,39 @@ class ParserHandler { |
8 | 8 | /** @var string */ |
9 | 9 | private $filePath; |
10 | 10 | |
11 | - /** @var string */ | |
11 | + /** @var instance of SplFileObject */ | |
12 | + private $fileObject; | |
13 | + | |
14 | + /** @var string - extension of file $filePath */ | |
12 | 15 | private $extension; |
13 | 16 | |
17 | + /** @var string - extension of file $filePath */ | |
18 | + private $first_line; | |
19 | + | |
14 | 20 | /** |
15 | - * @param string $filePath parsing file path | |
21 | + * @param string first line in file for parsing | |
16 | 22 | */ |
17 | - public function __construct( $filePath ) | |
23 | + public function __construct( $filePath, $first_line ) | |
18 | 24 | { |
19 | 25 | $this->filePath = $filePath; |
20 | - preg_match( '/\.[^\.]+$/i',$filePath, $resultArray ); | |
21 | - $this->extension = $resultArray[0]; | |
26 | + $this->first_line = $first_line; | |
27 | + | |
28 | + try { | |
29 | + $this->fileObject = new \SplFileObject( $this->filePath , 'r' );; | |
30 | + } catch (ErrorException $e) { | |
31 | + Yii::warning("Ошибка открытия файла {$this->filePath}"); | |
32 | + } | |
22 | 33 | |
34 | + //preg_match( '/\.[^\.]+$/i',$filePath, $resultArray ); | |
35 | + $this->extension = $this->fileObject->getExtension(); | |
23 | 36 | $this->run(); |
24 | 37 | } |
25 | 38 | |
26 | 39 | public function run(){ |
27 | - if ($this->extension = '.csv'){ | |
40 | + if ($this->extension = 'csv'){ | |
28 | 41 | |
29 | - $csvParser = new CsvParser( $this->filePath ); | |
42 | + $csvParser = new CsvParser( ); | |
43 | + $csvParser->setup( $this->fileObject, $this->first_line ); | |
30 | 44 | return $csvParser->read(); |
31 | 45 | }; |
32 | 46 | } | ... | ... |
backend/controllers/ParserController.php
... | ... | @@ -63,7 +63,7 @@ class ParserController extends Controller |
63 | 63 | $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension; |
64 | 64 | $model->file->saveAs( $filePath ); |
65 | 65 | |
66 | - $parser = new ParserHandler( $filePath ); | |
66 | + $parser = new ParserHandler( $filePath, 0 ); | |
67 | 67 | $data = $parser->run(); |
68 | 68 | |
69 | 69 | if( !is_array($data) ){ | ... | ... |