1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the feed-io package. |
4
|
|
|
* |
5
|
|
|
* (c) Alexandre Debril <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace FeedIo\Feed; |
12
|
|
|
|
13
|
|
|
use FeedIo\Feed\Node\Category; |
14
|
|
|
use FeedIo\Feed\Node\CategoryInterface; |
15
|
|
|
|
16
|
|
|
use FeedIo\Feed\Node\Element; |
17
|
|
|
use FeedIo\Feed\Node\ElementIterator; |
18
|
|
|
use FeedIo\Feed\Node\ElementInterface; |
19
|
|
|
|
20
|
|
|
class Node implements NodeInterface |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \ArrayIterator |
25
|
|
|
*/ |
26
|
|
|
protected $elements; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \ArrayIterator |
30
|
|
|
*/ |
31
|
|
|
protected $categories; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $title; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $publicId; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $description; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \DateTime |
50
|
|
|
*/ |
51
|
|
|
protected $lastModified; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $link; |
57
|
|
|
|
58
|
|
|
public function __construct() |
59
|
|
|
{ |
60
|
|
|
$this->elements = new \ArrayIterator(); |
61
|
|
|
$this->categories = new \ArrayIterator(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $name element name |
66
|
|
|
* @param string $value element value |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function set($name, $value) |
70
|
|
|
{ |
71
|
|
|
$element = $this->newElement(); |
72
|
|
|
|
73
|
|
|
$element->setName($name); |
74
|
|
|
$element->setValue($value); |
75
|
|
|
|
76
|
|
|
$this->addElement($element); |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* returns node's categories |
83
|
|
|
* |
84
|
|
|
* @return \ArrayIterator |
85
|
|
|
*/ |
86
|
|
|
public function getCategories() |
87
|
|
|
{ |
88
|
|
|
return $this->categories; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return \Generator |
93
|
|
|
*/ |
94
|
|
|
public function getCategoriesGenerator() |
95
|
|
|
{ |
96
|
|
|
foreach( $this->categories as $category ) { |
97
|
|
|
yield $category->getlabel(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* adds a category to the node |
103
|
|
|
* |
104
|
|
|
* @param \FeedIo\Feed\Node\CategoryInterface $category |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function addCategory(CategoryInterface $category) |
108
|
|
|
{ |
109
|
|
|
$this->categories->append($category); |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* returns a new CategoryInterface |
116
|
|
|
* |
117
|
|
|
* @return \FeedIo\Feed\Node\CategoryInterface |
118
|
|
|
*/ |
119
|
|
|
public function newCategory() |
120
|
|
|
{ |
121
|
|
|
return new Category(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return ElementInterface |
126
|
|
|
*/ |
127
|
|
|
public function newElement() |
128
|
|
|
{ |
129
|
|
|
return new Element(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $name element name |
134
|
|
|
* @return mixed |
135
|
|
|
*/ |
136
|
|
|
public function getValue($name) |
137
|
|
|
{ |
138
|
|
|
foreach ($this->getElementIterator($name) as $element) { |
139
|
|
|
return $element->getValue(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $name element name |
147
|
|
|
* @return ElementIterator |
148
|
|
|
*/ |
149
|
|
|
public function getElementIterator($name) |
150
|
|
|
{ |
151
|
|
|
return new ElementIterator($this->elements, $name); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $name element name |
156
|
|
|
* @return boolean true if the element exists |
157
|
|
|
*/ |
158
|
|
|
public function hasElement($name) |
159
|
|
|
{ |
160
|
|
|
$filter = $this->getElementIterator($name); |
161
|
|
|
|
162
|
|
|
return $filter->count() > 0; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param ElementInterface $element |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
|
|
public function addElement(ElementInterface $element) |
170
|
|
|
{ |
171
|
|
|
$this->elements->append($element); |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns all the item's optional elements |
178
|
|
|
* @return \ArrayIterator |
179
|
|
|
*/ |
180
|
|
|
public function getAllElements() |
181
|
|
|
{ |
182
|
|
|
return $this->elements; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns the item's optional elements tag names |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
public function listElements() |
190
|
|
|
{ |
191
|
|
|
foreach ($this->elements as $element) { |
192
|
|
|
yield ($element->getName()); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return \Generator |
198
|
|
|
*/ |
199
|
|
|
public function getElementsGenerator() |
200
|
|
|
{ |
201
|
|
|
$elements = $this->getAllElements(); |
202
|
|
|
|
203
|
|
|
foreach( $elements as $element ) { |
204
|
|
|
yield $element->getName() => $element->getValue(); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function getTitle() |
212
|
|
|
{ |
213
|
|
|
return $this->title; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $title |
218
|
|
|
* @return $this |
219
|
|
|
*/ |
220
|
|
|
public function setTitle($title) |
221
|
|
|
{ |
222
|
|
|
$this->title = $title; |
223
|
|
|
|
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function getPublicId() |
231
|
|
|
{ |
232
|
|
|
return $this->publicId; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param string $publicId |
237
|
|
|
* @return $this |
238
|
|
|
*/ |
239
|
|
|
public function setPublicId($publicId) |
240
|
|
|
{ |
241
|
|
|
$this->publicId = $publicId; |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
public function getDescription() |
250
|
|
|
{ |
251
|
|
|
return $this->description; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param string $description |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
|
|
public function setDescription($description) |
259
|
|
|
{ |
260
|
|
|
$this->description = $description; |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return \DateTime |
267
|
|
|
*/ |
268
|
|
|
public function getLastModified() |
269
|
|
|
{ |
270
|
|
|
return $this->lastModified; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param \DateTime $lastModified |
275
|
|
|
* @return $this |
276
|
|
|
*/ |
277
|
|
|
public function setLastModified(\DateTime $lastModified) |
278
|
|
|
{ |
279
|
|
|
$this->lastModified = $lastModified; |
280
|
|
|
|
281
|
|
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @return string |
286
|
|
|
*/ |
287
|
|
|
public function getLink() |
288
|
|
|
{ |
289
|
|
|
return $this->link; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @param string $link |
294
|
|
|
* @return $this |
295
|
|
|
*/ |
296
|
|
|
public function setLink($link) |
297
|
|
|
{ |
298
|
|
|
$this->link = $link; |
299
|
|
|
|
300
|
|
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @return array |
305
|
|
|
*/ |
306
|
|
|
public function toArray() |
307
|
|
|
{ |
308
|
|
|
$properties = get_object_vars($this); |
309
|
|
|
|
310
|
|
|
$properties['elements'] = iterator_to_array($this->getElementsGenerator()); |
311
|
|
|
$properties['categories'] = iterator_to_array($this->getCategoriesGenerator()); |
312
|
|
|
|
313
|
|
|
return $properties; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
} |
317
|
|
|
|