root/trunk/lib/sfFeed/sfFeed.class.php

Revision 2, 10.2 kB (checked in by fabien, 6 years ago)

initial import

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfFeed.
13  *
14  * based on feedgenerator.py from django project
15  *
16  * @package    symfony
17  * @subpackage addon
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 class sfFeed
22 {
23   protected
24     $items = array(),
25     $title,
26     $link,
27     $description,
28     $language = 'en',
29     $authorEmail,
30     $authorName,
31     $authorLink,
32     $subtitle,
33     $categories = array(),
34     $feedItemsRouteName = '',
35     $feedUrl;
36
37   private
38     $context = null;
39
40   /**
41    * Retrieve a new sfFeed implementation instance.
42    *
43    * @param string A sfFeed implementation name.
44    *
45    * @return sfFeed A sfFeed implementation instance.
46    *
47    * @throws sfFactoryException If a new syndication feed implementation instance cannot be created.
48    */
49   public static function newInstance ($class)
50   {
51     try
52     {
53       $class = 'sf'.ucfirst($class).'Feed';
54
55       // the class exists
56       $object = new $class();
57
58       if (!($object instanceof sfFeed))
59       {
60           // the class name is of the wrong type
61           $error = 'Class "%s" is not of the type sfFeed';
62           $error = sprintf($error, $class);
63
64           throw new sfFactoryException($error);
65       }
66
67       $object->context = sfContext::getInstance();
68
69       return $object;
70     }
71     catch (sfException $e)
72     {
73       $e->printStackTrace();
74     }
75   }
76
77   public function addItem($item)
78   {
79     $this->items[] = $item;
80   }
81
82   public function setItems($items)
83   {
84     $this->items = $items;
85   }
86
87   public function getItems()
88   {
89     return $this->items;
90   }
91
92   public function addItemFromArray($item_array)
93   {
94     $item = new sfFeedItem();
95
96     $item->setItemTitle(isset($item_array['title']) ? $item_array['title'] : '');
97     $item->setItemLink(isset($item_array['link']) ? $item_array['link'] : '');
98     $item->setItemDescription(isset($item_array['description']) ? $item_array['description'] : '');
99     $item->setItemAuthorEmail(isset($item_array['authorEmail']) ? $item_array['authorEmail'] : '');
100     $item->setItemAuthorName(isset($item_array['authorName']) ? $item_array['authorName'] : '');
101     $item->setItemAuthorLink(isset($item_array['authorLink']) ? $item_array['authorLink'] : '');
102     $item->setItemPubdate(isset($item_array['pubdate']) ? $item_array['pubdate'] : '');
103     $item->setItemComments(isset($item_array['comments']) ? $item_array['comments'] : '');
104     $item->setItemUniqueId(isset($item_array['uniqueId']) ? $item_array['uniqueId'] : '');
105     $item->setItemEnclosure(isset($item_array['enclosure']) ? $item_array['enclosure'] : '');
106     $item->setItemCategories(isset($item_array['categories']) ? $item_array['categories'] : '');
107
108     $this->items[] = $item;
109   }
110
111   public function getFeed()
112   {
113     throw new sfException('You must use newInstance to get a real feed.');
114   }
115
116   public function setFeedItemsRouteName($routeName)
117   {
118     $this->feedItemsRouteName = $routeName;
119   }
120
121   public function getFeedItemsRouteName()
122   {
123     return $this->feedItemsRouteName;
124   }
125
126   public function setTitle ($title)
127   {
128     $this->title = $title;
129   }
130
131   public function getTitle ()
132   {
133     return $this->title;
134   }
135
136   public function setLink ($link)
137   {
138     $this->link = $link;
139   }
140
141   public function getLink ()
142   {
143     return $this->link;
144   }
145
146   public function setDescription ($description)
147   {
148     $this->description = $description;
149   }
150
151   public function getDescription ()
152   {
153     return $this->description;
154   }
155
156   public function setLanguage ($language)
157   {
158     $this->language = $language;
159   }
160
161   public function getLanguage ()
162   {
163     return $this->language;
164   }
165
166   public function setAuthorEmail ($authorEmail)
167   {
168     $this->authorEmail = $authorEmail;
169   }
170
171   public function getAuthorEmail ()
172   {
173     return $this->authorEmail;
174   }
175
176   public function setAuthorName ($authorName)
177   {
178     $this->authorName = $authorName;
179   }
180
181   public function getAuthorName ()
182   {
183     return $this->authorName;
184   }
185
186   public function setAuthorLink ($authorLink)
187   {
188     $this->authorLink = $authorLink;
189   }
190
191   public function getAuthorLink ()
192   {
193     return $this->authorLink;
194   }
195
196   public function setSubtitle ($subtitle)
197   {
198     $this->subtitle = $subtitle;
199   }
200
201   public function getSubtitle ()
202   {
203     return $this->subtitle;
204   }
205
206   public function setFeedUrl ($feedUrl)
207   {
208     $this->feedUrl = $feedUrl;
209   }
210
211   public function getFeedUrl ()
212   {
213     return $this->feedUrl;
214   }
215
216   public function setCategories ($categories)
217   {
218     $this->categories = $categories;
219   }
220
221   public function getCategories ()
222   {
223     return $this->categories;
224   }
225
226   // item feed methods
227   public function getItemFeedTitle ($item)
228   {
229     foreach (array('getFeedTitle', 'getTitle', 'getName', '__toString') as $methodName)
230     {
231       if (method_exists($item, $methodName))
232       {
233         return $item->$methodName();
234       }
235     }
236
237     return '';
238   }
239
240   public function getItemFeedDescription ($item)
241   {
242     foreach (array('getFeedDescription', 'getDescription', 'getBody') as $methodName)
243     {
244       if (method_exists($item, $methodName))
245       {
246         return $item->$methodName();
247       }
248     }
249
250     return '';
251   }
252
253   public function getItemFeedLink ($item)
254   {
255     if ($routeName = $this->getFeedItemsRouteName())
256     {
257       $routing = sfRouting::getInstance();
258       $route = $routing->getRouteByName($routeName);
259
260       $url = $route[0];
261
262       // we get all parameters
263       $params = array();
264       if (preg_match_all('/\:([^\/]+)/', $url, $matches))
265       {
266         foreach ($matches[1] as $paramName)
267         {
268           $value = null;
269           $name = ucfirst(sfInflector::camelize($paramName));
270
271           foreach (array('getFeed'.$name, 'get'.$name) as $methodName)
272           {
273             if (method_exists($item, $methodName))
274             {
275               $value = $item->$methodName();
276             }
277           }
278
279           if ($value === null)
280           {
281             $error = 'Cannot find a matching method name for "%s" parameter to generate URL for the "%s" route name';
282             $error = sprintf($error, $name, $routeName);
283             throw new sfException($error);
284           }
285
286           $params[] = $paramName.'='.$value;
287         }
288       }
289
290       return $this->context->getController()->genUrl($routeName.($params ? '?'.implode('&', $params) : ''), true);
291     }
292
293     foreach (array('getFeedLink', 'getLink', 'getUrl') as $methodName)
294     {
295       if (method_exists($item, $methodName))
296       {
297         return $this->context->getController()->genUrl($item->$methodName(), true);
298       }
299     }
300
301     if ($this->getLink())
302     {
303       return sfContext::getInstance()->getController()->genUrl($this->getLink(), true);
304     }
305     else
306     {
307       return $this->context->getController()->genUrl('/', true);
308     }
309   }
310
311   public function getItemFeedUniqueId ($item)
312   {
313     foreach (array('getFeedUniqueId', 'getId') as $methodName)
314     {
315       if (method_exists($item, $methodName))
316       {
317         return $item->$methodName();
318       }
319     }
320
321     return '';
322   }
323
324   public function getItemFeedAuthorEmail ($item)
325   {
326     foreach (array('getFeedAuthorEmail', 'getAuthorEmail') as $methodName)
327     {
328       if (method_exists($item, $methodName))
329       {
330         return $item->$methodName();
331       }
332     }
333
334     // author as an object link
335     if ($author = $this->getItemFeedAuthor($item))
336     {
337       foreach (array('getEmail', 'getMail') as $methodName)
338       {
339         if (method_exists($author, $methodName))
340         {
341           return $author->$methodName();
342         }
343       }
344     }
345
346     return '';
347   }
348
349   public function getItemFeedAuthorName ($item)
350   {
351     foreach (array('getFeedAuthorName', 'getAuthorName') as $methodName)
352     {
353       if (method_exists($item, $methodName))
354       {
355         return $item->$methodName();
356       }
357     }
358
359     // author as an object link
360     if ($author = $this->getItemFeedAuthor($item))
361     {
362       foreach (array('getName', '__toString') as $methodName)
363       {
364         if (method_exists($author, $methodName))
365         {
366           return $author->$methodName();
367         }
368       }
369     }
370
371     return '';
372   }
373
374   public function getItemFeedAuthorLink ($item)
375   {
376     foreach (array('getFeedAuthorLink', 'getAuthorLink') as $methodName)
377     {
378       if (method_exists($item, $methodName))
379       {
380         return $item->$methodName();
381       }
382     }
383
384     // author as an object link
385     if ($author = $this->getItemFeedAuthor($item))
386     {
387       foreach (array('getLink') as $methodName)
388       {
389         if (method_exists($author, $methodName))
390         {
391           return $author->$methodName();
392         }
393       }
394     }
395
396     return '';
397   }
398
399   public function getItemFeedPubdate ($item)
400   {
401     foreach (array('getFeedPubdate', 'getPubdate', 'getCreatedAt', 'getDate') as $methodName)
402     {
403       if (method_exists($item, $methodName))
404       {
405         return $item->$methodName('U');
406       }
407     }
408
409     return '';
410   }
411
412   public function getItemFeedComments ($item)
413   {
414     foreach (array('getFeedComments', 'getComments') as $methodName)
415     {
416       if (method_exists($item, $methodName))
417       {
418         return $item->$methodName();
419       }
420     }
421
422     return '';
423   }
424
425   public function getItemFeedCategories ($item)
426   {
427     foreach (array('getFeedCategories') as $methodName)
428     {
429       if (method_exists($item, $methodName))
430       {
431         return $item->$methodName();
432       }
433     }
434
435     // categories as an object
436     foreach (array('getCategories') as $methodName)
437     {
438       if (method_exists($item, $methodName) && is_object($item->$methodName()))
439       {
440         $categories = $item->$methodName();
441         if (is_array($categories))
442         {
443           $cats = array();
444           foreach ($categories as $category)
445           {
446             $cats[] = (string) $category;
447           }
448
449           return $cats;
450         }
451       }
452     }
453
454     return array();
455   }
456
457   public function getContext()
458   {
459     return $this->context;
460   }
461
462   private function getItemFeedAuthor ($item)
463   {
464     foreach (array('getAuthor', 'getUser', 'getPerson') as $methodName)
465     {
466       if (method_exists($item, $methodName) && is_object($item->$methodName()))
467       {
468         return $item->$methodName();
469       }
470     }
471
472     return null;
473   }
474 }
475
476 ?>
477
Note: See TracBrowser for help on using the browser.