Blame view

backend/components/parsers/CsvParser.php 3.78 KB
1e991822   Mihail   csv parser with e...
1
2
3
4
5
6
7
8
  <?php
  /**
   * Created by PhpStorm.
   * User: Cibermag
   * Date: 26.08.2015
   * Time: 17:00
   */
  
36b545ad   Mihail   final upload form...
9
  namespace backend\components\parsers;
dc10d651   Mihail   add value filter ...
10
  
1e991822   Mihail   csv parser with e...
11
12
13
  
  use Yii;
  use yii\base\ErrorException;
dc10d651   Mihail   add value filter ...
14
  use common\components\debug\CustomVarDamp;
1e991822   Mihail   csv parser with e...
15
  
999b9326   Mihail   add detectStartPo...
16
17
  class CsvParser
  {
1e991822   Mihail   csv parser with e...
18
  
1e991822   Mihail   csv parser with e...
19
20
  
      /** @var bool */
e55d56cc   Mihail   add draft version...
21
      public $hasHeaderRow = false;
1e991822   Mihail   csv parser with e...
22
  
1e991822   Mihail   csv parser with e...
23
      /** @var resource */
e55d56cc   Mihail   add draft version...
24
      public $file;
1e991822   Mihail   csv parser with e...
25
26
  
      /** @var out encoding charset */
e55d56cc   Mihail   add draft version...
27
      public $out_charset = 'UTF-8';
1e991822   Mihail   csv parser with e...
28
      /** @var out encoding charset */
e55d56cc   Mihail   add draft version...
29
      public $in_charset = 'windows-1251';
b13b1c83   Mihail   final version par...
30
      /** @var int - first line for parsing */
e55d56cc   Mihail   add draft version...
31
      public $first_line = 0;
999b9326   Mihail   add detectStartPo...
32
      public $last_line = 10;
1e991822   Mihail   csv parser with e...
33
  
b13b1c83   Mihail   final version par...
34
      /** @var int - first column for parsing */
e55d56cc   Mihail   add draft version...
35
      public $first_column = 0;
b13b1c83   Mihail   final version par...
36
  
dc10d651   Mihail   add value filter ...
37
      /** @var array - array of headers values */
e55d56cc   Mihail   add draft version...
38
39
40
41
      public $keys;
      public $delimiter = ';';
      public $auto_detect_start_position = false;
      public $min_column_quantity = 5;
dc10d651   Mihail   add value filter ...
42
  
5c710262   Mihail   edit csv parser -...
43
  
e55d56cc   Mihail   add draft version...
44
45
      public function setup()
      {
2957209c   Mihail   csv parser - add ...
46
  
999b9326   Mihail   add detectStartPo...
47
48
49
50
51
          $this->file->setCsvControl($this->delimiter);
          $this->file->setFlags(\SplFileObject::READ_CSV);
          $this->file->setFlags(\SplFileObject::SKIP_EMPTY);
  //        $this->file->setFlags(\SplFileObject::READ_AHEAD);
  
e55d56cc   Mihail   add draft version...
52
53
54
          if ($this->auto_detect_start_position) {
              $this->first_line = $this->detectStartPosition();
          }
999b9326   Mihail   add detectStartPo...
55
56
57
58
      //    CustomVarDamp::dumpAndDie($this);
  //        echo  $this->file->key();
  //        $this->file->seek($this->first_line + 1);
  //        echo  $this->file->key();
2957209c   Mihail   csv parser - add ...
59
  
5c710262   Mihail   edit csv parser -...
60
  
1e991822   Mihail   csv parser with e...
61
62
      }
  
1e991822   Mihail   csv parser with e...
63
  
999b9326   Mihail   add detectStartPo...
64
      protected function detectStartPosition()
e55d56cc   Mihail   add draft version...
65
      {
999b9326   Mihail   add detectStartPo...
66
          $first_line = 0;
e55d56cc   Mihail   add draft version...
67
68
69
          $find = false;
          while (!$find) {
  
999b9326   Mihail   add detectStartPo...
70
71
72
73
74
              $j = 0;
              $row = $this->readRow();
  
              if ($row === false) {
                  continue;
e55d56cc   Mihail   add draft version...
75
              }
999b9326   Mihail   add detectStartPo...
76
77
78
79
80
81
82
83
84
85
86
87
  
              $first_line++;
              for ($i = 1; $i <= count($row); $i++) {
  
                  if ($row[$i - 1] <> '') {
                      $j++;
                  }
  
                  if ($j >= $this->min_column_quantity) {
                      $find = true;
                      break;
                  }
e55d56cc   Mihail   add draft version...
88
89
              }
          }
e55d56cc   Mihail   add draft version...
90
  
999b9326   Mihail   add detectStartPo...
91
          return $first_line;
e55d56cc   Mihail   add draft version...
92
93
  
      }
1e991822   Mihail   csv parser with e...
94
95
96
97
98
99
100
  
      /**
       * @return array
       * @throws InvalidFileException
       */
      public function read()
      {
e91188dd   Mihail   add Encoder class
101
          // @todo add comments
1e991822   Mihail   csv parser with e...
102
          $return = [];
999b9326   Mihail   add detectStartPo...
103
          //CustomVarDamp::dump(debug_print_backtrace(1,2));
1e991822   Mihail   csv parser with e...
104
          $line = 0;
dc10d651   Mihail   add value filter ...
105
          $this->keys = NULL;
999b9326   Mihail   add detectStartPo...
106
          CustomVarDamp::dump($this->file->key());
1e991822   Mihail   csv parser with e...
107
108
109
110
          while (($row = $this->readRow()) !== FALSE) {
              $line++;
  
              if ($this->hasHeaderRow) {
dc10d651   Mihail   add value filter ...
111
112
                  if ($this->keys === NULL) {
                      $this->keys = array_values($row);
1e991822   Mihail   csv parser with e...
113
114
                  } else {
  
dc10d651   Mihail   add value filter ...
115
                      if (count($this->keys) !== count($row)) {
1e991822   Mihail   csv parser with e...
116
117
118
119
120
  //
                          Yii::warning("Invalid columns detected on line #$line .");
                          return $return;
                      }
  
dc10d651   Mihail   add value filter ...
121
                      $return[] = array_combine($this->keys, $row);
1e991822   Mihail   csv parser with e...
122
123
124
125
                  }
              } else {
                  $return[] = $row;
              }
999b9326   Mihail   add detectStartPo...
126
127
128
129
130
131
              if(($this->last_line) && ($line > $this->last_line)){
  //                CustomVarDamp::dump($this->last_line);
  //                CustomVarDamp::dump($line);
                  break;
              }
  
1e991822   Mihail   csv parser with e...
132
133
134
          }
  
          $this->closeHandler();
999b9326   Mihail   add detectStartPo...
135
         //CustomVarDamp::dumpAndDie($return);
1e991822   Mihail   csv parser with e...
136
137
138
          return $return;
      }
  
2957209c   Mihail   csv parser - add ...
139
  
dfeb2d10   Mihail   edit universal cs...
140
      protected function closeHandler()
1e991822   Mihail   csv parser with e...
141
      {
2957209c   Mihail   csv parser - add ...
142
          $this->file = NULL;
1e991822   Mihail   csv parser with e...
143
144
      }
  
dfeb2d10   Mihail   edit universal cs...
145
      protected function readRow()
e91188dd   Mihail   add Encoder class
146
          // @todo add comments
1e991822   Mihail   csv parser with e...
147
      {
dfeb2d10   Mihail   edit universal cs...
148
  
999b9326   Mihail   add detectStartPo...
149
          $row = $this->file->fgetcsv();
dfeb2d10   Mihail   edit universal cs...
150
151
          //
          if (is_array($row)) {
999b9326   Mihail   add detectStartPo...
152
153
              //        $row = array_slice( $row, $this->first_column );
              $row = Encoder::encodeArray($this->in_charset, $this->out_charset, $row);
dfeb2d10   Mihail   edit universal cs...
154
          }
999b9326   Mihail   add detectStartPo...
155
156
          if (is_null($row))
              $row = false;
dc10d651   Mihail   add value filter ...
157
  //        if ($this->keys !== NULL)
dfeb2d10   Mihail   edit universal cs...
158
  //            @$clear_arr[3] = ValueFilter::pricefilter($clear_arr[3]);{}{}{}
dc10d651   Mihail   add value filter ...
159
  
dfeb2d10   Mihail   edit universal cs...
160
          return $row;
1e991822   Mihail   csv parser with e...
161
162
163
  
      }
  
1e991822   Mihail   csv parser with e...
164
165
  
  }