1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Nexendrie\Rss; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Rss Channel Item |
8
|
|
|
* |
9
|
|
|
* @author Jakub Konečný |
10
|
|
|
* @property string $title |
11
|
|
|
* @property string $description |
12
|
|
|
* @property string $link |
13
|
|
|
* @property int $pubDate |
14
|
|
|
* @property string $author |
15
|
|
|
* @property string $comments |
16
|
|
|
* @property string $guid |
17
|
|
|
* @property string $sourceUrl |
18
|
|
|
* @property string $sourceTitle |
19
|
|
|
* @property \Nexendrie\Utils\Collection|Category[] $categories |
20
|
|
|
* @property \Nexendrie\Utils\Collection|Enclosure[] $enclosures |
21
|
|
|
*/ |
22
|
1 |
|
class RssChannelItem { |
23
|
1 |
|
use \Nette\SmartObject; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $title; |
27
|
|
|
/** @var string */ |
28
|
|
|
protected $description; |
29
|
|
|
/** @var string */ |
30
|
|
|
protected $link; |
31
|
|
|
/** @var int */ |
32
|
|
|
protected $pubDate; |
33
|
|
|
/** @var string */ |
34
|
|
|
protected $author = ""; |
35
|
|
|
/** @var string */ |
36
|
|
|
protected $comments = ""; |
37
|
|
|
/** @var string */ |
38
|
|
|
protected $guid = ""; |
39
|
|
|
/** @var \stdClass */ |
40
|
|
|
protected $source; |
41
|
|
|
/** @var \Nexendrie\Utils\Collection|Category[] */ |
42
|
|
|
protected $categories; |
43
|
|
|
/** @var \Nexendrie\Utils\Collection|Enclosure[] */ |
44
|
|
|
protected $enclosures; |
45
|
|
|
|
46
|
|
|
public function __construct(string $title, string $description, string $link, int $pubDate) { |
47
|
1 |
|
$this->title = $title; |
48
|
1 |
|
$this->description = $description; |
49
|
1 |
|
$this->link = $link; |
50
|
1 |
|
$this->pubDate = $pubDate; |
51
|
1 |
|
$this->categories = new class extends \Nexendrie\Utils\Collection implements IXmlConvertible { |
52
|
|
|
/** @var string */ |
53
|
|
|
protected $class = Category::class; |
54
|
|
|
|
55
|
|
|
public function appendToXml(\SimpleXMLElement &$parent): void { |
56
|
1 |
|
array_walk($this->items, function(Category $value) use($parent) { |
57
|
1 |
|
$value->appendToXml($parent); |
58
|
1 |
|
}); |
59
|
1 |
|
} |
60
|
|
|
}; |
61
|
1 |
|
$this->enclosures = new class extends \Nexendrie\Utils\Collection implements IXmlConvertible { |
62
|
|
|
/** @var string */ |
63
|
|
|
protected $class = Enclosure::class; |
64
|
|
|
|
65
|
|
|
public function appendToXml(\SimpleXMLElement &$parent): void { |
66
|
1 |
|
array_walk($this->items, function(Enclosure $value) use($parent) { |
67
|
1 |
|
$value->appendToXml($parent); |
68
|
1 |
|
}); |
69
|
1 |
|
} |
70
|
|
|
}; |
71
|
1 |
|
$this->source = new class extends \stdClass implements IXmlConvertible { |
72
|
|
|
/** @var string */ |
73
|
|
|
public $url = ""; |
74
|
|
|
/** @var string */ |
75
|
|
|
public $title = ""; |
76
|
|
|
|
77
|
|
|
public function appendToXml(\SimpleXMLElement &$parent): void { |
78
|
1 |
|
if($this->url !== "") { |
79
|
1 |
|
$element = $parent->addChild("source", $this->title); |
80
|
1 |
|
$element->addAttribute("url", $this->url); |
81
|
|
|
} |
82
|
1 |
|
} |
83
|
|
|
}; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
public function getTitle(): string { |
87
|
1 |
|
return $this->title; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function setTitle(string $title): void { |
91
|
1 |
|
$this->title = $title; |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
public function getDescription(): string { |
95
|
1 |
|
return $this->description; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function setDescription(string $description): void { |
99
|
1 |
|
$this->description = $description; |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
public function getLink(): string { |
103
|
1 |
|
return $this->link; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function setLink(string $link): void { |
107
|
1 |
|
$this->link = $link; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
public function getPubDate(): int { |
111
|
1 |
|
return $this->pubDate; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function setPubDate(int $pubDate): void { |
115
|
1 |
|
$this->pubDate = $pubDate; |
116
|
1 |
|
} |
117
|
|
|
|
118
|
|
|
public function getAuthor(): string { |
119
|
1 |
|
return $this->author; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setAuthor(string $author): void { |
123
|
1 |
|
$this->author = $author; |
124
|
1 |
|
} |
125
|
|
|
|
126
|
|
|
public function getComments(): string { |
127
|
1 |
|
return $this->comments; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function setComments(string $comments): void { |
131
|
1 |
|
$this->comments = $comments; |
132
|
1 |
|
} |
133
|
|
|
|
134
|
|
|
public function getGuid(): string { |
135
|
1 |
|
return $this->guid; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function setGuid(string $guid): void { |
139
|
1 |
|
$this->guid = $guid; |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
public function getSourceUrl(): string { |
143
|
1 |
|
return $this->source->url; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function setSourceUrl(string $sourceUrl): void { |
147
|
1 |
|
$this->source->url = $sourceUrl; |
148
|
1 |
|
} |
149
|
|
|
|
150
|
|
|
public function getSourceTitle(): string { |
151
|
1 |
|
return $this->source->title; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function setSourceTitle(string $sourceTitle): void { |
155
|
1 |
|
$this->source->title = $sourceTitle; |
156
|
1 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return \Nexendrie\Utils\Collection|Category[] |
160
|
|
|
*/ |
161
|
|
|
public function getCategories(): \Nexendrie\Utils\Collection { |
162
|
1 |
|
return $this->categories; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return \Nexendrie\Utils\Collection|Enclosure[] |
167
|
|
|
*/ |
168
|
|
|
public function getEnclosures(): \Nexendrie\Utils\Collection { |
169
|
1 |
|
return $this->enclosures; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
protected function shortenDescription(string $description, int $maxLength): string { |
173
|
1 |
|
if($maxLength < 1) { |
174
|
1 |
|
return $description; |
175
|
|
|
} |
176
|
1 |
|
$originalDescription = $description; |
177
|
1 |
|
$description = substr($description, 0, $maxLength); |
178
|
1 |
|
if($description !== $originalDescription) { |
179
|
1 |
|
$description .= "..."; |
180
|
|
|
} |
181
|
1 |
|
return $description; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param mixed $value |
186
|
|
|
* @return mixed |
187
|
|
|
*/ |
188
|
|
|
protected function normalizeValue(string $name, $value, Generator $generator) { |
189
|
1 |
|
switch($name) { |
190
|
1 |
|
case "pubDate": |
191
|
1 |
|
return date($generator->dateTimeFormat, $value); |
192
|
1 |
|
case "description": |
193
|
1 |
|
return $this->shortenDescription($value, $generator->shortenDescription); |
194
|
|
|
default: |
195
|
1 |
|
return $value; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function toXml(\SimpleXMLElement &$element, Generator $generator): void { |
200
|
1 |
|
$properties = array_keys(get_object_vars($this)); |
201
|
1 |
|
foreach($properties as $property) { |
202
|
1 |
|
if($this->$property === "") { |
203
|
1 |
|
continue; |
204
|
|
|
} |
205
|
1 |
|
$value = $this->normalizeValue($property, $this->$property, $generator); |
206
|
1 |
|
if($value instanceof IXmlConvertible) { |
207
|
1 |
|
$value->appendToXml($element); |
208
|
|
|
} else { |
209
|
1 |
|
$element->addChild($property, $value); |
210
|
|
|
} |
211
|
|
|
} |
212
|
1 |
|
} |
213
|
|
|
} |
214
|
|
|
?> |