Blame view

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