Blame view

backend/components/parsers/CsvParser.php 3.09 KB
d0d39eaf   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
   */
  
67d432ac   Mihail   final upload form...
9
  namespace backend\components\parsers;
2501a752   Mihail   add value filter ...
10
  
d0d39eaf   Mihail   csv parser with e...
11
12
13
  
  use Yii;
  use yii\base\ErrorException;
2501a752   Mihail   add value filter ...
14
  use common\components\debug\CustomVarDamp;
d0d39eaf   Mihail   csv parser with e...
15
  
04c37836   Mihail   add draft version...
16
  class CsvParser  {
d0d39eaf   Mihail   csv parser with e...
17
  
d0d39eaf   Mihail   csv parser with e...
18
19
  
      /** @var bool */
04c37836   Mihail   add draft version...
20
      public $hasHeaderRow = false;
d0d39eaf   Mihail   csv parser with e...
21
  
d0d39eaf   Mihail   csv parser with e...
22
      /** @var resource */
04c37836   Mihail   add draft version...
23
      public $file;
d0d39eaf   Mihail   csv parser with e...
24
25
  
      /** @var out encoding charset */
04c37836   Mihail   add draft version...
26
      public $out_charset = 'UTF-8';
d0d39eaf   Mihail   csv parser with e...
27
      /** @var out encoding charset */
04c37836   Mihail   add draft version...
28
      public $in_charset = 'windows-1251';
febcec0b   Mihail   final version par...
29
      /** @var int - first line for parsing */
04c37836   Mihail   add draft version...
30
      public $first_line = 0;
d0d39eaf   Mihail   csv parser with e...
31
  
febcec0b   Mihail   final version par...
32
      /** @var int - first column for parsing */
04c37836   Mihail   add draft version...
33
      public $first_column = 0;
febcec0b   Mihail   final version par...
34
  
2501a752   Mihail   add value filter ...
35
      /** @var array - array of headers values */
04c37836   Mihail   add draft version...
36
37
38
39
      public $keys;
      public $delimiter = ';';
      public $auto_detect_start_position = false;
      public $min_column_quantity = 5;
2501a752   Mihail   add value filter ...
40
  
6efcd4fa   Mihail   edit csv parser -...
41
  
04c37836   Mihail   add draft version...
42
43
      public function setup()
      {
095562a1   Mihail   csv parser - add ...
44
  
04c37836   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);
095562a1   Mihail   csv parser - add ...
50
51
52
          $this->file->setFlags(\SplFileObject::READ_CSV);
          $this->file->seek( $this->first_line );
  
6efcd4fa   Mihail   edit csv parser -...
53
  
d0d39eaf   Mihail   csv parser with e...
54
55
      }
  
d0d39eaf   Mihail   csv parser with e...
56
  
17693af2   Mihail   edit universal cs...
57
  
04c37836   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;
  
      }
d0d39eaf   Mihail   csv parser with e...
81
82
83
84
85
86
87
  
      /**
       * @return array
       * @throws InvalidFileException
       */
      public function read()
      {
8b105e42   Mihail   add Encoder class
88
          // @todo add comments
d0d39eaf   Mihail   csv parser with e...
89
90
          $return = [];
  
d0d39eaf   Mihail   csv parser with e...
91
          $line = 0;
2501a752   Mihail   add value filter ...
92
          $this->keys = NULL;
d0d39eaf   Mihail   csv parser with e...
93
94
95
96
97
  
          while (($row = $this->readRow()) !== FALSE) {
              $line++;
  
              if ($this->hasHeaderRow) {
2501a752   Mihail   add value filter ...
98
99
                  if ($this->keys === NULL) {
                      $this->keys = array_values($row);
d0d39eaf   Mihail   csv parser with e...
100
101
                  } else {
  
2501a752   Mihail   add value filter ...
102
                      if (count($this->keys) !== count($row)) {
d0d39eaf   Mihail   csv parser with e...
103
104
105
106
107
  //
                          Yii::warning("Invalid columns detected on line #$line .");
                          return $return;
                      }
  
2501a752   Mihail   add value filter ...
108
                      $return[] = array_combine($this->keys, $row);
d0d39eaf   Mihail   csv parser with e...
109
110
111
112
113
114
115
116
117
118
119
                  }
              } else {
                  $return[] = $row;
              }
          }
  
          $this->closeHandler();
  
          return $return;
      }
  
095562a1   Mihail   csv parser - add ...
120
  
17693af2   Mihail   edit universal cs...
121
      protected function closeHandler()
d0d39eaf   Mihail   csv parser with e...
122
      {
095562a1   Mihail   csv parser - add ...
123
          $this->file = NULL;
d0d39eaf   Mihail   csv parser with e...
124
125
      }
  
17693af2   Mihail   edit universal cs...
126
      protected function readRow()
8b105e42   Mihail   add Encoder class
127
          // @todo add comments
d0d39eaf   Mihail   csv parser with e...
128
      {
17693af2   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;
          }
  
2501a752   Mihail   add value filter ...
139
140
  
  //        if ($this->keys !== NULL)
17693af2   Mihail   edit universal cs...
141
  //            @$clear_arr[3] = ValueFilter::pricefilter($clear_arr[3]);{}{}{}
2501a752   Mihail   add value filter ...
142
  
17693af2   Mihail   edit universal cs...
143
          return $row;
d0d39eaf   Mihail   csv parser with e...
144
145
146
  
      }
  
d0d39eaf   Mihail   csv parser with e...
147
148
  
  }