1
|
|
|
<?php |
2
|
|
|
namespace pastuhov\ymlcatalog\models; |
3
|
|
|
|
4
|
|
|
use yii\base\Model; |
5
|
|
|
use yii\base\Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Base model. |
9
|
|
|
* |
10
|
|
|
* @package pastuhov\ymlcatalog\models |
11
|
|
|
*/ |
12
|
|
|
class BaseModel extends Model |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
public static $tag; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string[] |
21
|
|
|
*/ |
22
|
|
|
public static $tagProperties = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return string[] |
26
|
9 |
|
*/ |
27
|
|
|
public function getYmlAttributes() |
28
|
9 |
|
{ |
29
|
|
|
return $this->attributes(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
9 |
|
*/ |
35
|
|
|
public function getYml() |
36
|
9 |
|
{ |
37
|
|
|
$string = ''; |
38
|
9 |
|
|
39
|
9 |
|
$string .= $this->getYmlStartTag(); |
40
|
9 |
|
$string .= $this->getYmlBody(); |
41
|
|
|
$string .= $this->getYmlEndTag() . PHP_EOL; |
42
|
9 |
|
|
43
|
|
|
return $string; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $params |
48
|
|
|
*/ |
49
|
|
|
public function setParams(array $params) |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $pictures |
56
|
|
|
*/ |
57
|
|
|
public function setPictures(array $pictures) |
58
|
|
|
{ |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $options |
64
|
9 |
|
*/ |
65
|
|
|
public function setDeliveryOptions(array $options) |
66
|
9 |
|
{ |
67
|
9 |
|
|
68
|
9 |
|
} |
69
|
6 |
|
|
70
|
|
|
/** |
71
|
9 |
|
* @param $valuesModel |
72
|
|
|
* @param null $onValidationError |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
* @throws Exception |
76
|
|
|
*/ |
77
|
9 |
|
public function loadModel($valuesModel, $onValidationError = null) |
78
|
|
|
{ |
79
|
9 |
|
$attributes = []; |
80
|
9 |
|
foreach ($this->attributes() as $attribute) { |
81
|
9 |
|
$methodName = 'get' . ucfirst($attribute); |
82
|
6 |
|
$attributeValue = $valuesModel->$methodName(); |
83
|
|
|
|
84
|
9 |
|
$attributes[$attribute] = $attributeValue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->load($attributes, ''); |
88
|
|
|
|
89
|
|
|
if (!$this->validate()) { |
90
|
9 |
|
if (is_callable($onValidationError)) { |
91
|
|
|
$onValidationError($this); |
92
|
9 |
|
return false; |
93
|
|
|
} else { |
94
|
|
|
throw new Exception('Model values is invalid ' . serialize($this->getErrors())); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
9 |
|
return true; |
99
|
|
|
} |
100
|
9 |
|
|
101
|
9 |
|
/** |
102
|
|
|
* @return string |
103
|
9 |
|
*/ |
104
|
9 |
View Code Duplication |
protected function getYmlStartTag() |
|
|
|
|
105
|
9 |
|
{ |
106
|
9 |
|
$string = ''; |
107
|
6 |
|
if (static::$tag) { |
108
|
6 |
|
$string = '<' . static::$tag . $this->getYmlTagProperties() . '>'; |
109
|
|
|
} |
110
|
9 |
|
|
111
|
|
|
return $string; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
9 |
View Code Duplication |
protected function getYmlEndTag() |
|
|
|
|
118
|
|
|
{ |
119
|
9 |
|
$string = ''; |
120
|
|
|
if (static::$tag) { |
121
|
|
|
$string .= '</' . static::$tag . '>'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $string; |
125
|
|
|
} |
126
|
9 |
|
|
127
|
|
|
/** |
128
|
9 |
|
* @return string |
129
|
9 |
|
*/ |
130
|
9 |
|
protected function getYmlBody() |
131
|
|
|
{ |
132
|
|
|
return $this->name; |
|
|
|
|
133
|
9 |
|
} |
134
|
|
|
|
135
|
9 |
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
protected function getYmlTagProperties() |
139
|
|
|
{ |
140
|
|
|
$string = ''; |
141
|
|
|
$properties = static::$tagProperties; |
142
|
|
|
|
143
|
|
|
foreach ($properties as $property) { |
144
|
|
|
$value = $this->getAttributeValue($property); |
145
|
|
|
if ($value !== null) { |
146
|
|
|
$string .= ' ' . $property . '="' . $value . '"'; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $string; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $attribute |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
protected function getAttributeValue($attribute) |
158
|
|
|
{ |
159
|
|
|
return $this->$attribute; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $attribute |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
protected function getYmlAttribute($attribute) |
167
|
|
|
{ |
168
|
|
|
$value = $this->getAttributeValue($attribute); |
169
|
|
|
if ($value === null) { |
170
|
|
|
return ''; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$string = '<' . $attribute . '>' . $value . '</' . $attribute. '>' . PHP_EOL; |
174
|
|
|
|
175
|
|
|
return $string; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
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.