Blame view

lib/Parser.php 2.04 KB
8e128526   Mihail   add xlsx parser
1
2
3
4
5
6
7
8
  <?php
  /**
   * Created by PhpStorm.
   * User: Cibermag
   * Date: 04.09.2015
   * Time: 18:25
   */
  
d0261fd1   Mihail   fixed namespace i...
9
  namespace yii\multiparser;
8e128526   Mihail   add xlsx parser
10
11
  
  //@todo - заменить read на parse
8e128526   Mihail   add xlsx parser
12
  
221da14e   Mihail   change SplFileObj...
13
14
  use common\components\CustomVarDamp;
  
8e128526   Mihail   add xlsx parser
15
16
17
18
19
  abstract class Parser
  {
      public $converter_conf  = [];
      protected $converter = NULL;
  
221da14e   Mihail   change SplFileObj...
20
      /** @var file-resource читаемого файла */
cd8b9f70   Mihail   add cleanUp metho...
21
      public $file;
221da14e   Mihail   change SplFileObj...
22
23
      /** @var string путь читаемого файла */
      public $file_path;
cd8b9f70   Mihail   add cleanUp metho...
24
  
8e128526   Mihail   add xlsx parser
25
26
27
28
29
30
31
32
      /**
       * @var array - результирующий массив с отпарсенными значениями
       */
      protected $result = [];
  
      /** @var array - массив с заголовком,
       * */
      public $keys = NULL;
8e128526   Mihail   add xlsx parser
33
  
a3b2aa93   Mihail   change meaning of...
34
      public abstract function read();
8e128526   Mihail   add xlsx parser
35
36
37
38
39
40
41
42
  
      public function setup()
      {
          $this->setupConverter();
      }
  
      protected function setupConverter()
      {
a3b2aa93   Mihail   change meaning of...
43
          if ( !empty( $this->keys ) ) {
8e128526   Mihail   add xlsx parser
44
45
46
47
48
49
50
51
52
53
54
55
              // если у файла есть заголовок, то в результате имеем ассоциативный массив
              $this->converter_conf['hasKey'] = 1;
          }
  
          if ( $this->converter_conf ) {
              $converter = ObjectCreator::build( $this->converter_conf );
              if ( $converter instanceof ConverterInterface ) {
  
                  $this->converter = $converter;
  
              }
          }
8e128526   Mihail   add xlsx parser
56
57
      }
  
8e128526   Mihail   add xlsx parser
58
59
60
61
62
63
64
      /**
       * @param $arr
       * @return mixed
       * преобразовует значения прочитанного массива в нужные типы, согласно конфигурации конвертера
       */
      protected function convert( $arr )
      {
8e128526   Mihail   add xlsx parser
65
66
67
68
69
          if ($this->converter !== NULL) {
  
              $arr = $this->converter->convertByConfiguration( $arr, $this->converter_conf );
  
          }
8e128526   Mihail   add xlsx parser
70
          return $arr;
8e128526   Mihail   add xlsx parser
71
      }
cd8b9f70   Mihail   add cleanUp metho...
72
  
ec440fb6   Mihail   add supported ext...
73
74
      public final static function supportedExtension()
      {
a3b2aa93   Mihail   change meaning of...
75
          return ['csv','xml','xlsx','txt','xls'];
ec440fb6   Mihail   add supported ext...
76
77
      }
  
cd8b9f70   Mihail   add cleanUp metho...
78
79
      protected function cleanUp(  )
      {
cd8b9f70   Mihail   add cleanUp metho...
80
81
82
          unset( $this->file );
          unset( $this->converter );
          unset( $this->converter_conf );
cd8b9f70   Mihail   add cleanUp metho...
83
84
      }
  
8e128526   Mihail   add xlsx parser
85
  }