1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Pry\Feed\Writers;
14:
15: use Pry\Feed\Feed;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class implements Interfaces
27: {
28:
29: 30: 31: 32: 33:
34: private $feed;
35:
36: 37: 38: 39: 40:
41: private $entries;
42:
43: 44: 45: 46: 47:
48: private $DOM;
49:
50: 51: 52: 53: 54:
55: private $file;
56:
57: 58: 59: 60: 61:
62: public function __construct(Feed $feed, $fichier = null)
63: {
64: $this->feed = $feed;
65: $this->entries = $feed->getEntries();
66: $this->DOM = new \DOMDocument('1.0', 'UTF-8');
67: $this->file = $fichier;
68:
69: $rss = $this->DOM->createElement('rss');
70: $rss->setAttribute('version', '2.0');
71: $this->DOM->appendChild($rss);
72:
73: $channel = $this->DOM->createElement('channel');
74: $rss->appendChild($channel);
75:
76: $this->buildFeedInfo($channel);
77: $this->buildItems($channel);
78: }
79:
80: 81: 82: 83: 84: 85:
86: private function buildFeedInfo($channel)
87: {
88: $title = $this->DOM->createElement('title', $this->feed->getTitle());
89: $link = $this->DOM->createElement('link', $this->feed->getLink());
90: $desc = $this->DOM->createElement('description');
91: $cdata = $this->DOM->createCDATASection($this->feed->getDescription());
92: $lang = $this->DOM->createElement('language', $this->feed->getLang());
93: $date = $this->DOM->createElement('lastBuildDate', $this->feed->getDate());
94: $copy = $this->DOM->createElement('copyright', $this->feed->getCopyright());
95:
96: $channel->appendChild($title);
97: $channel->appendChild($link);
98: $desc->appendChild($cdata);
99: $channel->appendChild($desc);
100: $channel->appendChild($lang);
101: $channel->appendChild($date);
102: $channel->appendChild($copy);
103: }
104:
105: 106: 107: 108: 109: 110:
111: private function buildItems($channel)
112: {
113: if (!empty($this->entries))
114: {
115: foreach ($this->entries as $entry) {
116: $item = $this->DOM->createElement('item');
117: $channel->appendChild($item);
118: $title = $this->DOM->createElement('title', $entry->getTitle());
119: $link = $this->DOM->createElement('link', $entry->getLink());
120: $desc = $this->DOM->createElement('description');
121: $cdata = $this->DOM->createCDATASection($entry->getContent());
122: $desc->appendChild($cdata);
123: $item->appendChild($title);
124: $item->appendChild($link);
125: $item->appendChild($desc);
126:
127:
128: $eAuthor = $entry->getAuthor('name');
129: if (!empty($eAuthor))
130: {
131: $author = $this->DOM->createElement('author', $eAuthor);
132: $item->appendChild($author);
133: }
134:
135: $eDate = $entry->getDate();
136: if (!empty($eDate))
137: {
138: $date = $this->DOM->createElement('pubDate', $eDate);
139: $item->appendChild($date);
140: }
141:
142: $eCom = $entry->getComments();
143: if (!empty($eCom))
144: {
145: $com = $this->DOM->createElement('comments', $eCom);
146: $item->appendChild($com);
147: }
148:
149: $eId = $entry->getId();
150: if (!empty($eId))
151: {
152: $guid = $this->DOM->createElement('guid', $eId);
153: $item->appendChild($guid);
154: }
155: }
156: }
157: }
158:
159: 160: 161: 162: 163:
164: private function convertDate($date)
165: {
166: if (is_int($date))
167: {
168: return date('r', $date);
169: }
170: elseif (preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}$`', $date))
171: {
172: $datetime = explode(' ', $date);
173: list($annee, $mois, $jour) = explode('/', $datetime[0]);
174: list($heure, $min, $sec) = explode(':', $datetime[1]);
175: return date('r', mktime($heure, $min, $sec, $mois, $jour, $annee));
176: }
177: else
178: {
179: return $date;
180: }
181: }
182:
183: 184: 185: 186:
187: public function finalize()
188: {
189: if (!empty($this->file))
190: {
191: if (file_exists($this->file) && is_writable($this->file))
192: return $this->DOM->save($this->file);
193: else
194: throw new \Exception('Impossible d\'ecrire dans le fichier');
195: }
196: else
197: {
198: return $this->DOM->saveXML();
199: }
200: }
201:
202: }
203:
204: ?>