Blame view

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