Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [skrypt] RSS Generator
Maslav
post
Post #1





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 6.02.2004

Ostrzeżenie: (0%)
-----


Hej. Wreszcie skończyła mi się sesja i mogę się na poważnie wziąść za szlifowanie swoich skilli. Jako "cel" obrałem sobie RSS dzięki czemu mogę sobie ćwiczyć zarówno OOP jak i DOM oraz XML. Na razie to taki trochę skromny szkielet bo bez obsługi błędów, kontroli typów i wielu dodatkowych znaczników RSS. Wobec tego nie proszę o ocenę tylko o wskazanie ewentualnych złych rozwiązań lub też zakamuflowanego myślenia proceduralnego.

  1. <?php
  2. class RssGenerator {
  3.  
  4. private $channelElement = array();
  5. private $itemElement = array();
  6. private $dom;
  7. private $channel;
  8.  
  9. function __construct() {
  10.  
  11. }
  12.  
  13. function setLanguage($value) {
  14.  
  15.  $this->ChannelElement[language] = $value ;
  16.  
  17. }
  18.  
  19. function setCopyright($value) {
  20.  
  21. $this->ChannelElement[copyright] = $value;
  22.  
  23. }
  24.  
  25. function setPubDate($value) {
  26. if(empty($value)) {
  27.  
  28. $this->ChannelElement[pubdate] = date(r);
  29.  
  30. }else {
  31.  
  32. $this->ChannelElement[pubdate] = $value;
  33.  
  34. }
  35.  
  36. }
  37.  
  38. /**
  39.  * generateRssChannel()
  40.  * Funkcja generująca <channel>.
  41.  *
  42.  * @param $title - Tytul strumienia.
  43.  * @param $link - Adres url do strony "własciciela" strumienia.
  44.  * @param $description - Opis strumienia.
  45.  * 
  46.  */
  47.  
  48. function generateRssChannel($title, $link, $description) {
  49.  
  50. $this->channelElement[title] = $title; 
  51. $this->channelElement[link] = $link; 
  52. $this->channelElement[description] = $description; 
  53.  
  54. $this->dom = new DOMDocument('1.0', 'utf-8');
  55.  
  56. $mainNode = $this->dom->appendChild($this->dom->createElement('rss'));
  57. $mainNode->setAttribute('version', '2.0');
  58.  
  59. $this->channel = $mainNode->appendChild($this->dom->createElement('channel'));
  60.  
  61. /*<channel>*/
  62.  
  63. /* Wymagane elementy */
  64.  
  65. $this->channel->appendChild($this->dom->createElement('title', $this->channelElement[title]));
  66. $this->channel->appendChild($this->dom->createElement('link', $this->channelElement[link]));
  67. $this->channel->appendChild($this->dom->createElement('description', $this->channelElement[description]));
  68.  
  69. /* Opcjonalne elementy */
  70.  
  71. if(!empty($this->channelElement[language])){ 
  72. $this->channel->appendChild($this->dom->createElement('language', $this->channelElement[language]));
  73. }
  74. if(!empty($this->channelElement[copyright])){ 
  75. $this->channel->appendChild($this->dom->createElement('copyright', $this->channelElement[copyright])); 
  76. }
  77. if(!empty($this->channelElement[pubdate])){ 
  78. $this->channel->appendChild($this->dom->createElement('pubdate', $this->channelElement[pubdate])); 
  79. }
  80.  
  81.  
  82.  
  83. }
  84.  
  85. /**
  86.  * generateRssItem()
  87.  * Funkcja generująca <item>.
  88.  *
  89.  * @param $title - Tytul strumienia.
  90.  * @param $link - Adres url itemu.
  91.  * @param $description - Opis strumienia.
  92.  * 
  93.  */
  94.  
  95. function generateRssItem($title, $link, $description) {
  96.  
  97. $this->itemElement[title] = $title; 
  98. $this->itemElement[link] = $link; 
  99. $this->itemElement[description] = $description; 
  100.  
  101. $item = $this->channel->appendChild($this->dom->createElement('item'));
  102. $item->appendChild($this->dom->createElement('title', $this->itemElement[title]));
  103. $item->appendChild($this->dom->createElement('link', $this->itemElement[link]));
  104. $item->appendChild($this->dom->createElement('description', $this->itemElement[description]));
  105.  
  106. }
  107.  
  108. function getRss() {
  109.  
  110. $this->dom->formatOutput = true;
  111.  
  112. return $this->dom->saveXML();
  113.  
  114.  
  115. }
  116.  
  117.  
  118. }
  119. ?>


I zastosowanko

  1. <?php
  2. $rss = new RssGenerator();
  3.  
  4. $rss->setPubDate('Sat, 07 Sep 2002 00:00:01 GMT');
  5. $rss->setLanguage('pl-pl');
  6. $rss->setCopyright('Ja ja@as.pl');
  7.  
  8. $rss->generateRssChannel('Tytuł', 'http://www.onet.pl', 'afasf');
  9.  
  10.  // Tutaj oczywiście symulacja działania while() ;]
  11.  
  12. $rss->generateRssItem('Title1', 'link2', 'afasfasff');
  13. $rss->generateRssItem('Title2', 'link2', 'afasfasff');
  14. $rss->generateRssItem('Title3', 'link2', 'afasfasff');
  15. $rss->generateRssItem('Title4', 'link2', 'afasfasff');
  16.  
  17. $feed = $rss->getRss();
  18.  
  19. echo $feed;
  20. ?>


pozdrawiam i dziękuję z góry za ewentualne wskazówki


--------------------
generalfailure
fabrykadrobiu*dot*com
Go to the top of the page
+Quote Post
mike
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

Ostrzeżenie: (0%)
-----


Błąd prawie w każdej linijce tongue.gif

Nie:
  1. <?php
  2.  
  3. $array[index]
  4.  
  5. ?>

tylko:
  1. <?php
  2.  
  3. $array['index']
  4.  
  5. ?>
Go to the top of the page
+Quote Post
Ludvik
post
Post #3





Grupa: Przyjaciele php.pl
Postów: 698
Pomógł: 3
Dołączył: 28.03.2004
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


A tak poza tym to jest OK smile.gif Ja bym pokusił się tylko o wyodrębnienie elementów do osobnej klasy, dzięki czemu mógłbyś mieć nad tym wszystkim większą kontrolę. Nie deklaruj atrybutów klasy jako prywatne, bo odbierasz sobie możliwość wygodnego dziedziczenia. Lepiej zastosować protected. Tak samo modyfikatory public - nie są wymagane, ale dobrze by było, gdyby się znalazły w kodzie. Najważniejsze jest to, czy wygodnie Ci się tego używa.


--------------------
Go to the top of the page
+Quote Post
Maslav
post
Post #4





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 6.02.2004

Ostrzeżenie: (0%)
-----


Dzięki za kommenty biggrin.gif


--------------------
generalfailure
fabrykadrobiu*dot*com
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 17:10