Blame view

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