From f90f5f26a26fb0013f9b1f8f6c1d757b9d838b2e Mon Sep 17 00:00:00 2001 From: Mihail Date: Fri, 4 Sep 2015 18:29:01 +0300 Subject: [PATCH] add parser interface and Parser configurator --- backend/components/parsers/CsvParser.php | 2 +- backend/components/parsers/ParserConfigurator.php | 22 ++++++++++++++++++++++ backend/components/parsers/ParserHandler.php | 21 +++++++-------------- backend/components/parsers/ParserInterface.php | 18 ++++++++++++++++++ 4 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 backend/components/parsers/ParserConfigurator.php create mode 100644 backend/components/parsers/ParserInterface.php diff --git a/backend/components/parsers/CsvParser.php b/backend/components/parsers/CsvParser.php index d396772..66b9b73 100644 --- a/backend/components/parsers/CsvParser.php +++ b/backend/components/parsers/CsvParser.php @@ -10,7 +10,7 @@ use Yii; use yii\base\ErrorException; use common\components\debug\CustomVarDamp; -class CsvParser +class CsvParser implements ParserInterface { diff --git a/backend/components/parsers/ParserConfigurator.php b/backend/components/parsers/ParserConfigurator.php new file mode 100644 index 0000000..7908a8c --- /dev/null +++ b/backend/components/parsers/ParserConfigurator.php @@ -0,0 +1,22 @@ + 'backend\components\parsers\CustomCsvParser', + // 'file' => $this->fileObject, + 'auto_detect_first_line' => true, + ]; + } + +} \ No newline at end of file diff --git a/backend/components/parsers/ParserHandler.php b/backend/components/parsers/ParserHandler.php index cf97abb..cd59c48 100644 --- a/backend/components/parsers/ParserHandler.php +++ b/backend/components/parsers/ParserHandler.php @@ -17,15 +17,15 @@ class ParserHandler { private $extension; /** @var string - extension of file $filePath */ - private $options; + private $mode; /** * @param string first line in file for parsing */ - public function __construct( $filePath, $options ) + public function __construct( $filePath, $mode ) { $this->filePath = $filePath; - $this->options = $options; + $this->mode = $mode; try { $this->fileObject = new \SplFileObject( $this->filePath , 'r' );; @@ -38,17 +38,10 @@ class ParserHandler { } public function run(){ - if ($this->extension = 'csv'){ - $csvParser = Yii::createObject([ - 'class' => 'backend\components\parsers\CustomCsvParser', - 'file' => $this->fileObject, - 'auto_detect_first_line' => true, - ]); - - $csvParser->setup(); - - return $csvParser->read(); - }; + $parser = Yii::createObject( ParserConfigurator::getConfiguration() ); + $parser->setup(); + return $parser->read(); } } + diff --git a/backend/components/parsers/ParserInterface.php b/backend/components/parsers/ParserInterface.php new file mode 100644 index 0000000..46db651 --- /dev/null +++ b/backend/components/parsers/ParserInterface.php @@ -0,0 +1,18 @@ +