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 Atom 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: $atom = $this->DOM->createElement('feed');
70: $atom->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');
71: $atom->setAttribute('xml:lang', $feed->getLang());
72: $this->DOM->appendChild($atom);
73:
74: $this->buildFeedInfo($atom);
75: $this->buildItems($atom);
76: }
77:
78: 79: 80: 81: 82: 83:
84: private function buildFeedInfo($atom)
85: {
86: $id = $this->DOM->createElement('id', $this->feed->getId());
87: $title = $this->DOM->createElement('title', $this->feed->getTitle());
88: $date = $this->DOM->createElement('updated', $this->convertDate($this->feed->getDate()));
89:
90: $atom->appendChild($id);
91: $atom->appendChild($title);
92: $atom->appendChild($date);
93:
94: $dLink = $this->feed->getLink();
95: if (!empty($dLink))
96: {
97: $link = $this->DOM->createElement('link');
98: $link->setAttribute('rel', 'alternate');
99: $link->setAttribute('href', $dLink);
100: $atom->appendChild($link);
101: }
102:
103: $eAuthor = $this->feed->getAuthor();
104: if (!empty($eAuthor))
105: {
106: $author = $this->DOM->createElement('author');
107: $atom->appendChild($author);
108: if (key_exists('name', $eAuthor))
109: {
110: $name = $this->DOM->createElement('name', $eAuthor['name']);
111: $author->appendChild($name);
112: }
113:
114: if (key_exists('email', $eAuthor))
115: {
116: $email = $this->DOM->createElement('email', $eAuthor['email']);
117: $author->appendChild($email);
118: }
119:
120: if (key_exists('uri', $eAuthor))
121: {
122: $uri = $this->DOM->createElement('uri', $eAuthor['uri']);
123: $author->appendChild($uri);
124: }
125: }
126: }
127:
128: 129: 130: 131: 132: 133:
134: private function buildItems($atom)
135: {
136: if (!empty($this->entries))
137: {
138: foreach ($this->entries as $entry) {
139: $item = $this->DOM->createElement('entry');
140: $atom->appendChild($item);
141:
142: $id = $this->DOM->createElement('id', $entry->getId());
143: $title = $this->DOM->createElement('title', $entry->getTitle());
144: $date = $this->DOM->createElement('updated', $this->convertDate($entry->getDate()));
145:
146: $item->appendChild($id);
147: $item->appendChild($title);
148: $item->appendChild($date);
149:
150: $eAuthor = $entry->getAuthor();
151: if (!empty($eAuthor))
152: {
153: $author = $this->DOM->createElement('author', $eAuthor);
154: $item->appendChild($author);
155: if (key_exists('name', $eAuthor))
156: {
157: $name = $this->DOM->createElement('name', $eAuthor['name']);
158: $author->appendChild($name);
159: }
160:
161: if (key_exists('email', $eAuthor))
162: {
163: $email = $this->DOM->createElement('email', $eAuthor['email']);
164: $author->appendChild($email);
165: }
166:
167: if (key_exists('uri', $eAuthor))
168: {
169: $uri = $this->DOM->createElement('uri', $eAuthor['uri']);
170: $author->appendChild($uri);
171: }
172: }
173:
174: $dContent = $entry->getContent();
175: if (!empty($dContent))
176: {
177:
178: $content = $this->DOM->createElement('content');
179: $cdata = $this->DOM->createCDATASection($dContent);
180: $content->appendChild($cdata);
181: $item->appendChild($content);
182: }
183:
184: $dLink = $entry->getLink();
185: if (!empty($dLink))
186: {
187: $link = $this->DOM->createElement('link');
188: $link->setAttribute('href', $dLink);
189: $item->appendChild($link);
190: }
191: }
192: }
193: }
194:
195: 196: 197: 198: 199:
200: private function convertDate($date)
201: {
202: if (is_int($date))
203: {
204: return date('c', $date);
205: }
206: elseif (preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}$`', $date))
207: {
208: $datetime = explode(' ', $date);
209: list($annee, $mois, $jour) = explode('/', $datetime[0]);
210: list($heure, $min, $sec) = explode(':', $datetime[1]);
211: return date('c', mktime($heure, $min, $sec, $mois, $jour, $annee));
212: }
213: else
214: {
215: return $date;
216: }
217: }
218:
219: 220: 221: 222:
223: public function finalize()
224: {
225: if (!empty($this->file))
226: {
227: if (file_exists($this->file) && is_writable($this->file))
228: return $this->DOM->save($this->file);
229: else
230: throw new \Exception('Impossible d\'ecrire dans le fichier');
231: }
232: else
233: {
234: return $this->DOM->saveXML();
235: }
236: }
237:
238: }
239:
240: ?>