1 | <?php |
||
18 | 1 | class Generator { |
|
19 | 1 | use \Nette\SmartObject; |
|
20 | |||
21 | /** @var string */ |
||
22 | protected $title = ""; |
||
23 | /** @var string */ |
||
24 | protected $description = ""; |
||
25 | /** @var string */ |
||
26 | protected $link = ""; |
||
27 | /** @var string */ |
||
28 | protected $dateTimeFormat = "Y-m-d H:i:s"; |
||
29 | /** @var callable|null */ |
||
30 | protected $dataSource = NULL; |
||
31 | /** @var int */ |
||
32 | protected $shortenDescription = 150; |
||
33 | /** @var callable */ |
||
34 | protected $lastBuildDate = "time"; |
||
35 | |||
36 | public function getTitle(): string { |
||
39 | |||
40 | public function setTitle(string $title) { |
||
43 | |||
44 | public function getDescription(): string { |
||
47 | |||
48 | public function setDescription(string $description) { |
||
51 | |||
52 | public function getLink(): string { |
||
55 | |||
56 | public function setLink(string $link) { |
||
59 | |||
60 | public function setDataSource(callable $dataSource) { |
||
63 | |||
64 | public function getShortenDescription(): int { |
||
67 | |||
68 | public function setShortenDescription(int $value) { |
||
71 | |||
72 | public function getDateTimeFormat(): string { |
||
75 | |||
76 | public function setDateTimeFormat(string $format) { |
||
79 | |||
80 | public function getLastBuildDate() { |
||
83 | |||
84 | public function setLastBuildDate(callable $lastBuildDate) { |
||
87 | |||
88 | /** |
||
89 | * @throws InvalidStateException |
||
90 | * @throws \InvalidArgumentException |
||
91 | */ |
||
92 | protected function getData(): Collection { |
||
102 | |||
103 | protected function shortenDescription(string $description): string { |
||
114 | |||
115 | protected function writeProperty(\SimpleXMLElement &$channel, string $property): void { |
||
116 | 1 | if(isset($this->$property) AND $this->$property !== "") { |
|
117 | 1 | $channel->channel->{$property}[0][0] = $this->$property; |
|
118 | } |
||
119 | 1 | } |
|
120 | |||
121 | /** |
||
122 | * @throws InvalidStateException |
||
123 | * @throws \InvalidArgumentException |
||
124 | */ |
||
125 | public function generate(): \SimpleXMLElement { |
||
126 | 1 | $items = $this->getData(); |
|
127 | 1 | $lastBuildDate = call_user_func($this->lastBuildDate); |
|
128 | 1 | if(!is_int($lastBuildDate)) { |
|
129 | 1 | throw new \InvalidArgumentException("Callback for last build date for RSS generator has to return integer."); |
|
130 | } |
||
131 | 1 | $channel = simplexml_load_file(__DIR__ . "/template.xml"); |
|
132 | 1 | $channel->channel->lastBuildDate[0][0] = date($this->dateTimeFormat, $lastBuildDate); |
|
133 | 1 | $this->writeProperty($channel, "link"); |
|
134 | 1 | $this->writeProperty($channel, "title"); |
|
135 | 1 | $this->writeProperty($channel, "description"); |
|
136 | /** @var RssChannelItem $item */ |
||
137 | 1 | foreach($items as $item) { |
|
138 | /** @var \SimpleXMLElement $i */ |
||
139 | 1 | $i = $channel->channel->addChild("item"); |
|
140 | 1 | $i->addChild("title", $item->title); |
|
141 | 1 | $i->addChild("link", $item->link); |
|
142 | 1 | $i->addChild("pubDate", $item->pubDate); |
|
143 | 1 | $i->addChild("description", $this->shortenDescription($item->description)); |
|
144 | } |
||
145 | 1 | return $channel; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * @throws InvalidStateException |
||
150 | * @throws \InvalidArgumentException |
||
151 | */ |
||
152 | public function response(): RssResponse { |
||
159 | } |
||
160 | ?> |