1
|
|
|
<?php |
2
|
|
|
namespace pastuhov\ymlcatalog; |
3
|
|
|
|
4
|
|
|
use pastuhov\ymlcatalog\models\BaseModel; |
5
|
|
|
use pastuhov\ymlcatalog\models\Category; |
6
|
|
|
use pastuhov\ymlcatalog\models\Currency; |
7
|
|
|
use pastuhov\ymlcatalog\models\LocalDeliveryCost; |
8
|
|
|
use pastuhov\ymlcatalog\models\Shop; |
9
|
|
|
use pastuhov\ymlcatalog\models\SimpleOffer; |
10
|
|
|
use pastuhov\ymlcatalog\models\DeliveryOption; |
11
|
|
|
use Yii; |
12
|
|
|
use pastuhov\FileStream\BaseFileStream; |
13
|
|
|
use yii\base\Exception; |
14
|
|
|
use yii\data\ActiveDataProvider; |
15
|
|
|
use yii\db\ActiveQuery; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Yml генератор каталога. |
19
|
|
|
* |
20
|
|
|
* @package pastuhov\ymlcatalog |
21
|
|
|
*/ |
22
|
|
|
class YmlCatalog |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var BaseFileStream |
26
|
|
|
*/ |
27
|
|
|
protected $handle; |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $shopClass; |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $currencyClass; |
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $categoryClass; |
40
|
|
|
/** |
41
|
|
|
* @var null|string |
42
|
|
|
*/ |
43
|
|
|
protected $localDeliveryCostClass; |
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $offerClass; |
48
|
|
|
/** |
49
|
|
|
* @var null|string |
50
|
|
|
*/ |
51
|
|
|
protected $date; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var null|callable |
55
|
|
|
*/ |
56
|
|
|
protected $onValidationError; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var null|string |
60
|
|
|
*/ |
61
|
|
|
protected $customOfferClass; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var null|string |
65
|
|
|
*/ |
66
|
|
|
protected $deliveryOptionClass; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param BaseFileStream $handle |
70
|
|
|
* @param string $shopClass class name |
71
|
|
|
* @param string $currencyClass class name |
72
|
|
|
* @param string $categoryClass class name |
73
|
|
|
* @param string $localDeliveryCostClass class name |
74
|
|
|
* @param array $offerClasses |
75
|
|
|
* @param null|string $date |
76
|
|
|
* @param null|callable $onValidationError |
77
|
|
|
* @param null|string $customOfferClass |
78
|
|
|
*/ |
79
|
5 |
|
public function __construct( |
80
|
|
|
BaseFileStream $handle, |
81
|
|
|
$shopClass, |
82
|
|
|
$currencyClass, |
83
|
|
|
$categoryClass, |
84
|
|
|
$localDeliveryCostClass = null, |
85
|
|
|
Array $offerClasses, |
86
|
|
|
$date = null, |
87
|
|
|
$onValidationError = null, |
88
|
|
|
$customOfferClass = null, |
89
|
|
|
$deliveryOptionClass = null |
90
|
|
|
) { |
91
|
5 |
|
$this->handle = $handle; |
92
|
5 |
|
$this->shopClass = $shopClass; |
93
|
5 |
|
$this->currencyClass = $currencyClass; |
94
|
5 |
|
$this->categoryClass = $categoryClass; |
95
|
5 |
|
$this->localDeliveryCostClass = $localDeliveryCostClass; |
96
|
5 |
|
$this->offerClasses = $offerClasses; |
|
|
|
|
97
|
5 |
|
$this->date = $date; |
98
|
5 |
|
$this->onValidationError = $onValidationError; |
99
|
5 |
|
$this->customOfferClass = $customOfferClass; |
100
|
5 |
|
$this->deliveryOptionClass = $deliveryOptionClass; |
101
|
5 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @throws Exception |
105
|
|
|
*/ |
106
|
5 |
|
public function generate() |
107
|
|
|
{ |
108
|
5 |
|
$date = $this->getDate(); |
109
|
|
|
|
110
|
5 |
|
$this->write( |
111
|
5 |
|
'<?xml version="1.0" encoding="utf-8"?>' . PHP_EOL . |
112
|
5 |
|
'<!DOCTYPE yml_catalog SYSTEM "shops.dtd">' . PHP_EOL . |
113
|
5 |
|
'<yml_catalog date="' . $date . '">' . PHP_EOL |
114
|
5 |
|
); |
115
|
|
|
|
116
|
5 |
|
$this->writeTag('shop'); |
117
|
5 |
|
$this->writeModel(new Shop(), new $this->shopClass()); |
118
|
5 |
|
$this->writeTag('currencies'); |
119
|
5 |
|
$this->writeEachModel($this->currencyClass); |
120
|
5 |
|
$this->writeTag('/currencies'); |
121
|
5 |
|
$this->writeTag('categories'); |
122
|
5 |
|
$this->writeEachModel($this->categoryClass); |
123
|
5 |
|
$this->writeTag('/categories'); |
124
|
5 |
|
if($this->deliveryOptionClass) { |
|
|
|
|
125
|
4 |
|
$this->writeTag('delivery-options'); |
126
|
4 |
|
$this->writeModel(new DeliveryOption(), \Yii::createObject($this->deliveryOptionClass)); |
127
|
4 |
|
$this->writeTag('/delivery-options'); |
128
|
4 |
|
} |
129
|
5 |
|
if($this->localDeliveryCostClass) { |
|
|
|
|
130
|
1 |
|
$this->writeModel(new LocalDeliveryCost(), \Yii::createObject($this->localDeliveryCostClass)); |
|
|
|
|
131
|
1 |
|
} |
132
|
5 |
|
$this->writeTag('offers'); |
133
|
5 |
|
foreach ($this->offerClasses as $offerClass) { |
|
|
|
|
134
|
5 |
|
$this->writeEachModel($offerClass); |
135
|
4 |
|
} |
136
|
4 |
|
$this->writeTag('/offers'); |
137
|
4 |
|
$this->writeTag('/shop'); |
138
|
|
|
|
139
|
4 |
|
$this->write('</yml_catalog>'); |
140
|
4 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return null|string |
144
|
|
|
*/ |
145
|
5 |
|
protected function getDate() |
146
|
|
|
{ |
147
|
5 |
|
$date = $this->date; |
148
|
|
|
|
149
|
5 |
|
if ($date === null) { |
150
|
1 |
|
$date = Yii::$app->formatter->asDatetime(new \DateTime(), 'php:Y-m-d H:i'); |
151
|
1 |
|
} |
152
|
|
|
|
153
|
5 |
|
return $date; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $string |
158
|
|
|
* @throws \Exception |
159
|
|
|
*/ |
160
|
5 |
|
protected function write($string) |
161
|
|
|
{ |
162
|
5 |
|
$this->handle->write($string); |
163
|
5 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $string tag name |
167
|
|
|
*/ |
168
|
5 |
|
protected function writeTag($string) |
169
|
|
|
{ |
170
|
5 |
|
$this->write('<' . $string . '>' . PHP_EOL); |
171
|
5 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param BaseModel $model |
175
|
|
|
* @param $valuesModel |
176
|
|
|
* @throws Exception |
177
|
|
|
*/ |
178
|
5 |
|
protected function writeModel(BaseModel $model, $valuesModel) |
179
|
|
|
{ |
180
|
5 |
|
if (method_exists($valuesModel, 'getParams')) { |
181
|
5 |
|
$model->setParams($valuesModel->getParams()); |
182
|
5 |
|
} |
183
|
5 |
|
if (method_exists($valuesModel, 'getPictures')) { |
184
|
5 |
|
$model->setPictures($valuesModel->getPictures()); |
185
|
5 |
|
} |
186
|
5 |
|
if(method_exists($valuesModel, 'getDeliveryOptions')) { |
187
|
5 |
|
$model->setDeliveryOptions($valuesModel->getDeliveryOptions()); |
188
|
5 |
|
} |
189
|
|
|
|
190
|
5 |
|
if($model->loadModel($valuesModel, $this->onValidationError)) { |
|
|
|
|
191
|
5 |
|
$string = $model->getYml(); |
192
|
5 |
|
$this->write($string); |
193
|
5 |
|
} |
194
|
5 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string|array $modelClass class name or yii configuration array. You can also set params: |
198
|
|
|
* `findParams`: array of additional find params; |
199
|
|
|
* `query`: ActiveQuery object to generate yml use already created object; |
200
|
|
|
* `dataProvider`: ActiveDataProvider or true to generate yml with pagination; |
201
|
|
|
*/ |
202
|
5 |
|
protected function writeEachModel($modelClass) |
203
|
|
|
{ |
204
|
|
|
/** |
205
|
|
|
* @var mixed |
206
|
|
|
*/ |
207
|
5 |
|
$findParams = []; |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @var ActiveQuery |
211
|
|
|
*/ |
212
|
5 |
|
$query = null; |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @var ActiveDataProvider |
216
|
|
|
*/ |
217
|
5 |
|
$dataProvider = null; |
218
|
|
|
|
219
|
5 |
|
if (is_array($modelClass)) { |
220
|
4 |
|
foreach (['findParams', 'query', 'dataProvider'] as $name) { |
221
|
4 |
|
if (array_key_exists($name, $modelClass)) { |
222
|
4 |
|
$$name = $modelClass[$name]; |
223
|
4 |
|
unset($modelClass[$name]); |
224
|
4 |
|
} |
225
|
4 |
|
} |
226
|
4 |
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @var BaseFindYmlInterface $class |
230
|
|
|
*/ |
231
|
5 |
|
$class = \Yii::createObject($modelClass); |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @var ReaderInterface |
235
|
|
|
*/ |
236
|
5 |
|
$reader = ReaderFactory::build( |
237
|
5 |
|
$class, |
238
|
5 |
|
$dataProvider, |
239
|
5 |
|
$query, |
240
|
|
|
$findParams |
241
|
5 |
|
); |
242
|
|
|
|
243
|
5 |
|
$newModel = $this->getNewModel($class); |
244
|
|
|
|
245
|
5 |
|
foreach ($reader as $models) { |
246
|
5 |
|
foreach ($models as $model) { |
247
|
5 |
|
$this->writeModel($newModel, $model); |
248
|
5 |
|
} |
249
|
5 |
|
$this->gc(); |
250
|
5 |
|
} |
251
|
5 |
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param $modelClass |
255
|
|
|
* @return Category|Currency|SimpleOffer |
256
|
|
|
* @throws Exception |
257
|
|
|
*/ |
258
|
5 |
|
protected function getNewModel($modelClass) |
259
|
|
|
{ |
260
|
5 |
|
$obj = is_object($modelClass) ? $modelClass : \Yii::createObject($modelClass); |
261
|
|
|
|
262
|
5 |
|
if ($obj instanceof CurrencyInterface) { |
263
|
5 |
|
$model = new Currency(); |
264
|
5 |
|
} elseif ($obj instanceof CategoryInterface) { |
265
|
5 |
|
$model = new Category(); |
266
|
5 |
|
} elseif ($obj instanceof CustomOfferInterface && $this->customOfferClass !== null && class_exists($this->customOfferClass)) { |
267
|
1 |
|
$model = \Yii::createObject($this->customOfferClass); |
268
|
5 |
|
} elseif ($obj instanceof SimpleOfferInterface) { |
269
|
4 |
|
$model = new SimpleOffer(); |
270
|
4 |
|
} else { |
271
|
|
|
throw new Exception('Model ' . get_class($obj) . ' has unknown interface'); |
272
|
|
|
} |
273
|
|
|
|
274
|
5 |
|
return $model; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Performs PHP memory garbage collection. |
279
|
|
|
*/ |
280
|
5 |
|
protected function gc() |
281
|
|
|
{ |
282
|
5 |
|
if (!gc_enabled()) { |
283
|
|
|
gc_enable(); |
284
|
|
|
} |
285
|
5 |
|
gc_collect_cycles(); |
286
|
5 |
|
} |
287
|
|
|
} |
288
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.