Completed
Push — master ( 18f374...1b13b7 )
by Alexey
01:47
created

Offer::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 25
nc 2
nop 1
dl 0
loc 35
rs 9.52
c 1
b 0
f 0
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
        $this->writePictures($writer);
96
97
        foreach ($tags as $tag) {
98
            $this->writeTag($tag['name'], $tag['value'], $tag['condition']);
99
        }
100
101
        $this->writeCustomTags($writer);
102
103
        $this->writeParams($writer);
104
105
        $writer->endElement();
106
    }
107
108
    /**
109
     * @param XMLWriter $writer
110
     */
111
    public function writePictures($writer): void
112
    {
113
        if (count($this->pictures) > 0) {
114
            foreach ($this->pictures as $picture) {
115
                $picture->write($writer);
116
            }
117
        }
118
    }
119
120
    /**
121
     * @param XMLWriter $writer
122
     */
123
    public function writeParams($writer): void
124
    {
125
        if (count($this->params) > 0) {
126
            foreach ($this->params as $param) {
127
                $param->write($writer);
128
            }
129
        }
130
    }
131
132
    /**
133
     * @param XMLWriter $writer
134
     */
135
    public function writeCustomTags($writer): void
136
    {
137
        foreach ($this->customTags as $tag) {
138
            $tag->write($writer);
139
        }
140
    }
141
142
    /**
143
     * @param OfferParam $param
144
     * @return bool
145
     * @throws IncorrectRuleException
146
     */
147
    public function addParam(OfferParam $param): ?bool
148
    {
149
        if ($param->validate()) {
150
            $this->params[] = $param;
151
            return true;
152
        }
153
154
        $this->errors['params'][] = $param->errors;
155
        return false;
156
    }
157
158
    /**
159
     * @param OfferPicture $picture
160
     * @return bool
161
     * @throws IncorrectRuleException
162
     */
163
    public function addPicture(OfferPicture $picture): ?bool
164
    {
165
        if ($picture->validate()) {
166
            $this->pictures[] = $picture;
167
            return true;
168
        }
169
170
        $this->errors['pictures'][] = $picture->errors;
171
        return false;
172
    }
173
174
    /**
175
     * @param $url
176
     * @return $this
177
     */
178
    public function setUrl($url): self
179
    {
180
        $this->url = $url;
181
        return $this;
182
    }
183
184
    /**
185
     * @param string $type
186
     * @return Offer
187
     */
188
    public function setType(string $type): Offer
189
    {
190
        $this->type = $type;
191
        return $this;
192
    }
193
194
    /**
195
     * @param bool $available
196
     * @return Offer
197
     */
198
    public function setAvailable(bool $available = true): Offer
199
    {
200
        $this->available = $available;
201
        return $this;
202
    }
203
204
    /**
205
     * @param mixed $price
206
     * @return Offer
207
     */
208
    public function setPrice($price): Offer
209
    {
210
        $this->price = $price;
211
        return $this;
212
    }
213
214
    /**
215
     * @param mixed $currencyId
216
     * @return Offer
217
     */
218
    public function setCurrencyId($currencyId): Offer
219
    {
220
        $this->currencyId = $currencyId;
221
        return $this;
222
    }
223
224
    /**
225
     * @param mixed $id
226
     * @return Offer
227
     */
228
    public function setId($id): Offer
229
    {
230
        $this->id = $id;
231
        return $this;
232
    }
233
234
    /**
235
     * @param mixed $categoryId
236
     * @return Offer
237
     */
238
    public function setCategoryId($categoryId): Offer
239
    {
240
        $this->categoryId = $categoryId;
241
        return $this;
242
    }
243
244
    /**
245
     * @param bool $delivery
246
     * @return Offer
247
     */
248
    public function setDelivery(bool $delivery): Offer
249
    {
250
        $this->delivery = $delivery;
251
        return $this;
252
    }
253
254
    /**
255
     * @param mixed $deliveryOptions
256
     * @return Offer
257
     */
258
    public function setDeliveryOptions($deliveryOptions): Offer
259
    {
260
        $this->deliveryOptions = $deliveryOptions;
261
        return $this;
262
    }
263
264
    /**
265
     * @param mixed $typePrefix
266
     * @return Offer
267
     */
268
    public function setTypePrefix($typePrefix): Offer
269
    {
270
        $this->typePrefix = $typePrefix;
271
        return $this;
272
    }
273
274
    /**
275
     * @param mixed $vendor
276
     * @return Offer
277
     */
278
    public function setVendor($vendor): Offer
279
    {
280
        $this->vendor = $vendor;
281
        return $this;
282
    }
283
284
    /**
285
     * @param mixed $vendorCode
286
     * @return Offer
287
     */
288
    public function setVendorCode($vendorCode): Offer
289
    {
290
        $this->vendorCode = $vendorCode;
291
        return $this;
292
    }
293
294
    /**
295
     * @param mixed $model
296
     * @return Offer
297
     */
298
    public function setModel($model): Offer
299
    {
300
        $this->model = $model;
301
        return $this;
302
    }
303
304
    /**
305
     * @param mixed $description
306
     * @return Offer
307
     */
308
    public function setDescription($description): Offer
309
    {
310
        $this->description = $description;
311
        return $this;
312
    }
313
314
    /**
315
     * @param bool $manufacturerWarranty
316
     * @return Offer
317
     */
318
    public function setManufacturerWarranty(bool $manufacturerWarranty): Offer
319
    {
320
        $this->manufacturerWarranty = $manufacturerWarranty;
321
        return $this;
322
    }
323
324
    /**
325
     * @param mixed $countryOfOrigin
326
     * @return Offer
327
     */
328
    public function setCountryOfOrigin($countryOfOrigin): Offer
329
    {
330
        $this->countryOfOrigin = $countryOfOrigin;
331
        return $this;
332
    }
333
334
    /**
335
     * @param mixed $params
336
     * @return Offer
337
     */
338
    public function setParams($params): Offer
339
    {
340
        $this->params = $params;
341
        return $this;
342
    }
343
344
    /**
345
     * @param $name
346
     * @return Offer
347
     */
348
    public function setName($name): Offer
349
    {
350
        $this->name = $name;
351
        return $this;
352
    }
353
354
    /**
355
     * @param bool $adult
356
     * @return Offer
357
     */
358
    public function setAdult(bool $adult): Offer
359
    {
360
        $this->adult = $adult;
361
        return $this;
362
    }
363
}
364