Commit e9ecd908e44810cfc65148a1621f5a04adc15239
0 parents
init commit
Showing
7 changed files
with
253 additions
and
0 deletions
Show diff stats
1 | +++ a/LICENSE | |
1 | +Copyright (c) 2015 Mihail Tsurkanov | |
2 | + | |
3 | +Permission is hereby granted, free of charge, to any person obtaining a copy | |
4 | +of this software and associated documentation files (the "Software"), to deal | |
5 | +in the Software without restriction, including without limitation the rights | |
6 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
7 | +copies of the Software, and to permit persons to whom the Software is furnished | |
8 | +to do so, subject to the following conditions: | |
9 | + | |
10 | +The above copyright notice and this permission notice shall be included in all | |
11 | +copies or substantial portions of the Software. | |
12 | + | |
13 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
14 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
15 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
16 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
17 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
18 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
19 | +THE SOFTWARE. | ... | ... |
1 | +++ a/composer.json | |
1 | +{ | |
2 | + "name": "artweb/yii-multiparser", | |
3 | + "type": "library", | |
4 | + "description": "Flexible bunch of parsers for PHP 5.3", | |
5 | + "keywords": [ "yii", "parser", "csv", "xml" ], | |
6 | + "homepage": "https://github.com/tsurkanovm/multiparser.git", | |
7 | + "license": "MIT", | |
8 | + "authors": [ | |
9 | + { | |
10 | + "name": "Mihail Tsurkanov", | |
11 | + "email": "tsurkanovm@gmail.com", | |
12 | + "homepage": "http://samoinvestor.ru", | |
13 | + "role": "Developer" | |
14 | + } | |
15 | + ], | |
16 | + "require": { | |
17 | + "yiisoft/yii2": "*", | |
18 | + "artweb/multiparser": "0.5.*" | |
19 | + }, | |
20 | + "autoload": { | |
21 | + "psr-4": { | |
22 | + "yii\\multiparser\\": "" | |
23 | + } | |
24 | + } | |
25 | +} | ... | ... |
1 | +++ a/lib/DynamicFormHelper.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Created by PhpStorm. | |
4 | + * User: Cibermag | |
5 | + * Date: 08.09.2015 | |
6 | + * Time: 14:50 | |
7 | + */ | |
8 | + | |
9 | +namespace yii\multiparser; | |
10 | + | |
11 | +use yii\base\DynamicModel; | |
12 | +use yii\grid\GridView; | |
13 | +use yii\grid\SerialColumn; | |
14 | +use yii\helpers\ArrayHelper; | |
15 | + | |
16 | +/** | |
17 | + * Class DynamicFormHelper | |
18 | + * @package backend\components\parsers | |
19 | + * Содержит процедуры генерации компонентов с динамическим количеством аттрибутов | |
20 | + */ | |
21 | +class DynamicFormHelper | |
22 | +{ | |
23 | + | |
24 | + const KEY_PREFIX = 'attr_'; | |
25 | + | |
26 | + /** | |
27 | + * @param $source - int or array | |
28 | + * если передан массив, то создается модель с атрибутами переданными в массиве, | |
29 | + * ключ - имя, значение - значение аттрибута | |
30 | + * если передано число, то создается переданное количество аттрибутов с именами - attr_0, attr_1... | |
31 | + */ | |
32 | + public static function CreateDynamicModel( $source ) | |
33 | + { | |
34 | + $arr_keys = []; | |
35 | + if (is_array($source)) { | |
36 | + $arr_keys = $source; | |
37 | + } elseif (is_int($source)) { | |
38 | + | |
39 | + $i = 0; | |
40 | + while ($source > $i) { | |
41 | + $arr_keys[] = self::KEY_PREFIX . $i; | |
42 | + $i++; | |
43 | + } | |
44 | + array_flip($arr_keys); | |
45 | + | |
46 | + } | |
47 | + | |
48 | + $model = new DynamicModel($arr_keys); | |
49 | + | |
50 | + return $model; | |
51 | + } | |
52 | + | |
53 | + // @todo add comments | |
54 | + public static function CreateGridWithDropDownListHeader( $dataProvider, $form, $header_model, $arr_header_values ) | |
55 | + { | |
56 | + $columns_config = [['class' => SerialColumn::className()]]; | |
57 | + $i = 0; | |
58 | + foreach( $header_model as $key => $value ) { | |
59 | + | |
60 | + $columns_config[] = ['header' => $form->field($header_model, $key, ['inputOptions' => ['label' => '']])->dropDownList($arr_header_values), 'attribute' => $i]; | |
61 | + $i++; | |
62 | + } | |
63 | + $dynamic_grid_view = GridView::widget( ['dataProvider' => $dataProvider, | |
64 | + 'columns' => $columns_config ] ); | |
65 | + | |
66 | + return $dynamic_grid_view; | |
67 | + | |
68 | + } | |
69 | + | |
70 | +} | |
0 | 71 | \ No newline at end of file | ... | ... |
1 | +++ a/lib/YiiMultiparser.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Created by PhpStorm. | |
4 | + * User: Cibermag | |
5 | + * Date: 07.09.2015 | |
6 | + * Time: 15:56 | |
7 | + */ | |
8 | + | |
9 | +namespace yii\multiparser; | |
10 | + | |
11 | +use common\components\CustomVarDamp; | |
12 | +use yii\base\Component; | |
13 | + | |
14 | + | |
15 | + | |
16 | + | |
17 | +class YiiMultiparser extends Component{ | |
18 | + | |
19 | +public $configuration; | |
20 | + | |
21 | + public function getConfiguration($extension, $conf_parameter){ | |
22 | + | |
23 | + if (!isset( $this->configuration[$extension] )){ | |
24 | + throw new \ErrorException( "Parser do not maintain file with extension {$extension}"); | |
25 | + } | |
26 | + if (!isset( $this->configuration[$extension][$conf_parameter] )){ | |
27 | + throw new \ErrorException( "Parser configurator do not have settings for {$conf_parameter} parameter"); | |
28 | + } | |
29 | + | |
30 | + return $this->configuration[$extension][$conf_parameter]; | |
31 | + | |
32 | + } | |
33 | + | |
34 | + | |
35 | + public function parse( $filePath, $options = [] ){ | |
36 | + | |
37 | + $parser = new YiiParserHandler( $filePath, $options ); | |
38 | + return $parser->run(); | |
39 | + | |
40 | + } | |
41 | + | |
42 | + | |
43 | +} | |
0 | 44 | \ No newline at end of file | ... | ... |
1 | +++ a/lib/YiiParserHandler.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Created by PhpStorm. | |
4 | + * User: Cibermag | |
5 | + * Date: 07.09.2015 | |
6 | + * Time: 15:53 | |
7 | + */ | |
8 | + | |
9 | +namespace yii\multiparser; | |
10 | + | |
11 | + | |
12 | +use common\components\CustomVarDamp; | |
13 | + | |
14 | +class YiiParserHandler extends ParserHandler{ | |
15 | + | |
16 | + | |
17 | + /** | |
18 | + * @param $filePath | |
19 | + * @param array $options | |
20 | + * проверяет читабенльность переданного файла, а также наличие настроек парсера в конфигурационном файле для данного типа файла | |
21 | + */ | |
22 | + public function __construct($filePath, $options = []) | |
23 | + { | |
24 | + $this->filePath = $filePath; | |
25 | + if (isset($options['mode'])) { | |
26 | + | |
27 | + $this->mode = $options['mode']; | |
28 | + unset($options['mode']); | |
29 | + | |
30 | + } else { | |
31 | + | |
32 | + $this->mode = self::DEFAULT_MODE; | |
33 | + | |
34 | + } | |
35 | + | |
36 | + $this->options = $options; | |
37 | + | |
38 | + try { | |
39 | + $this->fileObject = new \SplFileObject($this->filePath, 'r'); | |
40 | + } catch (\ErrorException $e) { | |
41 | + // Yii::warning("Ошибка открытия файла {$this->filePath}"); | |
42 | + echo "Ошибка открытия файла {$this->filePath}"; | |
43 | + return []; | |
44 | + } | |
45 | + | |
46 | + $options['file'] = $this->fileObject; | |
47 | + $this->extension = $this->fileObject->getExtension(); | |
48 | + | |
49 | + try { | |
50 | + $this->configuration = \Yii::$app->multiparser->getConfiguration($this->extension, $this->mode); | |
51 | + $this->configuration = array_merge_recursive ($this->configuration, $options); | |
52 | + } catch (\ErrorException $e) { | |
53 | + echo $e->getMessage(); | |
54 | + return []; | |
55 | + } | |
56 | + | |
57 | + } | |
58 | + | |
59 | + public function run() | |
60 | + { | |
61 | + | |
62 | + $result = []; | |
63 | + | |
64 | + // \common\components\CustomVarDamp::dumpAndDie($this); | |
65 | + if (count($this->configuration)) { | |
66 | + $parser = \Yii::createObject($this->configuration); | |
67 | + | |
68 | + try { | |
69 | + | |
70 | + $parser->setup(); | |
71 | + $result = $parser->read(); | |
72 | + | |
73 | + } catch (\ErrorException $e) { | |
74 | + | |
75 | + echo $e->getMessage(); | |
76 | + | |
77 | + } | |
78 | + | |
79 | + } | |
80 | + | |
81 | + return $result; | |
82 | + } | |
83 | + | |
84 | +} | |
0 | 85 | \ No newline at end of file | ... | ... |