Commit 2501a7522b4343bc358ab46abcd5a06325bc4fe5
1 parent
54ada04a
add value filter class
Showing
6 changed files
with
65 additions
and
19 deletions
Show diff stats
backend/components/parsers/CsvParser.php
| ... | ... | @@ -6,10 +6,12 @@ |
| 6 | 6 | * Time: 17:00 |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | -namespace backend\components\parsers; | |
| 9 | +namespace backend\components; | |
| 10 | + | |
| 10 | 11 | |
| 11 | 12 | use Yii; |
| 12 | 13 | use yii\base\ErrorException; |
| 14 | +use common\components\debug\CustomVarDamp; | |
| 13 | 15 | |
| 14 | 16 | class CsvParser implements \IteratorAggregate { |
| 15 | 17 | |
| ... | ... | @@ -30,6 +32,9 @@ class CsvParser implements \IteratorAggregate { |
| 30 | 32 | /** @var int - first column for parsing */ |
| 31 | 33 | private $first_column; |
| 32 | 34 | |
| 35 | + /** @var array - array of headers values */ | |
| 36 | + private $keys; | |
| 37 | + | |
| 33 | 38 | public function setup( $file, $first_line, $first_column, $hasHeaderRow = TRUE, $delimiter = ';') |
| 34 | 39 | { |
| 35 | 40 | |
| ... | ... | @@ -72,23 +77,23 @@ class CsvParser implements \IteratorAggregate { |
| 72 | 77 | $return = []; |
| 73 | 78 | |
| 74 | 79 | $line = 0; |
| 75 | - $keys = NULL; | |
| 80 | + $this->keys = NULL; | |
| 76 | 81 | |
| 77 | 82 | while (($row = $this->readRow()) !== FALSE) { |
| 78 | 83 | $line++; |
| 79 | 84 | |
| 80 | 85 | if ($this->hasHeaderRow) { |
| 81 | - if ($keys === NULL) { | |
| 82 | - $keys = array_values($row); | |
| 86 | + if ($this->keys === NULL) { | |
| 87 | + $this->keys = array_values($row); | |
| 83 | 88 | } else { |
| 84 | 89 | |
| 85 | - if (count($keys) !== count($row)) { | |
| 90 | + if (count($this->keys) !== count($row)) { | |
| 86 | 91 | // |
| 87 | 92 | Yii::warning("Invalid columns detected on line #$line ."); |
| 88 | 93 | return $return; |
| 89 | 94 | } |
| 90 | 95 | |
| 91 | - $return[] = array_combine($keys, $row); | |
| 96 | + $return[] = array_combine($this->keys, $row); | |
| 92 | 97 | } |
| 93 | 98 | } else { |
| 94 | 99 | $return[] = $row; |
| ... | ... | @@ -112,17 +117,13 @@ class CsvParser implements \IteratorAggregate { |
| 112 | 117 | $dirt_value_arr = $this->file->fgetcsv( ); |
| 113 | 118 | $dirt_value_arr = array_slice( $dirt_value_arr, $this->first_column ); |
| 114 | 119 | $clear_arr = Encoder::encodeArray( $this->in_charset, $this->out_charset, $dirt_value_arr ); |
| 120 | + | |
| 121 | +// if ($this->keys !== NULL) | |
| 122 | +// @$clear_arr[3] = ValueFilter::pricefilter($clear_arr[3]); | |
| 123 | + | |
| 115 | 124 | return $clear_arr; |
| 116 | 125 | |
| 117 | 126 | } |
| 118 | 127 | |
| 119 | -// private function encodeArray( $array_to_encode ) | |
| 120 | -// { | |
| 121 | -// return array_map(function($array_to_encode) { | |
| 122 | -// return iconv( $this->in_charset, $this->out_charset, $array_to_encode ); | |
| 123 | -// }, $array_to_encode); | |
| 124 | -// | |
| 125 | -// } | |
| 126 | - | |
| 127 | 128 | |
| 128 | 129 | } |
| 129 | 130 | \ No newline at end of file | ... | ... |
backend/components/parsers/Encoder.php
backend/components/parsers/ParserHandler.php
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Created by PhpStorm. | |
| 4 | + * User: Cibermag | |
| 5 | + * Date: 31.08.2015 | |
| 6 | + * Time: 12:50 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +namespace backend\components; | |
| 10 | + | |
| 11 | +// класс который содержит преобразователи значений (фильтры) используемые при парсинге | |
| 12 | +class ValueFilter | |
| 13 | +{ | |
| 14 | + | |
| 15 | + public static function pricefilter($str) | |
| 16 | + { | |
| 17 | + if ($str == '') { | |
| 18 | + $str = 0; | |
| 19 | + } | |
| 20 | + $str = trim(str_replace(",", ".", $str)); | |
| 21 | + $str = preg_replace("/[^0-9.]+/", "", strtoupper($str)); | |
| 22 | + | |
| 23 | + if ($str == '') { | |
| 24 | + return ''; | |
| 25 | + } | |
| 26 | + $str = round( (float)$str, 2 ); | |
| 27 | + | |
| 28 | + return $str; | |
| 29 | + } | |
| 30 | + | |
| 31 | + public static function boxfilter($str) | |
| 32 | + { | |
| 33 | + if ($str == '') { | |
| 34 | + $str = 0; | |
| 35 | + } | |
| 36 | + $str = trim(str_replace(",", ".", $str)); | |
| 37 | + $str = preg_replace("/[^0-9.]+/", "", strtoupper($str)); | |
| 38 | + if ($str == '') { | |
| 39 | + return ''; | |
| 40 | + } | |
| 41 | + $str = round((int)$str, 2); | |
| 42 | + | |
| 43 | + return $str; | |
| 44 | + } | |
| 45 | +} | |
| 0 | 46 | \ No newline at end of file | ... | ... |
backend/controllers/SiteController.php
backend/models/UploadFileParsingForm.php
| ... | ... | @@ -3,7 +3,7 @@ namespace backend\models; |
| 3 | 3 | |
| 4 | 4 | use yii\base\Model; |
| 5 | 5 | use yii\web\UploadedFile; |
| 6 | -use backend\components\parsers\ParserHandler; | |
| 6 | +use backend\components\ParserHandler; | |
| 7 | 7 | use Yii; |
| 8 | 8 | |
| 9 | 9 | /** |
| ... | ... | @@ -24,7 +24,7 @@ class UploadFileParsingForm extends Model |
| 24 | 24 | public function rules() |
| 25 | 25 | { |
| 26 | 26 | return [ |
| 27 | - [['file'], 'file'], //'extensions' => ['csv', 'xml'] ], | |
| 27 | + [['file'], 'file', 'extensions' => ['csv', 'xml'] ], | |
| 28 | 28 | ['first_line', 'integer'], |
| 29 | 29 | ['first_column', 'integer'] |
| 30 | 30 | ]; | ... | ... |