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.
<?php
class RssGenerator {
private $channelElement = array(); private $itemElement = array(); private $dom;
private $channel;
function __construct() {
}
function setLanguage($value) {
$this->ChannelElement[language] = $value ;
}
function setCopyright($value) {
$this->ChannelElement[copyright] = $value;
}
function setPubDate($value) {
$this->ChannelElement[pubdate
] = date(r
);
}else {
$this->ChannelElement[pubdate] = $value;
}
}
/**
* generateRssChannel()
* Funkcja generująca <channel>.
*
* @param $title - Tytul strumienia.
* @param $link - Adres url do strony "własciciela" strumienia.
* @param $description - Opis strumienia.
*
*/
function generateRssChannel($title, $link, $description) {
$this->channelElement[title] = $title;
$this->channelElement[link] = $link; $this->channelElement[description] = $description;
$this->dom = new DOMDocument('1.0', 'utf-8');
$mainNode = $this->dom->appendChild($this->dom->createElement('rss'));
$mainNode->setAttribute('version', '2.0');
$this->channel = $mainNode->appendChild($this->dom->createElement('channel'));
/*<channel>*/
/* Wymagane elementy */
$this->channel->appendChild($this->dom->createElement('title', $this->channelElement[title]));
$this->channel->appendChild($this->dom->createElement('link', $this->channelElement[link])); $this->channel->appendChild($this->dom->createElement('description', $this->channelElement[description]));
/* Opcjonalne elementy */
if(!empty($this->channelElement[language
])){ $this->channel->appendChild($this->dom->createElement('language', $this->channelElement[language]));
}
if(!empty($this->channelElement[copyright
])){ $this->channel->appendChild($this->dom->createElement('copyright', $this->channelElement[copyright]));
}
if(!empty($this->channelElement[pubdate
])){ $this->channel->appendChild($this->dom->createElement('pubdate', $this->channelElement[pubdate]));
}
}
/**
* generateRssItem()
* Funkcja generująca <item>.
*
* @param $title - Tytul strumienia.
* @param $link - Adres url itemu.
* @param $description - Opis strumienia.
*
*/
function generateRssItem($title, $link, $description) {
$this->itemElement[title] = $title;
$this->itemElement[link] = $link; $this->itemElement[description] = $description;
$item = $this->channel->appendChild($this->dom->createElement('item'));
$item->appendChild($this->dom->createElement('title', $this->itemElement[title]));
$item->appendChild($this->dom->createElement('link', $this->itemElement[link])); $item->appendChild($this->dom->createElement('description', $this->itemElement[description]));
}
function getRss() {
$this->dom->formatOutput = true;
return $this->dom->saveXML();
}
}
?>
I zastosowanko
<?php
$rss = new RssGenerator();
$rss->setPubDate('Sat, 07 Sep 2002 00:00:01 GMT');
$rss->setLanguage('pl-pl');
$rss->setCopyright('Ja ja@as.pl');
$rss->generateRssChannel('Tytuł', 'http://www.onet.pl', 'afasf');
// Tutaj oczywiście symulacja działania while() ;]
$rss->generateRssItem('Title1', 'link2', 'afasfasff');
$rss->generateRssItem('Title2', 'link2', 'afasfasff');
$rss->generateRssItem('Title3', 'link2', 'afasfasff');
$rss->generateRssItem('Title4', 'link2', 'afasfasff');
$feed = $rss->getRss();
?>
pozdrawiam i dziękuję z góry za ewentualne wskazówki