Blame view

common/components/parsers/Parser.php 2.05 KB
ad2e91f7   Mihail   move multyparser ...
1
2
3
4
5
6
7
8
9
10
11
  <?php
  /**
   * Created by PhpStorm.
   * User: Cibermag
   * Date: 04.09.2015
   * Time: 18:25
   */
  
  namespace common\components\parsers;
  
  //@todo - заменить read на parse
ad2e91f7   Mihail   move multyparser ...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  
  use common\components\CustomVarDamp;
  
  abstract class Parser
  {
      public $converter_conf  = [];
      protected $converter = NULL;
  
      /** @var file-resource читаемого файла */
      public $file;
      /** @var string путь читаемого файла */
      public $file_path;
  
      /**
       * @var array - результирующий массив с отпарсенными значениями
       */
      protected $result = [];
  
      /** @var array - массив с заголовком,
       * */
      public $keys = NULL;
ad2e91f7   Mihail   move multyparser ...
33
  
2d4b1514   Mihail   adapted xls parser
34
      public abstract function read();
ad2e91f7   Mihail   move multyparser ...
35
36
37
38
39
40
41
42
  
      public function setup()
      {
          $this->setupConverter();
      }
  
      protected function setupConverter()
      {
2d4b1514   Mihail   adapted xls parser
43
          if ( !empty( $this->keys ) ) {
ad2e91f7   Mihail   move multyparser ...
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;
  
              }
          }
ad2e91f7   Mihail   move multyparser ...
56
57
      }
  
ad2e91f7   Mihail   move multyparser ...
58
59
60
61
62
63
64
      /**
       * @param $arr
       * @return mixed
       * преобразовует значения прочитанного массива в нужные типы, согласно конфигурации конвертера
       */
      protected function convert( $arr )
      {
ad2e91f7   Mihail   move multyparser ...
65
66
67
68
69
          if ($this->converter !== NULL) {
  
              $arr = $this->converter->convertByConfiguration( $arr, $this->converter_conf );
  
          }
ad2e91f7   Mihail   move multyparser ...
70
          return $arr;
ad2e91f7   Mihail   move multyparser ...
71
72
      }
  
4b12e92e   Mihail   work with margins...
73
74
      public final static function supportedExtension()
      {
5c35d76d   Mihail   add xls parser
75
          return ['csv','xml','xlsx','txt','xls'];
4b12e92e   Mihail   work with margins...
76
77
      }
  
ad2e91f7   Mihail   move multyparser ...
78
79
      protected function cleanUp(  )
      {
ad2e91f7   Mihail   move multyparser ...
80
81
82
          unset( $this->file );
          unset( $this->converter );
          unset( $this->converter_conf );
ad2e91f7   Mihail   move multyparser ...
83
84
      }
  
ad2e91f7   Mihail   move multyparser ...
85
  }