Blame view

lib/ParserHandler.php 2.66 KB
8e128526   Mihail   add xlsx parser
1
2
  <?php
  
d0261fd1   Mihail   fixed namespace i...
3
  namespace yii\multiparser;
8e128526   Mihail   add xlsx parser
4
  
8e128526   Mihail   add xlsx parser
5
  
221da14e   Mihail   change SplFileObj...
6
7
  use common\components\CustomVarDamp;
  
8e128526   Mihail   add xlsx parser
8
9
10
11
12
13
  class ParserHandler
  {
      //@todo - добавить комменты на анг язе (ошибки выкидывать тоже на англ яз.)
      //@todo - сделать универсальную обработку ошибок
      //@todo - возможно отказаться от YiiParserHandler
      const DEFAULT_MODE = 'web';
221da14e   Mihail   change SplFileObj...
14
  
8e128526   Mihail   add xlsx parser
15
16
17
18
19
20
  
      /** @var string */
      protected $configuration = [];
      /** @var string */
      protected $custom_configuration = [];
  
221da14e   Mihail   change SplFileObj...
21
22
      /** @var file handle */
      protected $file;
8e128526   Mihail   add xlsx parser
23
  
221da14e   Mihail   change SplFileObj...
24
      /** @var string - extension of file $file_path */
8e128526   Mihail   add xlsx parser
25
26
27
28
29
30
31
32
33
34
35
      protected $extension;
  
      /** @var string - */
      protected $mode;
  
      /** @var string - */
      protected $options;
  
      /**
       * @param string first line in file for parsing
       */
221da14e   Mihail   change SplFileObj...
36
      public function setup($file_path, $options = [])
8e128526   Mihail   add xlsx parser
37
      {
221da14e   Mihail   change SplFileObj...
38
         //$this->file_path = $file_path;
8e128526   Mihail   add xlsx parser
39
40
41
42
43
44
45
46
47
48
49
50
          if (isset($options['mode'])) {
  
              $this->mode = $options['mode'];
              unset($options['mode']);
  
          } else {
  
              $this->mode = self::DEFAULT_MODE;
  
          }
  
          $this->options = $options;
221da14e   Mihail   change SplFileObj...
51
52
53
54
          $this->file = fopen($file_path, 'r');
          $options['file'] = $this->file;
          $options['file_path'] = $file_path;
          $this->extension = pathinfo( $file_path, PATHINFO_EXTENSION );
8e128526   Mihail   add xlsx parser
55
56
57
58
59
60
61
          $this->custom_configuration = $this->getCustomConfiguration($this->extension, $this->mode);
          $this->custom_configuration = array_merge_recursive($this->custom_configuration, $options);
  
      }
  
      public function run()
      {
cd8b9f70   Mihail   add cleanUp metho...
62
          $parser = $this->createObjectByConfiguration( $this->custom_configuration );
8e128526   Mihail   add xlsx parser
63
  
cd8b9f70   Mihail   add cleanUp metho...
64
65
          $parser->setup();
          $result = $parser->read();
8e128526   Mihail   add xlsx parser
66
  
221da14e   Mihail   change SplFileObj...
67
68
69
          unset($parser);
          fclose( $this->file );
  
8e128526   Mihail   add xlsx parser
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
          return $result;
      }
  
      public function getCustomConfiguration($extension, $parameter)
      {
          if (!count($this->configuration)) {
              $this->setConfiguration(require(__DIR__ . '/config.php'));
          }
  
          if (!isset($this->configuration[$extension])) {
              throw new \ErrorException("Parser do not maintain file with extension  {$extension}");
          }
          if (!isset($this->configuration[$extension][$parameter])) {
              throw new \ErrorException("Parser configurator do not have settings for {$parameter} parameter");
          }
  
          return $this->configuration[$extension][$parameter];
      }
  
      public function setConfiguration($configuration)
      {
          $this->configuration = $configuration;
      }
  
      protected function createObjectByConfiguration($configuration)
      {
          return ObjectCreator::build($configuration);
      }
cd8b9f70   Mihail   add cleanUp metho...
98
99
100
101
  
  
  
  
8e128526   Mihail   add xlsx parser
102
  }