Completed
Pull Request — master (#23)
by
unknown
01:14
created

AbstractOffer::getCondition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Bukashk0zzzYmlGenerator
5
 *
6
 * (c) Denis Golubovskiy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bukashk0zzz\YmlGenerator\Model\Offer;
13
14
/**
15
 * Abstract Class Offer
16
 */
17
abstract class AbstractOffer implements OfferInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    private $id;
23
24
    /**
25
     * @var bool
26
     */
27
    private $available;
28
29
    /**
30
     * @var string
31
     */
32
    private $url;
33
34
    /**
35
     * @var float
36
     */
37
    private $price;
38
39
    /**
40
     * @var float
41
     */
42
    private $oldPrice;
43
44
    /**
45
     * @var string
46
     */
47
    private $currencyId;
48
49
    /**
50
     * @var int
51
     */
52
    private $categoryId;
53
54
    /**
55
     * @var array
56
     */
57
    private $categoriesId = [];
58
59
    /**
60
     * @var string
61
     */
62
    private $name;
63
64
    /**
65
     * @var string
66
     */
67
    private $marketCategory;
68
69
    /**
70
     * @var bool
71
     */
72
    private $adult;
73
74
    /**
75
     * @var string
76
     */
77
    private $salesNotes;
78
79
    /**
80
     * @var bool
81
     */
82
    private $manufacturerWarranty;
83
84
    /**
85
     * @var bool
86
     */
87
    private $pickup;
88
89
    /**
90
     * @var bool
91
     */
92
    private $downloadable;
93
94
    /**
95
     * @var bool
96
     */
97
    private $delivery;
98
99
    /**
100
     * @var float
101
     */
102
    private $localDeliveryCost;
103
104
    /**
105
     * @var string
106
     */
107
    private $description;
108
109
    /**
110
     * @var string
111
     */
112
    private $countryOfOrigin;
113
114
    /**
115
     * @var string
116
     */
117
    private $weight;
118
119
    /**
120
     * @var int
121
     */
122
    private $cpa;
123
124
    /**
125
     * @var string[]
126
     */
127
    private $barcodes;
128
129
    /**
130
     * @var array
131
     */
132
    private $pictures = [];
133
134
    /**
135
     * @var array
136
     */
137
    private $params = [];
138
139
    /**
140
     * @var bool
141
     */
142
    private $store;
143
144
    /**
145
     * Array of custom elements (element types are keys) of arrays of element values
146
     * There may be multiple elements of the same type
147
     * @var array[]
148
     */
149
    private $customElements;
150
151
152
    /**
153
     * @var OfferCondition
154
     */
155
    private $condition;
156
157
    /**
158
     * @return array
159
     */
160
    public function toArray()
161
    {
162
        return \array_merge($this->getHeaderOptions(), $this->getOptions(), $this->getFooterOptions());
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getId()
169
    {
170
        return $this->id;
171
    }
172
173
    /**
174
     * @param string $id
175
     *
176
     * @return $this
177
     */
178
    public function setId($id)
179
    {
180
        $this->id = $id;
181
182
        return $this;
183
    }
184
185
    /**
186
     * @return bool
187
     */
188
    public function isAvailable()
189
    {
190
        return $this->available;
191
    }
192
193
    /**
194
     * @param bool $available
195
     *
196
     * @return $this
197
     */
198
    public function setAvailable($available)
199
    {
200
        $this->available = $available;
201
202
        return $this;
203
    }
204
205
    /**
206
     * @return string
207
     */
208
    public function getUrl()
209
    {
210
        return $this->url;
211
    }
212
213
    /**
214
     * @param string $url
215
     *
216
     * @return $this
217
     */
218
    public function setUrl($url)
219
    {
220
        $this->url = $url;
221
222
        return $this;
223
    }
224
225
    /**
226
     * @return float
227
     */
228
    public function getPrice()
229
    {
230
        return $this->price;
231
    }
232
233
    /**
234
     * @param float $price
235
     *
236
     * @return $this
237
     */
238
    public function setPrice($price)
239
    {
240
        $this->price = $price;
241
242
        return $this;
243
    }
244
245
    /**
246
     * @return float
247
     */
248
    public function getOldPrice()
249
    {
250
        return $this->oldPrice;
251
    }
252
253
    /**
254
     * @param float $oldPrice
255
     *
256
     * @return $this
257
     */
258
    public function setOldPrice($oldPrice)
259
    {
260
        $this->oldPrice = $oldPrice;
261
262
        return $this;
263
    }
264
265
    /**
266
     * @return string
267
     */
268
    public function getCurrencyId()
269
    {
270
        return $this->currencyId;
271
    }
272
273
    /**
274
     * @param string $currencyId
275
     *
276
     * @return $this
277
     */
278
    public function setCurrencyId($currencyId)
279
    {
280
        $this->currencyId = $currencyId;
281
282
        return $this;
283
    }
284
285
    /**
286
     * @return int
287
     */
288
    public function getCategoryId()
289
    {
290
        return $this->categoryId;
291
    }
292
293
    /**
294
     * @param int $categoryId
295
     *
296
     * @return $this
297
     */
298
    public function setCategoryId($categoryId)
299
    {
300
        $this->categoryId = $categoryId;
301
302
        return $this;
303
    }
304
305
    /**
306
     * @return array
307
     */
308
    public function getCategoriesId()
309
    {
310
        return $this->categoriesId;
311
    }
312
313
    /**
314
     * @param array $categoriesId
315
     *
316
     * @return $this
317
     */
318
    public function setCategoriesId(array $categoriesId)
319
    {
320
        $this->categoriesId = $categoriesId;
321
322
        return $this;
323
    }
324
325
    /**
326
     * @return string
327
     */
328
    public function getName()
329
    {
330
        return $this->name;
331
    }
332
333
    /**
334
     * @param $name
335
     * @return $this
336
     */
337
    public function setName($name)
338
    {
339
        $this->name = $name;
340
341
        return $this;
342
    }
343
344
    /**
345
     * @return string
346
     */
347
    public function getMarketCategory()
348
    {
349
        return $this->marketCategory;
350
    }
351
352
    /**
353
     * @param string $marketCategory
354
     *
355
     * @return $this
356
     */
357
    public function setMarketCategory($marketCategory)
358
    {
359
        $this->marketCategory = $marketCategory;
360
361
        return $this;
362
    }
363
364
    /**
365
     * @return bool
366
     */
367
    public function isAdult()
368
    {
369
        return $this->adult;
370
    }
371
372
    /**
373
     * @param bool $adult
374
     *
375
     * @return $this
376
     */
377
    public function setAdult($adult)
378
    {
379
        $this->adult = $adult;
380
381
        return $this;
382
    }
383
384
    /**
385
     * @return string
386
     */
387
    public function getSalesNotes()
388
    {
389
        return $this->salesNotes;
390
    }
391
392
    /**
393
     * @param string $salesNotes
394
     *
395
     * @return $this
396
     */
397
    public function setSalesNotes($salesNotes)
398
    {
399
        $this->salesNotes = $salesNotes;
400
401
        return $this;
402
    }
403
404
    /**
405
     * @return bool
406
     */
407
    public function isManufacturerWarranty()
408
    {
409
        return $this->manufacturerWarranty;
410
    }
411
412
    /**
413
     * @param bool $manufacturerWarranty
414
     *
415
     * @return $this
416
     */
417
    public function setManufacturerWarranty($manufacturerWarranty)
418
    {
419
        $this->manufacturerWarranty = $manufacturerWarranty;
420
421
        return $this;
422
    }
423
424
    /**
425
     * @return bool
426
     */
427
    public function isPickup()
428
    {
429
        return $this->pickup;
430
    }
431
432
    /**
433
     * @param bool $pickup
434
     *
435
     * @return $this
436
     */
437
    public function setPickup($pickup)
438
    {
439
        $this->pickup = $pickup;
440
441
        return $this;
442
    }
443
444
    /**
445
     * @return bool
446
     */
447
    public function isDownloadable()
448
    {
449
        return $this->downloadable;
450
    }
451
452
    /**
453
     * @param bool $downloadable
454
     *
455
     * @return $this
456
     */
457
    public function setDownloadable($downloadable)
458
    {
459
        $this->downloadable = $downloadable;
460
461
        return $this;
462
    }
463
464
    /**
465
     * @return bool
466
     */
467
    public function isDelivery()
468
    {
469
        return $this->delivery;
470
    }
471
472
    /**
473
     * @param bool $delivery
474
     *
475
     * @return $this
476
     */
477
    public function setDelivery($delivery)
478
    {
479
        $this->delivery = $delivery;
480
481
        return $this;
482
    }
483
484
    /**
485
     * @param bool $store
486
     *
487
     * @return $this
488
     */
489
    public function setStore($store)
490
    {
491
        $this->store = $store;
492
493
        return $this;
494
    }
495
496
    /**
497
     * @return bool
498
     */
499
    public function isStore()
500
    {
501
        return $this->store;
502
    }
503
504
    /**
505
     * @return float
506
     */
507
    public function getLocalDeliveryCost()
508
    {
509
        return $this->localDeliveryCost;
510
    }
511
512
    /**
513
     * @param float $localDeliveryCost
514
     *
515
     * @return $this
516
     */
517
    public function setLocalDeliveryCost($localDeliveryCost)
518
    {
519
        $this->localDeliveryCost = $localDeliveryCost;
520
521
        return $this;
522
    }
523
524
    /**
525
     * @return string
526
     */
527
    public function getDescription()
528
    {
529
        return $this->description;
530
    }
531
532
    /**
533
     * @param string $description
534
     *
535
     * @return $this
536
     */
537
    public function setDescription($description)
538
    {
539
        $this->description = $description;
540
541
        return $this;
542
    }
543
544
    /**
545
     * @return string
546
     */
547
    public function getCountryOfOrigin()
548
    {
549
        return $this->countryOfOrigin;
550
    }
551
552
    /**
553
     * @param string $countryOfOrigin
554
     *
555
     * @return $this
556
     */
557
    public function setCountryOfOrigin($countryOfOrigin)
558
    {
559
        $this->countryOfOrigin = $countryOfOrigin;
560
561
        return $this;
562
    }
563
564
    /**
565
     * @return string
566
     */
567
    public function getWeight()
568
    {
569
        return $this->weight;
570
    }
571
572
    /**
573
     * @param string $weight
574
     *
575
     * @return $this
576
     */
577
    public function setWeight($weight)
578
    {
579
        $this->weight = $weight;
580
581
        return $this;
582
    }
583
584
    /**
585
     * @return int
586
     */
587
    public function getCpa()
588
    {
589
        return $this->cpa;
590
    }
591
592
    /**
593
     * @param int $cpa
594
     *
595
     * @return $this
596
     */
597
    public function setCpa($cpa)
598
    {
599
        $this->cpa = $cpa;
600
601
        return $this;
602
    }
603
604
    /**
605
     * @return array
606
     */
607
    public function getParams()
608
    {
609
        return $this->params;
610
    }
611
612
    /**
613
     * @param OfferParam $param
614
     *
615
     * @return $this
616
     */
617
    public function addParam(OfferParam $param)
618
    {
619
        $this->params[] = $param;
620
621
        return $this;
622
    }
623
624
    /**
625
     * Add picture
626
     *
627
     * @param string $url
628
     *
629
     * @return $this
630
     */
631
    public function addPicture($url)
632
    {
633
        if (\count($this->pictures) < 10) {
634
            $this->pictures[] = $url;
635
        }
636
637
        return $this;
638
    }
639
640
    /**
641
     * Set pictures
642
     *
643
     * @param array $pictures
644
     *
645
     * @return $this
646
     */
647
    public function setPictures(array $pictures)
648
    {
649
        $this->pictures = $pictures;
650
651
        return $this;
652
    }
653
654
    /**
655
     * Get picture list
656
     *
657
     * @return array
658
     */
659
    public function getPictures()
660
    {
661
        return $this->pictures;
662
    }
663
664
    /**
665
     * Get list of barcodes of the offer
666
     *
667
     * @return string[]
668
     */
669
    public function getBarcodes()
670
    {
671
        return $this->barcodes;
672
    }
673
674
    /**
675
     * Set list of barcodes for that offer
676
     *
677
     * @param string[] $barcodes
678
     *
679
     * @return $this
680
     */
681
    public function setBarcodes(array $barcodes = [])
682
    {
683
        $this->barcodes = $barcodes;
684
685
        return $this;
686
    }
687
688
    /**
689
     * Add one barcode to the collection of barcodes of this offer
690
     *
691
     * @param string $barcode
692
     *
693
     * @return AbstractOffer
694
     */
695
    public function addBarcode($barcode)
696
    {
697
        $this->barcodes[] = $barcode;
698
699
        return $this;
700
    }
701
702
    /**
703
     * Sets list of custom elements
704
     *
705
     * @param array $customElements Array (keys are element types) of arrays (element values)
706
     *
707
     * @return $this
708
     */
709
    public function setCustomElements(array $customElements = [])
710
    {
711
        $this->customElements = $customElements;
712
713
        return $this;
714
    }
715
716
    /**
717
     * Add a custom element with given type and value
718
     * Multiple elements of the same type are supported
719
     *
720
     * @param string $elementType
721
     * @param mixed  $value
722
     *
723
     * @return $this
724
     */
725
    public function addCustomElement($elementType, $value)
726
    {
727
        if ($value !== null) {
728
            // Add value to the list of values of the given element type creating array when needed
729
            $this->customElements[$elementType][] = $value;
730
        }
731
732
        return $this;
733
    }
734
735
    /**
736
     * Returns a list of custom elements
737
     * Always returns an array even if no custom elements were added
738
     *
739
     * @return array
740
     */
741
    public function getCustomElements()
742
    {
743
        return $this->customElements ?: [];
744
    }
745
746
    /**
747
     * Returns a list of values for the specified custom element type
748
     * Always returns an array
749
     *
750
     * @param string $elementType
751
     *
752
     * @return array
753
     */
754
    public function getCustomElementByType($elementType)
755
    {
756
        // TODO: Use ?? operator when support for PHP 5.6 is no longer needed
757
        if (isset($this->customElements[$elementType])) {
758
            return $this->customElements[$elementType];
759
        }
760
761
        return [];
762
    }
763
764
765
    /**
766
     * @return OfferCondition
767
     */
768
    public function getCondition()
769
    {
770
        return $this->condition;
771
    }
772
773
774
    /**
775
     * @param OfferCondition $condition
776
     *
777
     * @return $this
778
     */
779
    public function addCondition(OfferCondition $condition)
780
    {
781
        $this->condition = $condition;
782
783
        return $this;
784
    }
785
786
787
    /**
788
     * @return array
789
     */
790
    abstract protected function getOptions();
791
792
    /**
793
     * @return array
794
     */
795
    private function getHeaderOptions()
796
    {
797
        return [
798
            'url' => $this->getUrl(),
799
            'price' => $this->getPrice(),
800
            'oldprice' => $this->getOldPrice(),
801
            'currencyId' => $this->getCurrencyId(),
802
            'categoryId' => array_merge(
803
                [$this->getCategoryId()],
804
                $this->getCategoriesId()
805
            ),
806
            'market_category' => $this->getMarketCategory(),
807
            'picture' => $this->getPictures(),
808
            'pickup' => $this->isPickup(),
809
            'store' => $this->isStore(),
810
            'delivery' => $this->isDelivery(),
811
            'local_delivery_cost' => $this->getLocalDeliveryCost(),
812
            'weight' => $this->getWeight(),
813
            'name' => $this->getName()
814
        ] + $this->getCustomElements();
815
    }
816
817
    /**
818
     * @return array
819
     */
820
    private function getFooterOptions()
821
    {
822
        return [
823
            'description' => $this->getDescription(),
824
            'sales_notes' => $this->getSalesNotes(),
825
            'manufacturer_warranty' => $this->isManufacturerWarranty(),
826
            'country_of_origin' => $this->getCountryOfOrigin(),
827
            'downloadable' => $this->isDownloadable(),
828
            'adult' => $this->isAdult(),
829
            'cpa' => $this->getCpa(),
830
            'barcode' => $this->getBarcodes()
831
        ];
832
    }
833
}
834