Commit e55d56cc451aee24d07b72e478b28ad90b3dc88b

Authored by Mihail
1 parent dfeb2d10

add draft version of detect function

backend/components/parsers/CsvParser.php
@@ -13,51 +13,71 @@ use Yii; @@ -13,51 +13,71 @@ use Yii;
13 use yii\base\ErrorException; 13 use yii\base\ErrorException;
14 use common\components\debug\CustomVarDamp; 14 use common\components\debug\CustomVarDamp;
15 15
16 -class CsvParser implements \IteratorAggregate { 16 +class CsvParser {
17 17
18 18
19 /** @var bool */ 19 /** @var bool */
20 - private $hasHeaderRow; 20 + public $hasHeaderRow = false;
21 21
22 /** @var resource */ 22 /** @var resource */
23 - private $file; 23 + public $file;
24 24
25 /** @var out encoding charset */ 25 /** @var out encoding charset */
26 - private $out_charset = 'UTF-8'; 26 + public $out_charset = 'UTF-8';
27 /** @var out encoding charset */ 27 /** @var out encoding charset */
28 - private $in_charset; 28 + public $in_charset = 'windows-1251';
29 /** @var int - first line for parsing */ 29 /** @var int - first line for parsing */
30 - private $first_line; 30 + public $first_line = 0;
31 31
32 /** @var int - first column for parsing */ 32 /** @var int - first column for parsing */
33 - private $first_column; 33 + public $first_column = 0;
34 34
35 /** @var array - array of headers values */ 35 /** @var array - array of headers values */
36 - private $keys; 36 + public $keys;
  37 + public $delimiter = ';';
  38 + public $auto_detect_start_position = false;
  39 + public $min_column_quantity = 5;
37 40
38 - public function setup( $file, $first_line, $first_column, $hasHeaderRow = false, $delimiter = ';')  
39 - {  
40 -  
41 - $this->first_line = $first_line;  
42 - $this->first_column = $first_column;  
43 41
44 - $this->file = $file; 42 + public function setup()
  43 + {
45 44
46 - $this->file->setCsvControl($delimiter); 45 + if ($this->auto_detect_start_position) {
  46 + $this->first_line = $this->detectStartPosition();
  47 + }
  48 + CustomVarDamp::dumpAndDie( $this->first_line );
  49 + $this->file->setCsvControl($this->$delimiter);
47 $this->file->setFlags(\SplFileObject::READ_CSV); 50 $this->file->setFlags(\SplFileObject::READ_CSV);
48 $this->file->seek( $this->first_line ); 51 $this->file->seek( $this->first_line );
49 52
50 53
51 - $this->in_charset = 'windows-1251';  
52 - $this->hasHeaderRow = $hasHeaderRow;  
53 } 54 }
54 55
55 - public function getIterator() 56 +
  57 +
  58 + protected function detectStartPosition ()
56 { 59 {
57 - return new \ArrayIterator($this->read());  
58 - } 60 + $first_column = 0;
  61 + $find = false;
  62 + while (!$find) {
  63 +
  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 + }
  71 + if ( $j >= $this->min_column_quantity ) {
  72 + $find = true;
  73 + break;
  74 + }
  75 + }
  76 + }
59 77
  78 + return $first_column;
60 79
  80 + }
61 81
62 /** 82 /**
63 * @return array 83 * @return array
backend/components/parsers/ParserHandler.php
1 <?php 1 <?php
2 namespace backend\components\parsers; 2 namespace backend\components\parsers;
  3 +use common\components\debug\CustomVarDamp;
3 4
4 use Yii; 5 use Yii;
5 6
@@ -42,6 +43,11 @@ class ParserHandler { @@ -42,6 +43,11 @@ class ParserHandler {
42 $first_line = isset( $this->options->first_line )? $this->options->first_line : 0; 43 $first_line = isset( $this->options->first_line )? $this->options->first_line : 0;
43 $first_column = isset( $this->options->first_column )? $this->options->first_column : 0; 44 $first_column = isset( $this->options->first_column )? $this->options->first_column : 0;
44 45
  46 + $csvParser = Yii::createObject([
  47 + 'class' => 'backend\components\parsers\CsvParser',
  48 + 'file' => $this->fileObject,
  49 + ]);
  50 + //CustomVarDamp::dumpAndDie($csvParser);
45 $csvParser = new CsvParser( ); 51 $csvParser = new CsvParser( );
46 $csvParser->setup( $this->fileObject, $first_line, $first_column ); 52 $csvParser->setup( $this->fileObject, $first_line, $first_column );
47 53