|
2 | 2 |
|
3 | 3 | namespace Suin\RSSWriter; |
4 | 4 |
|
5 | | -use \Suin\RSSWriter\SimpleXMLElement; |
6 | | - |
7 | | -class Channel implements \Suin\RSSWriter\ChannelInterface |
| 5 | +/** |
| 6 | + * Class Channel |
| 7 | + * @package Suin\RSSWriter |
| 8 | + */ |
| 9 | +class Channel implements ChannelInterface |
8 | 10 | { |
9 | | - /** @var string */ |
10 | | - protected $title; |
11 | | - /** @var string */ |
12 | | - protected $url; |
13 | | - /** @var string */ |
14 | | - protected $description; |
15 | | - /** @var string */ |
16 | | - protected $language; |
17 | | - /** @var string */ |
18 | | - protected $copyright; |
19 | | - /** @var int */ |
20 | | - protected $pubDate; |
21 | | - /** @var int */ |
22 | | - protected $lastBuildDate; |
23 | | - /** @var int */ |
24 | | - protected $ttl; |
25 | | - /** @var \Suin\RSSWriter\ItemInterface[] */ |
26 | | - protected $items = array(); |
27 | | - |
28 | | - /** |
29 | | - * Set channel title |
30 | | - * @param string $title |
31 | | - * @return $this |
32 | | - */ |
33 | | - public function title($title) |
34 | | - { |
35 | | - $this->title = $title; |
36 | | - return $this; |
37 | | - } |
38 | | - |
39 | | - /** |
40 | | - * Set channel URL |
41 | | - * @param string $url |
42 | | - * @return $this |
43 | | - */ |
44 | | - public function url($url) |
45 | | - { |
46 | | - $this->url = $url; |
47 | | - return $this; |
48 | | - } |
49 | | - |
50 | | - /** |
51 | | - * Set channel description |
52 | | - * @param string $description |
53 | | - * @return $this |
54 | | - */ |
55 | | - public function description($description) |
56 | | - { |
57 | | - $this->description = $description; |
58 | | - return $this; |
59 | | - } |
60 | | - |
61 | | - /** |
62 | | - * Set ISO639 language code |
63 | | - * |
64 | | - * The language the channel is written in. This allows aggregators to group all |
65 | | - * Italian language sites, for example, on a single page. A list of allowable |
66 | | - * values for this element, as provided by Netscape, is here. You may also use |
67 | | - * values defined by the W3C. |
68 | | - * |
69 | | - * @param string $language |
70 | | - * @return $this |
71 | | - */ |
72 | | - public function language($language) |
73 | | - { |
74 | | - $this->language = $language; |
75 | | - return $this; |
76 | | - } |
77 | | - |
78 | | - /** |
79 | | - * Set channel copyright |
80 | | - * @param string $copyright |
81 | | - * @return $this |
82 | | - */ |
83 | | - public function copyright($copyright) |
84 | | - { |
85 | | - $this->copyright = $copyright; |
86 | | - return $this; |
87 | | - } |
88 | | - |
89 | | - /** |
90 | | - * Set channel published date |
91 | | - * @param int $pubDate Unix timestamp |
92 | | - * @return $this |
93 | | - */ |
94 | | - public function pubDate($pubDate) |
95 | | - { |
96 | | - $this->pubDate = $pubDate; |
97 | | - return $this; |
98 | | - } |
99 | | - |
100 | | - /** |
101 | | - * Set channel last build date |
102 | | - * @param int $lastBuildDate Unix timestamp |
103 | | - * @return $this |
104 | | - */ |
105 | | - public function lastBuildDate($lastBuildDate) |
106 | | - { |
107 | | - $this->lastBuildDate = $lastBuildDate; |
108 | | - return $this; |
109 | | - } |
110 | | - |
111 | | - /** |
112 | | - * Set channel ttl (minutes) |
113 | | - * @param int $ttl |
114 | | - * @return $this |
115 | | - */ |
116 | | - public function ttl($ttl) |
117 | | - { |
118 | | - $this->ttl = $ttl; |
119 | | - return $this; |
120 | | - } |
121 | | - |
122 | | - /** |
123 | | - * Add item object |
124 | | - * @param \Suin\RSSWriter\ItemInterface $item |
125 | | - * @return $this |
126 | | - */ |
127 | | - public function addItem(ItemInterface $item) |
128 | | - { |
129 | | - $this->items[] = $item; |
130 | | - return $this; |
131 | | - } |
132 | | - |
133 | | - /** |
134 | | - * Append to feed |
135 | | - * @param \Suin\RSSWriter\FeedInterface $feed |
136 | | - * @return $this |
137 | | - */ |
138 | | - public function appendTo(FeedInterface $feed) |
139 | | - { |
140 | | - $feed->addChannel($this); |
141 | | - return $this; |
142 | | - } |
143 | | - |
144 | | - /** |
145 | | - * Return XML object |
146 | | - * @return \Suin\RSSWriter\SimpleXMLElement |
147 | | - */ |
148 | | - public function asXML() |
149 | | - { |
150 | | - $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><channel></channel>', LIBXML_NOERROR|LIBXML_ERR_NONE|LIBXML_ERR_FATAL); |
151 | | - $xml->addChild('title', $this->title); |
152 | | - $xml->addChild('link', $this->url); |
153 | | - $xml->addChild('description', $this->description); |
154 | | - |
155 | | - if ( $this->language !== null ) |
156 | | - { |
157 | | - $xml->addChild('language', $this->language); |
158 | | - } |
159 | | - |
160 | | - if ( $this->copyright !== null ) |
161 | | - { |
162 | | - $xml->addChild('copyright', $this->copyright); |
163 | | - } |
164 | | - |
165 | | - if ( $this->pubDate !== null ) |
166 | | - { |
167 | | - $xml->addChild('pubDate', date(DATE_RSS, $this->pubDate)); |
168 | | - } |
169 | | - |
170 | | - if ( $this->lastBuildDate !== null ) |
171 | | - { |
172 | | - $xml->addChild('lastBuildDate', date(DATE_RSS, $this->lastBuildDate)); |
173 | | - } |
174 | | - |
175 | | - if ( $this->ttl !== null ) |
176 | | - { |
177 | | - $xml->addChild('ttl', $this->ttl); |
178 | | - } |
179 | | - |
180 | | - foreach ( $this->items as $item ) |
181 | | - { |
182 | | - $toDom = dom_import_simplexml($xml); |
183 | | - $fromDom = dom_import_simplexml($item->asXML()); |
184 | | - $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
185 | | - } |
186 | | - |
187 | | - return $xml; |
188 | | - } |
| 11 | + /** @var string */ |
| 12 | + protected $title; |
| 13 | + |
| 14 | + /** @var string */ |
| 15 | + protected $url; |
| 16 | + |
| 17 | + /** @var string */ |
| 18 | + protected $description; |
| 19 | + |
| 20 | + /** @var string */ |
| 21 | + protected $language; |
| 22 | + |
| 23 | + /** @var string */ |
| 24 | + protected $copyright; |
| 25 | + |
| 26 | + /** @var int */ |
| 27 | + protected $pubDate; |
| 28 | + |
| 29 | + /** @var int */ |
| 30 | + protected $lastBuildDate; |
| 31 | + |
| 32 | + /** @var int */ |
| 33 | + protected $ttl; |
| 34 | + |
| 35 | + /** @var ItemInterface[] */ |
| 36 | + protected $items = array(); |
| 37 | + |
| 38 | + /** |
| 39 | + * Set channel title |
| 40 | + * @param string $title |
| 41 | + * @return $this |
| 42 | + */ |
| 43 | + public function title($title) |
| 44 | + { |
| 45 | + $this->title = $title; |
| 46 | + return $this; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Set channel URL |
| 51 | + * @param string $url |
| 52 | + * @return $this |
| 53 | + */ |
| 54 | + public function url($url) |
| 55 | + { |
| 56 | + $this->url = $url; |
| 57 | + return $this; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Set channel description |
| 62 | + * @param string $description |
| 63 | + * @return $this |
| 64 | + */ |
| 65 | + public function description($description) |
| 66 | + { |
| 67 | + $this->description = $description; |
| 68 | + return $this; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Set ISO639 language code |
| 73 | + * |
| 74 | + * The language the channel is written in. This allows aggregators to group all |
| 75 | + * Italian language sites, for example, on a single page. A list of allowable |
| 76 | + * values for this element, as provided by Netscape, is here. You may also use |
| 77 | + * values defined by the W3C. |
| 78 | + * |
| 79 | + * @param string $language |
| 80 | + * @return $this |
| 81 | + */ |
| 82 | + public function language($language) |
| 83 | + { |
| 84 | + $this->language = $language; |
| 85 | + return $this; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Set channel copyright |
| 90 | + * @param string $copyright |
| 91 | + * @return $this |
| 92 | + */ |
| 93 | + public function copyright($copyright) |
| 94 | + { |
| 95 | + $this->copyright = $copyright; |
| 96 | + return $this; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Set channel published date |
| 101 | + * @param int $pubDate Unix timestamp |
| 102 | + * @return $this |
| 103 | + */ |
| 104 | + public function pubDate($pubDate) |
| 105 | + { |
| 106 | + $this->pubDate = $pubDate; |
| 107 | + return $this; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Set channel last build date |
| 112 | + * @param int $lastBuildDate Unix timestamp |
| 113 | + * @return $this |
| 114 | + */ |
| 115 | + public function lastBuildDate($lastBuildDate) |
| 116 | + { |
| 117 | + $this->lastBuildDate = $lastBuildDate; |
| 118 | + return $this; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Set channel ttl (minutes) |
| 123 | + * @param int $ttl |
| 124 | + * @return $this |
| 125 | + */ |
| 126 | + public function ttl($ttl) |
| 127 | + { |
| 128 | + $this->ttl = $ttl; |
| 129 | + return $this; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Add item object |
| 134 | + * @param ItemInterface $item |
| 135 | + * @return $this |
| 136 | + */ |
| 137 | + public function addItem(ItemInterface $item) |
| 138 | + { |
| 139 | + $this->items[] = $item; |
| 140 | + return $this; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Append to feed |
| 145 | + * @param FeedInterface $feed |
| 146 | + * @return $this |
| 147 | + */ |
| 148 | + public function appendTo(FeedInterface $feed) |
| 149 | + { |
| 150 | + $feed->addChannel($this); |
| 151 | + return $this; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Return XML object |
| 156 | + * @return SimpleXMLElement |
| 157 | + */ |
| 158 | + public function asXML() |
| 159 | + { |
| 160 | + $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><channel></channel>', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL); |
| 161 | + $xml->addChild('title', $this->title); |
| 162 | + $xml->addChild('link', $this->url); |
| 163 | + $xml->addChild('description', $this->description); |
| 164 | + |
| 165 | + if ($this->language !== null) { |
| 166 | + $xml->addChild('language', $this->language); |
| 167 | + } |
| 168 | + |
| 169 | + if ($this->copyright !== null) { |
| 170 | + $xml->addChild('copyright', $this->copyright); |
| 171 | + } |
| 172 | + |
| 173 | + if ($this->pubDate !== null) { |
| 174 | + $xml->addChild('pubDate', date(DATE_RSS, $this->pubDate)); |
| 175 | + } |
| 176 | + |
| 177 | + if ($this->lastBuildDate !== null) { |
| 178 | + $xml->addChild('lastBuildDate', date(DATE_RSS, $this->lastBuildDate)); |
| 179 | + } |
| 180 | + |
| 181 | + if ($this->ttl !== null) { |
| 182 | + $xml->addChild('ttl', $this->ttl); |
| 183 | + } |
| 184 | + |
| 185 | + foreach ($this->items as $item) { |
| 186 | + $toDom = dom_import_simplexml($xml); |
| 187 | + $fromDom = dom_import_simplexml($item->asXML()); |
| 188 | + $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
| 189 | + } |
| 190 | + |
| 191 | + return $xml; |
| 192 | + } |
189 | 193 | } |
0 commit comments