|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Parser Object Param |
|
5
|
|
|
* |
|
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
|
7
|
|
|
* @link http://inji.ru/ |
|
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
|
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Migrations\Parser\Object; |
|
13
|
|
|
|
|
14
|
|
|
class Param extends \Migrations\Parser |
|
15
|
|
|
{ |
|
16
|
|
|
public function parse() |
|
17
|
|
|
{ |
|
18
|
|
|
$params = \Migrations\Migration\Object\Param::getList(['where' => [ |
|
19
|
|
|
['parent_id', $this->param->id], |
|
20
|
|
|
['object_id', $this->object->object->id], |
|
21
|
|
|
]]); |
|
22
|
|
|
if (!\Tools::isAssoc($this->data)) { |
|
23
|
|
|
foreach ($this->data as &$data) { |
|
24
|
|
|
$this->parseData($data, $params); |
|
25
|
|
|
} |
|
26
|
|
|
} else { |
|
27
|
|
|
$this->parseData($this->data, $params); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private function parseData(&$data, $params) |
|
32
|
|
|
{ |
|
33
|
|
|
$objectParamValue = [ |
|
34
|
|
|
'col' => '', |
|
35
|
|
|
'value' => '' |
|
36
|
|
|
]; |
|
37
|
|
|
$walked = []; |
|
38
|
|
|
foreach ($params as $param) { |
|
39
|
|
|
$objectParam = $data[$param->code]; |
|
40
|
|
|
if ($this->model && $param->type) { |
|
41
|
|
|
switch ($param->type) { |
|
42
|
|
|
case 'paramName': |
|
43
|
|
|
$col = \Migrations\Migration\Object\Param\Value::get([['original', (string) $objectParam], ['param_id', $param->id]]); |
|
44
|
|
View Code Duplication |
if (!$col) { |
|
|
|
|
|
|
45
|
|
|
$valueObject = new \Migrations\Migration\Object\Param\Value(); |
|
46
|
|
|
$valueObject->param_id = $param->id; |
|
47
|
|
|
$valueObject->original = (string) $objectParam; |
|
48
|
|
|
$valueObject->save(); |
|
49
|
|
|
} else { |
|
50
|
|
|
$objectParamValue['col'] = $col->replace; |
|
51
|
|
|
} |
|
52
|
|
|
break; |
|
53
|
|
|
case 'paramValue': |
|
54
|
|
|
if ($objectParamValue['col']) { |
|
55
|
|
|
$modelName = get_class($this->model); |
|
56
|
|
|
$col = $modelName::$cols[$objectParamValue['col']]; |
|
57
|
|
|
if ($col['type'] == 'select' && $col['source'] == 'relation') { |
|
58
|
|
|
$relation = $modelName::getRelation($col['relation']); |
|
59
|
|
|
$item = $relation['model']::get((string) $objectParam, 'name'); |
|
60
|
|
|
if (!$item) { |
|
61
|
|
|
$item = new $relation['model']; |
|
62
|
|
|
$item->name = (string) $objectParam; |
|
63
|
|
|
$item->save(); |
|
64
|
|
|
} |
|
65
|
|
|
$objectParamValue['value'] = $item->id; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
break; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
$walked[$param->code] = true; |
|
72
|
|
|
} |
|
73
|
|
|
if ($objectParamValue['col']) { |
|
74
|
|
|
$this->model->{$objectParamValue['col']} = $objectParamValue['value']; |
|
75
|
|
|
} |
|
76
|
|
|
//check unparsed params |
|
77
|
|
View Code Duplication |
foreach ($data as $key => $item) { |
|
|
|
|
|
|
78
|
|
|
//skip parsed and attribtes |
|
79
|
|
|
if ($key == '@attributes' || !empty($walked[$key])) { |
|
80
|
|
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
$param = new \Migrations\Migration\Object\Param(); |
|
83
|
|
|
$param->parent_id = $this->param->id; |
|
84
|
|
|
$param->object_id = $this->object->object->id; |
|
85
|
|
|
$param->code = $key; |
|
86
|
|
|
$param->save(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function editor() |
|
91
|
|
|
{ |
|
92
|
|
|
return [ |
|
93
|
|
|
'' => 'Выберите', |
|
94
|
|
|
'paramName' => 'Название параметра', |
|
95
|
|
|
'paramValue' => 'Значение параметра', |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.