1<?php
2
3namespace FINDOLOGIC\Export\Data;
4
5use DateTime;
6use FINDOLOGIC\Export\Helpers\Serializable;
7
8abstract class Item implements Serializable
9{
10    protected $id;
11
12    /** @var Name */
13    protected $name;
14
15    /** @var Summary */
16    protected $summary;
17
18    /** @var Description */
19    protected $description;
20
21    /** @var Price */
22    protected $price;
23
24    /** @var Url */
25    protected $url;
26
27    /** @var Bonus */
28    protected $bonus;
29
30    /** @var SalesFrequency */
31    protected $salesFrequency;
32
33    /** @var DateAdded */
34    protected $dateAdded;
35
36    /** @var Sort */
37    protected $sort;
38
39    /** @var AllKeywords */
40    protected $keywords;
41
42    /** @var AllOrdernumbers */
43    protected $ordernumbers;
44
45    protected $properties = [];
46
47    protected $attributes = [];
48
49    protected $images = [];
50
51    protected $usergroups = [];
52
53    public function __construct($id)
54    {
55        $this->id = $id;
56
57        $this->name = new Name();
58        $this->summary = new Summary();
59        $this->description = new Description();
60        $this->url = new Url();
61        $this->bonus = new Bonus();
62        $this->salesFrequency = new SalesFrequency();
63        $this->dateAdded = new DateAdded();
64        $this->sort = new Sort();
65        $this->keywords = new AllKeywords();
66        $this->ordernumbers = new AllOrdernumbers();
67    }
68
69    public function getName()
70    {
71        return $this->name;
72    }
73
74    public function setName(Name $name)
75    {
76        $this->name = $name;
77    }
78
79    public function addName($name, $usergroup = '')
80    {
81        $this->name->setValue($name, $usergroup);
82    }
83
84    public function getSummary()
85    {
86        return $this->summary;
87    }
88
89    public function setSummary(Summary $summary)
90    {
91        $this->summary = $summary;
92    }
93
94    public function addSummary($summary, $usergroup = '')
95    {
96        $this->summary->setValue($summary, $usergroup);
97    }
98
99    public function getDescription()
100    {
101        return $this->description;
102    }
103
104    public function setDescription(Description $description)
105    {
106        $this->description = $description;
107    }
108
109    public function addDescription($description, $usergroup = '')
110    {
111        $this->description->setValue($description, $usergroup);
112    }
113
114    public function getPrice()
115    {
116        return $this->price;
117    }
118
119    public function setPrice(Price $price)
120    {
121        $this->price = $price;
122    }
123
124    public function addPrice($price, $usergroup = '')
125    {
126        if ($this->price === null) {
127            $this->price = new Price();
128        }
129
130        $this->price->setValue($price, $usergroup);
131    }
132
133    public function getUrl()
134    {
135        return $this->url;
136    }
137
138    public function setUrl(Url $url)
139    {
140        $this->url = $url;
141    }
142
143    public function addUrl($url, $usergroup = '')
144    {
145        $this->url->setValue($url, $usergroup);
146    }
147
148    public function getBonus()
149    {
150        return $this->bonus;
151    }
152
153    public function setBonus(Bonus $bonus)
154    {
155        $this->bonus = $bonus;
156    }
157
158    public function addBonus($bonus, $usergroup = '')
159    {
160        $this->bonus->setValue($bonus, $usergroup);
161    }
162
163    public function getSalesFrequency()
164    {
165        return $this->salesFrequency;
166    }
167
168    public function setSalesFrequency(SalesFrequency $salesFrequency)
169    {
170        $this->salesFrequency = $salesFrequency;
171    }
172
173    public function addSalesFrequency($salesFrequency, $usergroup = '')
174    {
175        $this->salesFrequency->setValue($salesFrequency, $usergroup);
176    }
177
178    public function getDateAdded()
179    {
180        return $this->dateAdded;
181    }
182
183    public function setDateAdded(DateAdded $dateAdded)
184    {
185        $this->dateAdded = $dateAdded;
186    }
187
188    public function addDateAdded(DateTime $dateAdded, $usergroup = '')
189    {
190        $this->dateAdded->setDateValue($dateAdded, $usergroup);
191    }
192
193    public function getSort()
194    {
195        return $this->sort;
196    }
197
198    public function setSort(Sort $sort)
199    {
200        $this->sort = $sort;
201    }
202
203    public function addSort($sort, $usergroup = '')
204    {
205        $this->sort->setValue($sort, $usergroup);
206    }
207
208    public function addProperty(Property $property)
209    {
210        foreach ($property->getAllValues() as $usergroup => $value) {
211            if (!array_key_exists($usergroup, $this->properties)) {
212                $this->properties[$usergroup] = [];
213            }
214            // No need to check if there are duplicate values for a single property and usergroup, because
215            // Property::addValue() already takes care of that.
216
217            $this->properties[$usergroup][$property->getKey()] = $value;
218        }
219    }
220
221    public function addAttribute(Attribute $attribute)
222    {
223        $this->attributes[$attribute->getKey()] = $attribute;
224    }
225
226    public function addImage(Image $image)
227    {
228        if (!array_key_exists($image->getUsergroup(), $this->images)) {
229            $this->images[$image->getUsergroup()] = [];
230        }
231
232        array_push($this->images[$image->getUsergroup()], $image);
233    }
234
235    public function setAllImages(array $images)
236    {
237        foreach ($images as $image) {
238            $this->addImage($image);
239        }
240    }
241
242    public function addOrdernumber(Ordernumber $ordernumber)
243    {
244        $this->ordernumbers->addValue($ordernumber);
245    }
246
247    public function setAllOrdernumbers(array $ordernumbers)
248    {
249        $this->ordernumbers->setAllValues($ordernumbers);
250    }
251
252    public function addKeyword(Keyword $keyword)
253    {
254        $this->keywords->addValue($keyword);
255    }
256
257    public function setAllKeywords(array $keywords)
258    {
259        $this->keywords->setAllValues($keywords);
260    }
261
262    public function addUsergroup(Usergroup $usergroup)
263    {
264        array_push($this->usergroups, $usergroup);
265    }
266
267    public function setAllUsergroups(array $usergroups)
268    {
269        $this->usergroups = $usergroups;
270    }
271
272    /**
273     * @inheritdoc
274     */
275    abstract public function getDomSubtree(\DOMDocument $document);
276}
277