Commit 4fd43c2545259bcfbba966a5a54e9a36ec156267
1 parent
04c37836
add detectStartPosition func in CsvParser
Showing
4 changed files
with
64 additions
and
34 deletions
Show diff stats
backend/components/parsers/CsvParser.php
... | ... | @@ -13,7 +13,8 @@ use Yii; |
13 | 13 | use yii\base\ErrorException; |
14 | 14 | use common\components\debug\CustomVarDamp; |
15 | 15 | |
16 | -class CsvParser { | |
16 | +class CsvParser | |
17 | +{ | |
17 | 18 | |
18 | 19 | |
19 | 20 | /** @var bool */ |
... | ... | @@ -28,6 +29,7 @@ class CsvParser { |
28 | 29 | public $in_charset = 'windows-1251'; |
29 | 30 | /** @var int - first line for parsing */ |
30 | 31 | public $first_line = 0; |
32 | + public $last_line = 10; | |
31 | 33 | |
32 | 34 | /** @var int - first column for parsing */ |
33 | 35 | public $first_column = 0; |
... | ... | @@ -42,40 +44,51 @@ class CsvParser { |
42 | 44 | public function setup() |
43 | 45 | { |
44 | 46 | |
47 | + $this->file->setCsvControl($this->delimiter); | |
48 | + $this->file->setFlags(\SplFileObject::READ_CSV); | |
49 | + $this->file->setFlags(\SplFileObject::SKIP_EMPTY); | |
50 | +// $this->file->setFlags(\SplFileObject::READ_AHEAD); | |
51 | + | |
45 | 52 | if ($this->auto_detect_start_position) { |
46 | 53 | $this->first_line = $this->detectStartPosition(); |
47 | 54 | } |
48 | - CustomVarDamp::dumpAndDie( $this->first_line ); | |
49 | - $this->file->setCsvControl($this->$delimiter); | |
50 | - $this->file->setFlags(\SplFileObject::READ_CSV); | |
51 | - $this->file->seek( $this->first_line ); | |
55 | + // CustomVarDamp::dumpAndDie($this); | |
56 | +// echo $this->file->key(); | |
57 | +// $this->file->seek($this->first_line + 1); | |
58 | +// echo $this->file->key(); | |
52 | 59 | |
53 | 60 | |
54 | 61 | } |
55 | 62 | |
56 | 63 | |
57 | - | |
58 | - protected function detectStartPosition () | |
64 | + protected function detectStartPosition() | |
59 | 65 | { |
60 | - $first_column = 0; | |
66 | + $first_line = 0; | |
61 | 67 | $find = false; |
62 | 68 | while (!$find) { |
63 | 69 | |
64 | - $j = 0; | |
65 | - $row = $this->readRow(); | |
66 | - $first_column++; | |
67 | - for ($i = 1; $i<=count($row); $i++) { | |
68 | - if (!$row[$i]) { | |
69 | - $j++; | |
70 | + $j = 0; | |
71 | + $row = $this->readRow(); | |
72 | + | |
73 | + if ($row === false) { | |
74 | + continue; | |
70 | 75 | } |
71 | - if ( $j >= $this->min_column_quantity ) { | |
72 | - $find = true; | |
73 | - break; | |
76 | + | |
77 | + $first_line++; | |
78 | + for ($i = 1; $i <= count($row); $i++) { | |
79 | + | |
80 | + if ($row[$i - 1] <> '') { | |
81 | + $j++; | |
82 | + } | |
83 | + | |
84 | + if ($j >= $this->min_column_quantity) { | |
85 | + $find = true; | |
86 | + break; | |
87 | + } | |
74 | 88 | } |
75 | 89 | } |
76 | - } | |
77 | 90 | |
78 | - return $first_column; | |
91 | + return $first_line; | |
79 | 92 | |
80 | 93 | } |
81 | 94 | |
... | ... | @@ -87,10 +100,10 @@ class CsvParser { |
87 | 100 | { |
88 | 101 | // @todo add comments |
89 | 102 | $return = []; |
90 | - | |
103 | + //CustomVarDamp::dump(debug_print_backtrace(1,2)); | |
91 | 104 | $line = 0; |
92 | 105 | $this->keys = NULL; |
93 | - | |
106 | + CustomVarDamp::dump($this->file->key()); | |
94 | 107 | while (($row = $this->readRow()) !== FALSE) { |
95 | 108 | $line++; |
96 | 109 | |
... | ... | @@ -110,10 +123,16 @@ class CsvParser { |
110 | 123 | } else { |
111 | 124 | $return[] = $row; |
112 | 125 | } |
126 | + if(($this->last_line) && ($line > $this->last_line)){ | |
127 | +// CustomVarDamp::dump($this->last_line); | |
128 | +// CustomVarDamp::dump($line); | |
129 | + break; | |
130 | + } | |
131 | + | |
113 | 132 | } |
114 | 133 | |
115 | 134 | $this->closeHandler(); |
116 | - | |
135 | + //CustomVarDamp::dumpAndDie($return); | |
117 | 136 | return $return; |
118 | 137 | } |
119 | 138 | |
... | ... | @@ -127,16 +146,14 @@ class CsvParser { |
127 | 146 | // @todo add comments |
128 | 147 | { |
129 | 148 | |
130 | - $row = $this->file->fgetcsv( ); | |
149 | + $row = $this->file->fgetcsv(); | |
131 | 150 | // |
132 | 151 | if (is_array($row)) { |
133 | - $row = array_slice( $row, $this->first_column ); | |
134 | - $row = Encoder::encodeArray( $this->in_charset, $this->out_charset, $row ); | |
135 | - } else{ | |
136 | - $row = false; | |
152 | + // $row = array_slice( $row, $this->first_column ); | |
153 | + $row = Encoder::encodeArray($this->in_charset, $this->out_charset, $row); | |
137 | 154 | } |
138 | - | |
139 | - | |
155 | + if (is_null($row)) | |
156 | + $row = false; | |
140 | 157 | // if ($this->keys !== NULL) |
141 | 158 | // @$clear_arr[3] = ValueFilter::pricefilter($clear_arr[3]);{}{}{} |
142 | 159 | ... | ... |
backend/components/parsers/ParserHandler.php
... | ... | @@ -35,7 +35,6 @@ class ParserHandler { |
35 | 35 | |
36 | 36 | //preg_match( '/\.[^\.]+$/i',$filePath, $resultArray ); |
37 | 37 | $this->extension = $this->fileObject->getExtension(); |
38 | - $this->run(); | |
39 | 38 | } |
40 | 39 | |
41 | 40 | public function run(){ |
... | ... | @@ -46,12 +45,14 @@ class ParserHandler { |
46 | 45 | $csvParser = Yii::createObject([ |
47 | 46 | 'class' => 'backend\components\parsers\CsvParser', |
48 | 47 | 'file' => $this->fileObject, |
48 | + 'auto_detect_start_position' => true, | |
49 | 49 | ]); |
50 | 50 | //CustomVarDamp::dumpAndDie($csvParser); |
51 | - $csvParser = new CsvParser( ); | |
52 | - $csvParser->setup( $this->fileObject, $first_line, $first_column ); | |
51 | + // $csvParser = new CsvParser( ); | |
52 | + $csvParser->setup( ); | |
53 | 53 | |
54 | - return $csvParser->read(); | |
54 | +// CustomVarDamp::dumpAndDie($data); | |
55 | + return $csvParser->read();;// | |
55 | 56 | }; |
56 | 57 | } |
57 | 58 | } | ... | ... |
backend/views/parser/results.php
... | ... | @@ -22,9 +22,14 @@ $this->params['breadcrumbs'][] = $this->title; |
22 | 22 | 'dataProvider' => $dataProvider, |
23 | 23 | 'columns' => [['class' => SerialColumn::className()], |
24 | 24 | '1', |
25 | - '2',] | |
25 | + '2', | |
26 | + '3', | |
27 | + '4', | |
28 | + '5',] | |
26 | 29 | ]); ?> |
27 | 30 | |
31 | + | |
32 | + | |
28 | 33 | <?= Html::a('Вернуться', ['parser/index'], ['class' => 'btn btn-primary', 'name' => 'Return',]) ?> |
29 | 34 | |
30 | 35 | </div> |
31 | 36 | \ No newline at end of file | ... | ... |
common/components/debug/CustomVarDamp.php
... | ... | @@ -17,4 +17,11 @@ class CustomVarDamp extends BaseVarDumper { |
17 | 17 | echo "</pre>"; |
18 | 18 | die; |
19 | 19 | } |
20 | + public static function dump($var, $depth = 10, $highlight = false) | |
21 | + { | |
22 | + echo "<pre>"; | |
23 | + echo static::dumpAsString($var, $depth, $highlight); | |
24 | + echo "</pre>"; | |
25 | + | |
26 | + } | |
20 | 27 | } |
21 | 28 | \ No newline at end of file | ... | ... |