00001 <?php
00002 
00003 
00004 
00005 class GSiteMapItemFormatter {
00006         protected $url = '';
00007         protected $data = array();
00008 
00009 
00010 
00011 
00012 
00013 
00014         public function __construct($url, $data) {
00015                 $this->url = $url;
00016                 $this->data = $data;
00017         }
00018 
00019 
00020 
00021 
00022 
00023 
00024         public function as_xml() {
00025                 $tags = $this->get_xml_tags();
00026                 return '<url>' . $this->xml_from_tags($tags) . '</url>';
00027         }
00028 
00029 
00030 
00031 
00032 
00033         protected function get_xml_tags() {
00034                 $tags = array();
00035                 $tags['loc'] = $this->url;
00036                 if (Arr::get_item($this->data, 'lastmod', 0) > 0) { $tags['lastmod'] = GyroDate::iso_date($this->data['lastmod']); }
00037                 if (!empty($this->data['priority'])) { $tags['priority'] = $this->data['priority']; }
00038                 if (!empty($this->data['changefreq'])) { $tags['changefreq'] = $this->data['changefreq']; }
00039 
00040                 return $tags;
00041         }
00042 
00043 
00044 
00045 
00046 
00047 
00048         public function as_html() {
00049                 $s_url = String::escape($this->url);
00050                 return "<a href=\"$s_url\">$s_url</a>";
00051         }
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059         protected function xml_from_tags($tags) {
00060                 $ret = '';
00061                 foreach($tags as $tag => $content) {
00062                         $c = is_array($content) ? $this->xml_from_tags($content) : String::escape($content, String::XML);
00063                         $ret .= "<$tag>" . $c . "</$tag>\n";
00064                 }
00065                 return $ret;
00066         }
00067 }