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
|
|
|
class Node implements NodeInterface |
17
|
|
|
{ |
18
|
|
|
use ElementsAwareTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \ArrayIterator |
22
|
|
|
*/ |
23
|
|
|
protected $categories; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $title; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $publicId; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $description; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \DateTime |
42
|
|
|
*/ |
43
|
|
|
protected $lastModified; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $link; |
49
|
|
|
|
50
|
91 |
|
public function __construct() |
51
|
|
|
{ |
52
|
91 |
|
$this->initElements(); |
53
|
91 |
|
$this->categories = new \ArrayIterator(); |
54
|
91 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $name element name |
58
|
|
|
* @param string $value element value |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
10 |
|
public function set($name, $value) |
62
|
|
|
{ |
63
|
10 |
|
$element = $this->newElement(); |
64
|
|
|
|
65
|
10 |
|
$element->setName($name); |
66
|
10 |
|
$element->setValue($value); |
67
|
|
|
|
68
|
10 |
|
$this->addElement($element); |
69
|
|
|
|
70
|
10 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* returns node's categories |
75
|
|
|
* |
76
|
|
|
* @return \ArrayIterator |
77
|
|
|
*/ |
78
|
9 |
|
public function getCategories() |
79
|
|
|
{ |
80
|
9 |
|
return $this->categories; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return \Generator |
85
|
|
|
*/ |
86
|
4 |
|
public function getCategoriesGenerator() |
87
|
|
|
{ |
88
|
4 |
|
foreach( $this->categories as $category ) { |
89
|
2 |
|
yield $category->getlabel(); |
90
|
3 |
|
} |
91
|
3 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* adds a category to the node |
95
|
|
|
* |
96
|
|
|
* @param \FeedIo\Feed\Node\CategoryInterface $category |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
11 |
|
public function addCategory(CategoryInterface $category) |
100
|
|
|
{ |
101
|
11 |
|
$this->categories->append($category); |
102
|
|
|
|
103
|
11 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* returns a new CategoryInterface |
108
|
|
|
* |
109
|
|
|
* @return \FeedIo\Feed\Node\CategoryInterface |
110
|
|
|
*/ |
111
|
5 |
|
public function newCategory() |
112
|
|
|
{ |
113
|
5 |
|
return new Category(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
16 |
|
public function getTitle() |
120
|
|
|
{ |
121
|
16 |
|
return $this->title; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $title |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
18 |
|
public function setTitle($title) |
129
|
|
|
{ |
130
|
18 |
|
$this->title = $title; |
131
|
|
|
|
132
|
18 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
8 |
|
public function getPublicId() |
139
|
|
|
{ |
140
|
8 |
|
return $this->publicId; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $publicId |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
10 |
|
public function setPublicId($publicId) |
148
|
|
|
{ |
149
|
10 |
|
$this->publicId = $publicId; |
150
|
|
|
|
151
|
10 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
9 |
|
public function getDescription() |
158
|
|
|
{ |
159
|
9 |
|
return $this->description; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $description |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
12 |
|
public function setDescription($description) |
167
|
|
|
{ |
168
|
12 |
|
$this->description = $description; |
169
|
|
|
|
170
|
12 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return \DateTime |
175
|
|
|
*/ |
176
|
14 |
|
public function getLastModified() |
177
|
|
|
{ |
178
|
14 |
|
return $this->lastModified; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param \DateTime $lastModified |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
18 |
|
public function setLastModified(\DateTime $lastModified) |
186
|
|
|
{ |
187
|
18 |
|
$this->lastModified = $lastModified; |
188
|
|
|
|
189
|
18 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
13 |
|
public function getLink() |
196
|
|
|
{ |
197
|
13 |
|
return $this->link; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $link |
202
|
|
|
* @return $this |
203
|
|
|
*/ |
204
|
15 |
|
public function setLink($link) |
205
|
|
|
{ |
206
|
15 |
|
$this->link = $link; |
207
|
|
|
|
208
|
15 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return array |
213
|
|
|
*/ |
214
|
3 |
|
public function toArray() |
215
|
|
|
{ |
216
|
3 |
|
$properties = get_object_vars($this); |
217
|
|
|
|
218
|
3 |
|
foreach( $properties as $name => $property ) { |
219
|
3 |
|
if ( $property instanceof \DateTime ) { |
220
|
2 |
|
$properties[$name] = $property->format(\DateTime::ATOM); |
221
|
2 |
|
} |
222
|
3 |
|
} |
223
|
|
|
|
224
|
3 |
|
$properties['elements'] = iterator_to_array($this->getElementsGenerator()); |
225
|
3 |
|
$properties['categories'] = iterator_to_array($this->getCategoriesGenerator()); |
226
|
|
|
|
227
|
3 |
|
return $properties; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|