Blame view

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