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 function getTagProperties() { |
23
|
|
|
return []; |
24
|
|
|
} |
25
|
|
|
|
26
|
9 |
|
/** |
27
|
|
|
* @return string[] |
28
|
9 |
|
*/ |
29
|
|
|
public function getYmlAttributes() |
30
|
|
|
{ |
31
|
|
|
return $this->attributes(); |
32
|
|
|
} |
33
|
|
|
|
34
|
9 |
|
/** |
35
|
|
|
* @return string |
36
|
9 |
|
*/ |
37
|
|
|
public function getYml() |
38
|
9 |
|
{ |
39
|
9 |
|
$string = ''; |
40
|
9 |
|
|
41
|
|
|
$string .= $this->getYmlStartTag(); |
42
|
9 |
|
$string .= $this->getYmlBody(); |
43
|
|
|
$string .= $this->getYmlEndTag() . PHP_EOL; |
44
|
|
|
|
45
|
|
|
return $string; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $params |
50
|
|
|
*/ |
51
|
|
|
public function setParams(array $params) |
52
|
|
|
{ |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $pictures |
58
|
|
|
*/ |
59
|
|
|
public function setPictures(array $pictures) |
60
|
|
|
{ |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
9 |
|
/** |
65
|
|
|
* @param array $options |
66
|
9 |
|
*/ |
67
|
9 |
|
public function setDeliveryOptions(array $options) |
|
|
|
|
68
|
9 |
|
{ |
69
|
6 |
|
|
70
|
|
|
} |
71
|
9 |
|
|
72
|
|
|
/** |
73
|
|
|
* @param $data |
74
|
|
|
* @param null $onValidationError |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
9 |
|
* @throws Exception |
78
|
|
|
*/ |
79
|
9 |
|
public function loadAndValidate($data, $onValidationError = null) |
80
|
9 |
|
{ |
81
|
9 |
|
$this->load($data, ''); |
82
|
6 |
|
|
83
|
|
|
if (!$this->validate()) { |
84
|
9 |
|
if (is_callable($onValidationError)) { |
85
|
|
|
$onValidationError($this); |
86
|
|
|
return false; |
87
|
|
|
} else { |
88
|
|
|
throw new Exception('Model values is invalid ' . serialize($this->getErrors())); |
89
|
|
|
} |
90
|
9 |
|
} |
91
|
|
|
|
92
|
9 |
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $valuesModel |
97
|
|
|
* @param null $onValidationError |
98
|
9 |
|
* |
99
|
|
|
* @return bool |
100
|
9 |
|
* @throws Exception |
101
|
9 |
|
*/ |
102
|
|
|
public function loadModel($valuesModel, $onValidationError = null) |
103
|
9 |
|
{ |
104
|
9 |
|
if (is_array($valuesModel)) { |
105
|
9 |
|
$valuesModel = \yii::createObject($valuesModel); |
106
|
9 |
|
} |
107
|
6 |
|
|
108
|
6 |
|
$attributes = []; |
109
|
|
|
foreach ($this->attributes() as $attribute) { |
110
|
9 |
|
$methodName = 'get' . ucfirst($attribute); |
111
|
|
|
$attributeValue = $valuesModel->$methodName(); |
112
|
|
|
|
113
|
|
|
$attributes[$attribute] = $attributeValue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->load($attributes, ''); |
117
|
9 |
|
|
118
|
|
|
if (!$this->validate()) { |
119
|
9 |
|
$e = new Exception('Model ' . get_class($this) . ' values is invalid ' . var_export($this->getErrors(), true) . ' values: ' . var_export($attributes, true)); |
120
|
|
|
if (is_callable($onValidationError)) { |
121
|
|
|
$onValidationError($this, $e); |
122
|
|
|
return false; |
123
|
|
|
} else { |
124
|
|
|
throw $e; |
125
|
|
|
} |
126
|
9 |
|
} |
127
|
|
|
|
128
|
9 |
|
return true; |
129
|
9 |
|
} |
130
|
9 |
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
9 |
|
*/ |
134
|
|
View Code Duplication |
protected function getYmlStartTag() |
|
|
|
|
135
|
9 |
|
{ |
136
|
|
|
$string = ''; |
137
|
|
|
if (static::$tag) { |
138
|
|
|
$string = '<' . static::$tag . $this->getYmlTagProperties() . '>'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $string; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
protected function getYmlEndTag() |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$string = ''; |
150
|
|
|
if (static::$tag) { |
151
|
|
|
$string .= '</' . static::$tag . '>'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $string; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
protected function getYmlBody() |
161
|
|
|
{ |
162
|
|
|
return $this->name; |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
protected function getYmlTagProperties() |
169
|
|
|
{ |
170
|
|
|
$string = ''; |
171
|
|
|
$properties = static::getTagProperties(); |
172
|
|
|
|
173
|
|
|
foreach ($properties as $property) { |
174
|
|
|
$value = $this->getAttributeValue($property); |
175
|
|
|
if ($value !== null) { |
176
|
|
|
$string .= ' ' . $property . '="' . $value . '"'; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $string; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $attribute |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
protected function getAttributeValue($attribute) |
188
|
|
|
{ |
189
|
|
|
return $this->$attribute; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $attribute |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
protected function getYmlAttribute($attribute) |
197
|
|
|
{ |
198
|
|
|
$value = $this->getAttributeValue($attribute); |
199
|
|
|
if ($value === null) { |
200
|
|
|
return ''; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
if (($key = array_search($attribute, $this->getYmlAttributes())) !== false && is_string($key)) { |
205
|
|
|
$attribute = $key; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$string = '<' . $attribute . '>' . $value . '</' . $attribute. '>' . PHP_EOL; |
209
|
|
|
|
210
|
|
|
return $string; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.