Commit 37656b1f760de045200fabbde7a096dc639bfe87
1 parent
40ff24a1
add parser interface and Parser configurator
Showing
4 changed files
with
48 additions
and
15 deletions
Show diff stats
backend/components/parsers/CsvParser.php
| @@ -10,7 +10,7 @@ use Yii; | @@ -10,7 +10,7 @@ use Yii; | ||
| 10 | use yii\base\ErrorException; | 10 | use yii\base\ErrorException; |
| 11 | use common\components\debug\CustomVarDamp; | 11 | use common\components\debug\CustomVarDamp; |
| 12 | 12 | ||
| 13 | -class CsvParser | 13 | +class CsvParser implements ParserInterface |
| 14 | { | 14 | { |
| 15 | 15 | ||
| 16 | 16 |
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * Created by PhpStorm. | ||
| 4 | + * User: Cibermag | ||
| 5 | + * Date: 04.09.2015 | ||
| 6 | + * Time: 18:17 | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | +namespace backend\components\parsers; | ||
| 10 | + | ||
| 11 | + | ||
| 12 | +class ParserConfigurator { | ||
| 13 | + public static function getConfiguration () | ||
| 14 | + { | ||
| 15 | + return [ | ||
| 16 | + 'class' => 'backend\components\parsers\CustomCsvParser', | ||
| 17 | + // 'file' => $this->fileObject, | ||
| 18 | + 'auto_detect_first_line' => true, | ||
| 19 | + ]; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | +} | ||
| 0 | \ No newline at end of file | 23 | \ No newline at end of file |
backend/components/parsers/ParserHandler.php
| @@ -17,15 +17,15 @@ class ParserHandler { | @@ -17,15 +17,15 @@ class ParserHandler { | ||
| 17 | private $extension; | 17 | private $extension; |
| 18 | 18 | ||
| 19 | /** @var string - extension of file $filePath */ | 19 | /** @var string - extension of file $filePath */ |
| 20 | - private $options; | 20 | + private $mode; |
| 21 | 21 | ||
| 22 | /** | 22 | /** |
| 23 | * @param string first line in file for parsing | 23 | * @param string first line in file for parsing |
| 24 | */ | 24 | */ |
| 25 | - public function __construct( $filePath, $options ) | 25 | + public function __construct( $filePath, $mode ) |
| 26 | { | 26 | { |
| 27 | $this->filePath = $filePath; | 27 | $this->filePath = $filePath; |
| 28 | - $this->options = $options; | 28 | + $this->mode = $mode; |
| 29 | 29 | ||
| 30 | try { | 30 | try { |
| 31 | $this->fileObject = new \SplFileObject( $this->filePath , 'r' );; | 31 | $this->fileObject = new \SplFileObject( $this->filePath , 'r' );; |
| @@ -38,17 +38,10 @@ class ParserHandler { | @@ -38,17 +38,10 @@ class ParserHandler { | ||
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | public function run(){ | 40 | public function run(){ |
| 41 | - if ($this->extension = 'csv'){ | ||
| 42 | 41 | ||
| 43 | - $csvParser = Yii::createObject([ | ||
| 44 | - 'class' => 'backend\components\parsers\CustomCsvParser', | ||
| 45 | - 'file' => $this->fileObject, | ||
| 46 | - 'auto_detect_first_line' => true, | ||
| 47 | - ]); | ||
| 48 | - | ||
| 49 | - $csvParser->setup(); | ||
| 50 | - | ||
| 51 | - return $csvParser->read(); | ||
| 52 | - }; | 42 | + $parser = Yii::createObject( ParserConfigurator::getConfiguration() ); |
| 43 | + $parser->setup(); | ||
| 44 | + return $parser->read(); | ||
| 53 | } | 45 | } |
| 54 | } | 46 | } |
| 47 | + |
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * Created by PhpStorm. | ||
| 4 | + * User: Cibermag | ||
| 5 | + * Date: 04.09.2015 | ||
| 6 | + * Time: 18:25 | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | +namespace backend\components\parsers; | ||
| 10 | + | ||
| 11 | + | ||
| 12 | +interface ParserInterface { | ||
| 13 | + public function setup(); | ||
| 14 | + | ||
| 15 | + public function read(); | ||
| 16 | + | ||
| 17 | + | ||
| 18 | +} | ||
| 0 | \ No newline at end of file | 19 | \ No newline at end of file |