]>
Commit | Line | Data |
---|---|---|
42c80841 NL |
1 | <?php\r |
2 | /**\r | |
3 | * Univarsel Feed Writer\r | |
3ec62cf9 | 4 | *\r |
42c80841 NL |
5 | * FeedItem class - Used as feed element in FeedWriter class\r |
6 | *\r | |
7 | * @package UnivarselFeedWriter\r | |
8 | * @author Anis uddin Ahmad <anisniit@gmail.com>\r | |
9 | * @link http://www.ajaxray.com/projects/rss\r | |
10 | */\r | |
11 | class FeedItem\r | |
12 | {\r | |
f86784c2 NL |
13 | private $elements = array(); //Collection of feed elements\r |
14 | private $version;\r | |
3ec62cf9 | 15 | \r |
f86784c2 | 16 | /**\r |
3ec62cf9 MR |
17 | * Constructor\r |
18 | *\r | |
19 | * @param contant (RSS1/RSS2/ATOM) RSS2 is default.\r | |
20 | */\r | |
f86784c2 | 21 | function __construct($version = RSS2)\r |
3ec62cf9 | 22 | {\r |
f86784c2 NL |
23 | $this->version = $version;\r |
24 | }\r | |
42c80841 | 25 | \r |
f86784c2 NL |
26 | /**\r |
27 | * Set element (overwrites existing elements with $elementName)\r | |
3ec62cf9 | 28 | *\r |
f86784c2 NL |
29 | * @access public\r |
30 | * @param srting The tag name of an element\r | |
31 | * @param srting The content of tag\r | |
32 | * @param array Attributes(if any) in 'attrName' => 'attrValue' format\r | |
33 | * @return void\r | |
34 | */\r | |
35 | public function setElement($elementName, $content, $attributes = null)\r | |
36 | {\r | |
37 | if (isset($this->elements[$elementName])) {\r | |
38 | unset($this->elements[$elementName]);\r | |
39 | }\r | |
40 | $this->addElement($elementName, $content, $attributes);\r | |
3ec62cf9 MR |
41 | }\r |
42 | \r | |
f86784c2 NL |
43 | /**\r |
44 | * Add an element to elements array\r | |
3ec62cf9 | 45 | *\r |
f86784c2 NL |
46 | * @access public\r |
47 | * @param srting The tag name of an element\r | |
48 | * @param srting The content of tag\r | |
49 | * @param array Attributes(if any) in 'attrName' => 'attrValue' format\r | |
50 | * @return void\r | |
51 | */\r | |
52 | public function addElement($elementName, $content, $attributes = null)\r | |
53 | {\r | |
54 | $i = 0;\r | |
55 | if (isset($this->elements[$elementName])) {\r | |
56 | $i = count($this->elements[$elementName]);\r | |
57 | } else {\r | |
58 | $this->elements[$elementName] = array();\r | |
59 | }\r | |
60 | $this->elements[$elementName][$i]['name'] = $elementName;\r | |
61 | $this->elements[$elementName][$i]['content'] = $content;\r | |
62 | $this->elements[$elementName][$i]['attributes'] = $attributes;\r | |
63 | }\r | |
3ec62cf9 | 64 | \r |
f86784c2 | 65 | /**\r |
3ec62cf9 | 66 | * Set multiple feed elements from an array.\r |
f86784c2 | 67 | * Elements which have attributes cannot be added by this method\r |
3ec62cf9 | 68 | *\r |
f86784c2 NL |
69 | * @access public\r |
70 | * @param array array of elements in 'tagName' => 'tagContent' format.\r | |
71 | * @return void\r | |
72 | */\r | |
73 | public function addElementArray($elementArray)\r | |
74 | {\r | |
75 | if(! is_array($elementArray)) return;\r | |
3ec62cf9 | 76 | foreach ($elementArray as $elementName => $content)\r |
f86784c2 NL |
77 | {\r |
78 | $this->addElement($elementName, $content);\r | |
79 | }\r | |
80 | }\r | |
3ec62cf9 | 81 | \r |
f86784c2 NL |
82 | /**\r |
83 | * Return the collection of elements in this feed item\r | |
3ec62cf9 | 84 | *\r |
f86784c2 NL |
85 | * @access public\r |
86 | * @return array\r | |
87 | */\r | |
88 | public function getElements()\r | |
89 | {\r | |
90 | return $this->elements;\r | |
91 | }\r | |
3ec62cf9 | 92 | \r |
f86784c2 | 93 | // Wrapper functions ------------------------------------------------------\r |
3ec62cf9 | 94 | \r |
f86784c2 NL |
95 | /**\r |
96 | * Set the 'dscription' element of feed item\r | |
3ec62cf9 | 97 | *\r |
f86784c2 NL |
98 | * @access public\r |
99 | * @param string The content of 'description' element\r | |
100 | * @return void\r | |
101 | */\r | |
3ec62cf9 | 102 | public function setDescription($description)\r |
f86784c2 | 103 | {\r |
3ec62cf9 MR |
104 | $tag = ($this->version == ATOM)? 'summary' : 'description';\r |
105 | $this->setElement($tag, $description);\r | |
f86784c2 | 106 | }\r |
3ec62cf9 | 107 | \r |
f86784c2 NL |
108 | /**\r |
109 | * @desc Set the 'title' element of feed item\r | |
110 | * @access public\r | |
111 | * @param string The content of 'title' element\r | |
112 | * @return void\r | |
113 | */\r | |
3ec62cf9 | 114 | public function setTitle($title)\r |
f86784c2 | 115 | {\r |
3ec62cf9 | 116 | $this->setElement('title', $title);\r |
f86784c2 | 117 | }\r |
3ec62cf9 | 118 | \r |
f86784c2 NL |
119 | /**\r |
120 | * Set the 'date' element of feed item\r | |
3ec62cf9 | 121 | *\r |
f86784c2 NL |
122 | * @access public\r |
123 | * @param string The content of 'date' element\r | |
124 | * @return void\r | |
125 | */\r | |
3ec62cf9 | 126 | public function setDate($date)\r |
f86784c2 NL |
127 | {\r |
128 | if(! is_numeric($date))\r | |
129 | {\r | |
130 | $date = strtotime($date);\r | |
131 | }\r | |
3ec62cf9 MR |
132 | \r |
133 | if($this->version == ATOM)\r | |
134 | {\r | |
135 | $tag = 'updated';\r | |
136 | $value = date(DATE_ATOM, $date);\r | |
137 | }\r | |
138 | elseif($this->version == RSS2)\r | |
f86784c2 | 139 | {\r |
3ec62cf9 MR |
140 | $tag = 'pubDate';\r |
141 | $value = date(DATE_RSS, $date);\r | |
f86784c2 | 142 | }\r |
3ec62cf9 | 143 | else\r |
f86784c2 | 144 | {\r |
3ec62cf9 MR |
145 | $tag = 'dc:date';\r |
146 | $value = date("Y-m-d", $date);\r | |
f86784c2 | 147 | }\r |
3ec62cf9 MR |
148 | \r |
149 | $this->setElement($tag, $value);\r | |
f86784c2 | 150 | }\r |
3ec62cf9 | 151 | \r |
f86784c2 NL |
152 | /**\r |
153 | * Set the 'link' element of feed item\r | |
3ec62cf9 | 154 | *\r |
f86784c2 NL |
155 | * @access public\r |
156 | * @param string The content of 'link' element\r | |
157 | * @return void\r | |
158 | */\r | |
3ec62cf9 | 159 | public function setLink($link)\r |
f86784c2 NL |
160 | {\r |
161 | if($this->version == RSS2 || $this->version == RSS1)\r | |
162 | {\r | |
163 | $this->setElement('link', $link);\r | |
164 | $this->setElement('guid', $link);\r | |
165 | }\r | |
166 | else\r | |
167 | {\r | |
168 | $this->setElement('link','',array('href'=>$link));\r | |
169 | $this->setElement('id', FeedWriter::uuid($link,'urn:uuid:'));\r | |
3ec62cf9 MR |
170 | }\r |
171 | \r | |
f86784c2 NL |
172 | }\r |
173 | \r | |
174 | /**\r | |
175 | * Set the 'source' element of feed item\r | |
3ec62cf9 | 176 | *\r |
f86784c2 NL |
177 | * @access public\r |
178 | * @param string The content of 'source' element\r | |
179 | * @return void\r | |
180 | */\r | |
3ec62cf9 | 181 | public function setSource($link)\r |
f86784c2 | 182 | {\r |
ef179149 | 183 | $attributes = array('url'=>$link);\r |
184 | $this->setElement('source', "wallabag",$attributes);\r | |
f86784c2 | 185 | }\r |
3ec62cf9 | 186 | \r |
f86784c2 NL |
187 | /**\r |
188 | * Set the 'encloser' element of feed item\r | |
189 | * For RSS 2.0 only\r | |
3ec62cf9 | 190 | *\r |
f86784c2 NL |
191 | * @access public\r |
192 | * @param string The url attribute of encloser tag\r | |
193 | * @param string The length attribute of encloser tag\r | |
194 | * @param string The type attribute of encloser tag\r | |
195 | * @return void\r | |
196 | */\r | |
197 | public function setEncloser($url, $length, $type)\r | |
198 | {\r | |
199 | $attributes = array('url'=>$url, 'length'=>$length, 'type'=>$type);\r | |
200 | $this->setElement('enclosure','',$attributes);\r | |
201 | }\r | |
3ec62cf9 | 202 | \r |
42c80841 | 203 | } // end of class FeedItem\r |
f86784c2 | 204 | ?> |