Blame view

lib/Encoder.php 1.39 KB
8e128526   Mihail   add xlsx parser
1
2
3
4
5
6
7
8
  <?php
  /**
   * Created by PhpStorm.
   * User: Cibermag
   * Date: 27.08.2015
   * Time: 13:36
   */
  
d0261fd1   Mihail   fixed namespace i...
9
  namespace yii\multiparser;
8e128526   Mihail   add xlsx parser
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
  
  // @todo add comments
  class Encoder
  {
      /** @var out encoding charset */
      public static $out_charset = 'UTF-8';
      /** @var out encoding charset */
      public static $in_charset = 'windows-1251';
  
      public static function encodeFile($in_charset, $out_charset, $filePath)
      {
  
          $old_content = file_get_contents($filePath);
          $encode_content = self::encodeString( $old_content, $in_charset, $out_charset );
          $file = @fopen($filePath, "w");
          fwrite($file, $encode_content);
          @fclose($file);
      }
  
      public static function encodeArray( $array, $in_charset = '', $out_charset = '')
      {
          if ($in_charset)
          self::$in_charset = $in_charset;
  
          if ($out_charset)
          self::$out_charset = $out_charset;
  
          $result = array_map(
              function ($value) {
  
                  return self::encodeString( $value, self::$in_charset, self::$out_charset );
  
              },
              $array);
  
          return $result;
      }
  
      public static function encodeString( $source, $in_charset = '', $out_charset = '' ){
  
          if ($in_charset)
              self::$in_charset = $in_charset;
  
          if ($out_charset)
              self::$out_charset = $out_charset;
  
          return iconv( self::$in_charset, self::$out_charset, $source );
  
      }
  }