1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace iamsaint\yml\components; |
4
|
|
|
|
5
|
|
|
use iamsaint\yml\BaseObject; |
6
|
|
|
use iamsaint\yml\exceptions\IncorrectRuleException; |
7
|
|
|
use XMLWriter; |
8
|
|
|
use function count; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Offer |
12
|
|
|
* @package iamsaint\yml\components |
13
|
|
|
* |
14
|
|
|
* @property int $id |
15
|
|
|
* @property string $type |
16
|
|
|
* @property bool $available |
17
|
|
|
* @property string $url |
18
|
|
|
* @property float $price |
19
|
|
|
* @property int $currencyId |
20
|
|
|
* @property int $categoryId |
21
|
|
|
* @property bool $store |
22
|
|
|
* @property bool $delivery |
23
|
|
|
* @property string $name |
24
|
|
|
* @property array $deliveryOptions |
25
|
|
|
* @property string $typePrefix |
26
|
|
|
* @property string $vendor |
27
|
|
|
* @property string $vendorCode |
28
|
|
|
* @property string $model |
29
|
|
|
* @property string $description |
30
|
|
|
* @property string $salesNotes |
31
|
|
|
* @property string $barcode |
32
|
|
|
* @property int $age |
33
|
|
|
* @property array $pictures |
34
|
|
|
* @property bool $manufacturerWarranty |
35
|
|
|
* @property array $countryOfOrigin |
36
|
|
|
* @property OfferParam[] $params |
37
|
|
|
* @property bool $adult |
38
|
|
|
* @property Tag[] $customTags |
39
|
|
|
*/ |
40
|
|
|
class Offer extends BaseObject |
41
|
|
|
{ |
42
|
|
|
public $id; |
43
|
|
|
public $type; |
44
|
|
|
public $available = true; |
45
|
|
|
public $url; |
46
|
|
|
public $price; |
47
|
|
|
public $currencyId; |
48
|
|
|
public $categoryId; |
49
|
|
|
public $store = true; |
50
|
|
|
public $delivery = false; |
51
|
|
|
public $name; |
52
|
|
|
public $deliveryOptions; |
53
|
|
|
public $typePrefix; |
54
|
|
|
public $vendor; |
55
|
|
|
public $vendorCode; |
56
|
|
|
public $model; |
57
|
|
|
public $description; |
58
|
|
|
public $salesNotes; |
59
|
|
|
public $barcode; |
60
|
|
|
public $age = false; |
61
|
|
|
public $pictures = []; |
62
|
|
|
public $manufacturerWarranty = true; |
63
|
|
|
public $countryOfOrigin; |
64
|
|
|
public $params; |
65
|
|
|
public $adult = false; |
66
|
|
|
public $customTags = []; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param XMLWriter $writer |
70
|
|
|
*/ |
71
|
|
|
public function write($writer): void |
72
|
|
|
{ |
73
|
|
|
$writer->startElement('offer'); |
74
|
|
|
$writer->writeAttribute('id', $this->id); |
75
|
|
|
|
76
|
|
|
$tags = [ |
77
|
|
|
['name' => 'type', 'value' => $this->type, 'condition' => null], |
78
|
|
|
['name' => 'available', 'value' => $this->available, 'condition' => null], |
79
|
|
|
['name' => 'url', 'value' => $this->url, 'condition' => null], |
80
|
|
|
['name' => 'price', 'value' => $this->price, 'condition' => null], |
81
|
|
|
['name' => 'currencyId', 'value' => $this->currencyId, 'condition' => null], |
82
|
|
|
['name' => 'categoryId', 'value' => $this->categoryId, 'condition' => null], |
83
|
|
|
['name' => 'store', 'value' => $this->store, 'condition' => null], |
84
|
|
|
['name' => 'delivery', 'value' => $this->delivery, 'condition' => null], |
85
|
|
|
['name' => 'name', 'value' => $this->name, 'condition' => null], |
86
|
|
|
['name' => 'vendor', 'value' => $this->vendor, 'condition' => null], |
87
|
|
|
['name' => 'model', 'value' => $this->model, 'condition' => null], |
88
|
|
|
['name' => 'description', 'value' => $this->description, 'condition' => null], |
89
|
|
|
['name' => 'sales_notes', 'value' => $this->salesNotes, 'condition' => null], |
90
|
|
|
['name' => 'barcode', 'value' => $this->barcode, 'condition' => null], |
91
|
|
|
['name' => 'age', 'value' => $this->age, 'condition' => false], |
92
|
|
|
['name' => 'manufacturer_warranty', 'value' => $this->manufacturerWarranty, 'condition' => null], |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
if (count($this->pictures) > 0) { |
96
|
|
|
foreach ($this->pictures as $picture) { |
97
|
|
|
$picture->write($writer); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
foreach ($tags as $tag) { |
102
|
|
|
$this->writeTag($tag['name'], $tag['value'], $tag['condition']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->writeCustomTags($writer); |
106
|
|
|
|
107
|
|
|
if (count($this->params) > 0) { |
108
|
|
|
foreach ($this->params as $param) { |
109
|
|
|
$param->write($writer); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$writer->endElement(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param XMLWriter $writer |
118
|
|
|
*/ |
119
|
|
|
public function writeCustomTags($writer): void |
120
|
|
|
{ |
121
|
|
|
foreach ($this->customTags as $tag) { |
122
|
|
|
$tag->write($writer); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param OfferParam $param |
128
|
|
|
* @return bool |
129
|
|
|
* @throws IncorrectRuleException |
130
|
|
|
*/ |
131
|
|
|
public function addParam(OfferParam $param): ?bool |
132
|
|
|
{ |
133
|
|
|
if ($param->validate()) { |
134
|
|
|
$this->params[] = $param; |
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->errors['params'][] = $param->errors; |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param OfferPicture $picture |
144
|
|
|
* @return bool |
145
|
|
|
* @throws IncorrectRuleException |
146
|
|
|
*/ |
147
|
|
|
public function addPicture(OfferPicture $picture): ?bool |
148
|
|
|
{ |
149
|
|
|
if ($picture->validate()) { |
150
|
|
|
$this->pictures[] = $picture; |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->errors['pictures'][] = $picture->errors; |
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param $url |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
|
|
public function setUrl($url): self |
163
|
|
|
{ |
164
|
|
|
$this->url = $url; |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $type |
170
|
|
|
* @return Offer |
171
|
|
|
*/ |
172
|
|
|
public function setType(string $type): Offer |
173
|
|
|
{ |
174
|
|
|
$this->type = $type; |
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param bool $available |
180
|
|
|
* @return Offer |
181
|
|
|
*/ |
182
|
|
|
public function setAvailable(bool $available = true): Offer |
183
|
|
|
{ |
184
|
|
|
$this->available = $available; |
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param mixed $price |
190
|
|
|
* @return Offer |
191
|
|
|
*/ |
192
|
|
|
public function setPrice($price): Offer |
193
|
|
|
{ |
194
|
|
|
$this->price = $price; |
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param mixed $currencyId |
200
|
|
|
* @return Offer |
201
|
|
|
*/ |
202
|
|
|
public function setCurrencyId($currencyId): Offer |
203
|
|
|
{ |
204
|
|
|
$this->currencyId = $currencyId; |
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param mixed $id |
210
|
|
|
* @return Offer |
211
|
|
|
*/ |
212
|
|
|
public function setId($id): Offer |
213
|
|
|
{ |
214
|
|
|
$this->id = $id; |
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param mixed $categoryId |
220
|
|
|
* @return Offer |
221
|
|
|
*/ |
222
|
|
|
public function setCategoryId($categoryId): Offer |
223
|
|
|
{ |
224
|
|
|
$this->categoryId = $categoryId; |
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param bool $delivery |
230
|
|
|
* @return Offer |
231
|
|
|
*/ |
232
|
|
|
public function setDelivery(bool $delivery): Offer |
233
|
|
|
{ |
234
|
|
|
$this->delivery = $delivery; |
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param mixed $deliveryOptions |
240
|
|
|
* @return Offer |
241
|
|
|
*/ |
242
|
|
|
public function setDeliveryOptions($deliveryOptions): Offer |
243
|
|
|
{ |
244
|
|
|
$this->deliveryOptions = $deliveryOptions; |
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param mixed $typePrefix |
250
|
|
|
* @return Offer |
251
|
|
|
*/ |
252
|
|
|
public function setTypePrefix($typePrefix): Offer |
253
|
|
|
{ |
254
|
|
|
$this->typePrefix = $typePrefix; |
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param mixed $vendor |
260
|
|
|
* @return Offer |
261
|
|
|
*/ |
262
|
|
|
public function setVendor($vendor): Offer |
263
|
|
|
{ |
264
|
|
|
$this->vendor = $vendor; |
265
|
|
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param mixed $vendorCode |
270
|
|
|
* @return Offer |
271
|
|
|
*/ |
272
|
|
|
public function setVendorCode($vendorCode): Offer |
273
|
|
|
{ |
274
|
|
|
$this->vendorCode = $vendorCode; |
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param mixed $model |
280
|
|
|
* @return Offer |
281
|
|
|
*/ |
282
|
|
|
public function setModel($model): Offer |
283
|
|
|
{ |
284
|
|
|
$this->model = $model; |
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param mixed $description |
290
|
|
|
* @return Offer |
291
|
|
|
*/ |
292
|
|
|
public function setDescription($description): Offer |
293
|
|
|
{ |
294
|
|
|
$this->description = $description; |
295
|
|
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param bool $manufacturerWarranty |
300
|
|
|
* @return Offer |
301
|
|
|
*/ |
302
|
|
|
public function setManufacturerWarranty(bool $manufacturerWarranty): Offer |
303
|
|
|
{ |
304
|
|
|
$this->manufacturerWarranty = $manufacturerWarranty; |
305
|
|
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param mixed $countryOfOrigin |
310
|
|
|
* @return Offer |
311
|
|
|
*/ |
312
|
|
|
public function setCountryOfOrigin($countryOfOrigin): Offer |
313
|
|
|
{ |
314
|
|
|
$this->countryOfOrigin = $countryOfOrigin; |
315
|
|
|
return $this; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @param mixed $params |
320
|
|
|
* @return Offer |
321
|
|
|
*/ |
322
|
|
|
public function setParams($params): Offer |
323
|
|
|
{ |
324
|
|
|
$this->params = $params; |
325
|
|
|
return $this; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param $name |
330
|
|
|
* @return Offer |
331
|
|
|
*/ |
332
|
|
|
public function setName($name): Offer |
333
|
|
|
{ |
334
|
|
|
$this->name = $name; |
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @param bool $adult |
340
|
|
|
* @return Offer |
341
|
|
|
*/ |
342
|
|
|
public function setAdult(bool $adult): Offer |
343
|
|
|
{ |
344
|
|
|
$this->adult = $adult; |
345
|
|
|
return $this; |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|