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

Shop::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
nc 2
nop 1
dl 0
loc 21
rs 9.8333
c 3
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 Shop
12
 * @package iamsaint\yml\components
13
 *
14
 * @property string $name
15
 * @property string $company
16
 * @property string $url
17
 * @property Currency[] $currencies
18
 * @property Category[] $categories
19
 * @property array $deliveryOptions
20
 * @property Offer[] $offers
21
 * @property bool $adult
22
 * @property Tag[] $customTags
23
 */
24
class Shop extends BaseObject
25
{
26
    public $name;
27
    public $company;
28
    public $url;
29
    public $currencies = [];
30
    public $categories = [];
31
    public $deliveryOptions;
32
    public $offers = [];
33
    public $adult = false;
34
    public $customTags = [];
35
36
    /**
37
     * @param XMLWriter $writer
38
     */
39
    public function write($writer): void
40
    {
41
        $writer->startElement('shop');
42
43
        $tags = [
44
            ['name' => 'name', 'value' => $this->name, 'condition' => null],
45
            ['name' => 'company', 'value' => $this->company, 'condition' => null],
46
            ['name' => 'url', 'value' => $this->url, 'condition' => null],
47
            ['name' => 'adult', 'value' => $this->adult, 'condition' => false],
48
        ];
49
50
        foreach ($tags as $tag) {
51
            $this->writeTag($tag['name'], $tag['value'], $tag['condition']);
52
        }
53
54
        $this->writeCurrencies($writer);
55
        $this->writeCategories($writer);
56
        $this->writeOffers($writer);
57
        $this->writeCustomTags($writer);
58
59
        $writer->endElement();
60
    }
61
62
    /**
63
     * @param XMLWriter $writer
64
     */
65
    public function writeCurrencies($writer): void
66
    {
67
        if (count($this->currencies) > 0) {
68
            $this->writeElements($writer, 'currencies', $this->currencies);
69
        }
70
    }
71
72
    /**
73
     * @param XMLWriter $writer
74
     */
75
    public function writeCategories($writer): void
76
    {
77
        if (count($this->categories) > 0) {
78
            $this->writeElements($writer, 'categories', $this->categories);
79
        }
80
    }
81
82
    /**
83
     * @param XMLWriter $writer
84
     */
85
    public function writeOffers($writer): void
86
    {
87
        if (count($this->offers) > 0) {
88
            $this->writeElements($writer, 'offers', $this->offers);
89
        }
90
    }
91
92
    /**
93
     * @param XMLWriter $writer
94
     */
95
    public function writeCustomTags($writer): void
96
    {
97
        foreach ($this->customTags as $tag) {
98
            $tag->write($writer);
99
        }
100
    }
101
102
    /**
103
     * @param Category $category
104
     */
105
    public function addCategory(Category $category): void
106
    {
107
        $this->categories[] = $category;
108
    }
109
110
    /**
111
     * @param Currency $currency
112
     * @return bool
113
     * @throws IncorrectRuleException
114
     */
115
    public function addCurrency(Currency $currency): bool
116
    {
117
        if ($is_valid = $currency->validate()) {
118
            $this->currencies[] = $currency;
119
        } else {
120
            $this->errors['currency'][] = $currency->errors;
121
        }
122
123
        return $is_valid;
124
    }
125
126
    /**
127
     * @param Offer $offer
128
     */
129
    public function addOffer(Offer $offer): void
130
    {
131
        $this->offers[] = $offer;
132
    }
133
134
    /**
135
     * @param mixed $name
136
     * @return Shop
137
     */
138
    public function setName($name): Shop
139
    {
140
        $this->name = $name;
141
        return $this;
142
    }
143
144
    /**
145
     * @param mixed $company
146
     * @return Shop
147
     */
148
    public function setCompany($company): Shop
149
    {
150
        $this->company = $company;
151
        return $this;
152
    }
153
154
    /**
155
     * @param mixed $url
156
     * @return Shop
157
     */
158
    public function setUrl($url): Shop
159
    {
160
        $this->url = $url;
161
        return $this;
162
    }
163
}
164