Commit 8b105e4256b24dc26d6cda7d6053e4b0887d956a

Authored by Mihail
1 parent 095562a1

add Encoder class

backend/components/parsers/CsvParser.php
... ... @@ -27,27 +27,7 @@ class CsvParser implements \IteratorAggregate {
27 27 /** @var out encoding charset */
28 28 private $first_line;
29 29  
30   -
31   -
32   -
33   -// public function encodeFile( $in_charset, $out_charset, $filePath ){
34   -//
35   -// $old_content = file_get_contents( $filePath );
36   -// $encode_content = iconv( $in_charset, $out_charset, $old_content );
37   -// $file = @fopen( $filePath, "w" );
38   -// fwrite( $file, $encode_content );
39   -// @fclose( $file );
40   -//
41   -// }
42   -
43   -
44   - /**
45   - * @param string $delimiter
46   - * @param string $enclosure
47   - * @param string $escape
48   - * @return $this
49   - */
50   - public function setup( $file, $first_line = 1, $hasHeaderRow = TRUE, $delimiter = ';')
  30 + public function setup( $file, $first_line, $hasHeaderRow = TRUE, $delimiter = ';')
51 31 {
52 32  
53 33 $this->file = $file;
... ... @@ -82,6 +62,7 @@ class CsvParser implements \IteratorAggregate {
82 62 */
83 63 public function read()
84 64 {
  65 + // @todo add comments
85 66 $return = [];
86 67  
87 68 $line = 0;
... ... @@ -120,21 +101,24 @@ class CsvParser implements \IteratorAggregate {
120 101 }
121 102  
122 103 private function readRow()
  104 + // @todo add comments
123 105 {
124 106 $dirt_value_arr = $this->file->fgetcsv( );
125   - $encode_arr = $this->encodeArray( $dirt_value_arr );
126 107  
127   - return $encode_arr;
128   -
129   - }
  108 + $dirt_value_arr = array_slice( $dirt_value_arr, 2 );
  109 + $clear_arr = Encoder::encodeArray( $this->in_charset, $this->out_charset, $dirt_value_arr );
130 110  
131   - private function encodeArray( $array_to_encode )
132   - {
133   - return array_map(function($array_to_encode) {
134   - return iconv( $this->in_charset, $this->out_charset, $array_to_encode );
135   - }, $array_to_encode);
  111 + return $clear_arr;
136 112  
137 113 }
138 114  
  115 +// private function encodeArray( $array_to_encode )
  116 +// {
  117 +// return array_map(function($array_to_encode) {
  118 +// return iconv( $this->in_charset, $this->out_charset, $array_to_encode );
  119 +// }, $array_to_encode);
  120 +//
  121 +// }
  122 +
139 123  
140 124 }
141 125 \ No newline at end of file
... ...
backend/components/parsers/Encoder.php 0 → 100644
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Cibermag
  5 + * Date: 27.08.2015
  6 + * Time: 13:36
  7 + */
  8 +
  9 +namespace app\components\parsers;
  10 +
  11 +// @todo add comments
  12 +class Encoder
  13 +{
  14 + public static $in_charset;
  15 + public static $out_charset;
  16 +
  17 +
  18 + public static function encodeFile($in_charset, $out_charset, $filePath)
  19 + {
  20 +
  21 + $old_content = file_get_contents($filePath);
  22 + $encode_content = self::encode( $in_charset, $out_charset, $old_content );
  23 + $file = @fopen($filePath, "w");
  24 + fwrite($file, $encode_content);
  25 + @fclose($file);
  26 + }
  27 +
  28 + public static function encodeArray($in_charset, $out_charset, $array)
  29 + {
  30 +
  31 + self::$in_charset = $in_charset;
  32 + self::$out_charset = $out_charset;
  33 +
  34 + $result = array_map(
  35 + function ( $array) {
  36 +
  37 + return self::encode( self::$in_charset, self::$out_charset, $array );
  38 +
  39 + },
  40 + $array);
  41 +
  42 + return $result;
  43 + }
  44 +
  45 + private static function encode( $in_charset, $out_charset, $source ){
  46 +
  47 + return iconv($in_charset, $out_charset, $source);
  48 +
  49 + }
  50 +}
0 51 \ No newline at end of file
... ...
backend/controllers/ParserController.php
... ... @@ -63,7 +63,7 @@ class ParserController extends Controller
63 63 $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension;
64 64 $model->file->saveAs( $filePath );
65 65  
66   - $parser = new ParserHandler( $filePath, 0 );
  66 + $parser = new ParserHandler( $filePath, 2 );
67 67 $data = $parser->run();
68 68  
69 69 if( !is_array($data) ){
... ...