Blame view

lib/Parser.php 2.09 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
  
8e128526   Mihail   add xlsx parser
13
14
15
16
17
  abstract class Parser
  {
      public $converter_conf  = [];
      protected $converter = NULL;
  
c3a52936   Mihail   code refactoring
18
      //@todo - данные  реквизиты нужно инкапсулировать
221da14e   Mihail   change SplFileObj...
19
      /** @var file-resource читаемого файла */
cd8b9f70   Mihail   add cleanUp metho...
20
      public $file;
221da14e   Mihail   change SplFileObj...
21
22
      /** @var string путь читаемого файла */
      public $file_path;
cd8b9f70   Mihail   add cleanUp metho...
23
  
8e128526   Mihail   add xlsx parser
24
25
26
27
28
29
30
31
      /**
       * @var array - результирующий массив с отпарсенными значениями
       */
      protected $result = [];
  
      /** @var array - массив с заголовком,
       * */
      public $keys = NULL;
8e128526   Mihail   add xlsx parser
32
  
a3b2aa93   Mihail   change meaning of...
33
      public abstract function read();
8e128526   Mihail   add xlsx parser
34
35
36
37
38
39
40
41
  
      public function setup()
      {
          $this->setupConverter();
      }
  
      protected function setupConverter()
      {
a3b2aa93   Mihail   change meaning of...
42
          if ( !empty( $this->keys ) ) {
8e128526   Mihail   add xlsx parser
43
44
45
46
47
48
49
50
51
52
53
54
              // если у файла есть заголовок, то в результате имеем ассоциативный массив
              $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
55
56
      }
  
8e128526   Mihail   add xlsx parser
57
58
59
60
61
62
63
      /**
       * @param $arr
       * @return mixed
       * преобразовует значения прочитанного массива в нужные типы, согласно конфигурации конвертера
       */
      protected function convert( $arr )
      {
8e128526   Mihail   add xlsx parser
64
65
66
67
68
          if ($this->converter !== NULL) {
  
              $arr = $this->converter->convertByConfiguration( $arr, $this->converter_conf );
  
          }
8e128526   Mihail   add xlsx parser
69
          return $arr;
8e128526   Mihail   add xlsx parser
70
      }
cd8b9f70   Mihail   add cleanUp metho...
71
  
ec440fb6   Mihail   add supported ext...
72
73
      public final static function supportedExtension()
      {
a3b2aa93   Mihail   change meaning of...
74
          return ['csv','xml','xlsx','txt','xls'];
ec440fb6   Mihail   add supported ext...
75
76
      }
  
cd8b9f70   Mihail   add cleanUp metho...
77
78
      protected function cleanUp(  )
      {
cd8b9f70   Mihail   add cleanUp metho...
79
80
81
          unset( $this->file );
          unset( $this->converter );
          unset( $this->converter_conf );
cd8b9f70   Mihail   add cleanUp metho...
82
83
      }
  
8e128526   Mihail   add xlsx parser
84
  }