1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: execut |
4
|
|
|
* Date: 26.07.16 |
5
|
|
|
* Time: 15:43 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace execut\import\components\parser; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use execut\import\components\Parser; |
12
|
|
|
use yii\base\Component; |
13
|
|
|
|
14
|
|
|
class Stack extends Component |
15
|
|
|
{ |
16
|
|
|
protected $parsers = []; |
17
|
|
|
public $relations = []; |
18
|
2 |
|
public function setParsers($parsers) { |
19
|
2 |
|
foreach ($parsers as $key => $parser) { |
20
|
2 |
|
if (is_array($parser)) { |
21
|
|
|
$parser = new Parser($parser); |
22
|
|
|
} |
23
|
|
|
|
24
|
2 |
|
$parser->stack = $this; |
25
|
2 |
|
$parsers[$key] = $parser; |
26
|
|
|
} |
27
|
|
|
|
28
|
2 |
|
$this->parsers = $parsers; |
29
|
2 |
|
} |
30
|
|
|
|
31
|
1 |
|
public function parse() { |
32
|
1 |
|
$this->results = []; |
33
|
1 |
|
foreach (array_keys($this->parsers) as $parserKey) { |
34
|
1 |
|
$this->parseModel($parserKey); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
$result = $this->results; |
38
|
|
|
|
39
|
1 |
|
return $result; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected $results = []; |
43
|
1 |
|
public function parseModel($parserKey) { |
44
|
1 |
|
if (isset($this->results[$parserKey])) { |
45
|
1 |
|
return $this->results[$parserKey]; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
$relations = []; |
49
|
1 |
|
if (!empty($this->relations[$parserKey])) { |
50
|
1 |
|
foreach ($this->relations[$parserKey] as $relationAttribute => $relationAdapter) { |
51
|
1 |
|
if (is_int($relationAttribute)) { |
52
|
|
|
$relationAttribute = $relationAdapter; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
$relationModel = $this->parseModel($relationAdapter); |
56
|
1 |
|
$relations[$relationAttribute] = $relationModel->id; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$parser = $this->parsers[$parserKey]; |
61
|
1 |
|
$parserAttributes = array_merge($relations, $parser->attributes); |
62
|
1 |
|
$parser->attributes = $parserAttributes; |
63
|
1 |
|
$result = $parser->parse(); |
64
|
1 |
|
$model = $result->getModel(); |
65
|
1 |
|
foreach ($relations as $relation => $value) { |
66
|
1 |
|
$model->$relation = $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
if ($model->isNewRecord || $parser->modelsFinder->isUpdateAlways) { |
70
|
1 |
|
$model->save(false); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return $this->results[$parserKey] = $model; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function setRow($row) { |
77
|
1 |
|
foreach ($this->parsers as $parser) { |
78
|
1 |
|
$parser->row = $row; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |