Blame view

backend/components/parsers/ParserHandler.php 1.18 KB
a63b5418   Administrator   first_commit
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
  <?php
  namespace app\components\parsers;
  
  use app\components\parsers\CsvParser;
  
  class ParserHandler {
  
  /** @var string */
      private $filePath;
  
      /** @var instance of SplFileObject */
      private $fileObject;
  
      /** @var string - extension of file $filePath */
      private $extension;
  
      /** @var string - extension of file $filePath */
      private $first_line;
  
      /**
       * @param string first line in file for parsing
       */
      public function __construct( $filePath, $first_line  )
      {
          $this->filePath = $filePath;
          $this->first_line = $first_line;
  
          try {
              $this->fileObject = new \SplFileObject( $this->filePath , 'r' );;
          } catch (ErrorException $e) {
              Yii::warning("Ошибка открытия файла {$this->filePath}");
          }
  
          //preg_match( '/\.[^\.]+$/i',$filePath, $resultArray );
          $this->extension = $this->fileObject->getExtension();
          $this->run();
      }
  
      public function run(){
          if ($this->extension = 'csv'){
  
              $csvParser = new CsvParser( );
              $csvParser->setup( $this->fileObject, $this->first_line );
              return $csvParser->read();
          };
      }
  }