]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/3rdparty/libraries/feedwriter/FeedItem.php
Merge pull request #634 from wallabag/dev
[github/wallabag/wallabag.git] / inc / 3rdparty / libraries / feedwriter / FeedItem.php
CommitLineData
42c80841
NL
1<?php\r
2 /**\r
3 * Univarsel Feed Writer\r
4 * \r
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
15 \r
16 /**\r
17 * Constructor \r
18 * \r
19 * @param contant (RSS1/RSS2/ATOM) RSS2 is default. \r
20 */ \r
21 function __construct($version = RSS2)\r
22 { \r
23 $this->version = $version;\r
24 }\r
42c80841 25\r
f86784c2
NL
26 /**\r
27 * Set element (overwrites existing elements with $elementName)\r
28 * \r
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
41 } \r
42 \r
43 /**\r
44 * Add an element to elements array\r
45 * \r
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
64 \r
65 /**\r
66 * Set multiple feed elements from an array. \r
67 * Elements which have attributes cannot be added by this method\r
68 * \r
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
76 foreach ($elementArray as $elementName => $content) \r
77 {\r
78 $this->addElement($elementName, $content);\r
79 }\r
80 }\r
81 \r
82 /**\r
83 * Return the collection of elements in this feed item\r
84 * \r
85 * @access public\r
86 * @return array\r
87 */\r
88 public function getElements()\r
89 {\r
90 return $this->elements;\r
91 }\r
92 \r
93 // Wrapper functions ------------------------------------------------------\r
94 \r
95 /**\r
96 * Set the 'dscription' element of feed item\r
97 * \r
98 * @access public\r
99 * @param string The content of 'description' element\r
100 * @return void\r
101 */\r
102 public function setDescription($description) \r
103 {\r
7a873ef1 104 $this->setElement('description', $description);\r
f86784c2
NL
105 }\r
106 \r
107 /**\r
108 * @desc Set the 'title' element of feed item\r
109 * @access public\r
110 * @param string The content of 'title' element\r
111 * @return void\r
112 */\r
113 public function setTitle($title) \r
114 {\r
115 $this->setElement('title', $title); \r
116 }\r
117 \r
118 /**\r
119 * Set the 'date' element of feed item\r
120 * \r
121 * @access public\r
122 * @param string The content of 'date' element\r
123 * @return void\r
124 */\r
125 public function setDate($date) \r
126 {\r
127 if(! is_numeric($date))\r
128 {\r
129 $date = strtotime($date);\r
130 }\r
9e7c840b 131 \r
f86784c2
NL
132 if($this->version == RSS2) \r
133 {\r
134 $tag = 'pubDate';\r
135 $value = date(DATE_RSS, $date);\r
136 }\r
137 else \r
138 {\r
139 $tag = 'dc:date';\r
140 $value = date("Y-m-d", $date);\r
141 }\r
142 \r
143 $this->setElement($tag, $value); \r
144 }\r
145 \r
146 /**\r
147 * Set the 'link' element of feed item\r
148 * \r
149 * @access public\r
150 * @param string The content of 'link' element\r
151 * @return void\r
152 */\r
153 public function setLink($link) \r
154 {\r
155 if($this->version == RSS2 || $this->version == RSS1)\r
156 {\r
157 $this->setElement('link', $link);\r
158 $this->setElement('guid', $link);\r
159 }\r
160 else\r
161 {\r
162 $this->setElement('link','',array('href'=>$link));\r
163 $this->setElement('id', FeedWriter::uuid($link,'urn:uuid:'));\r
164 } \r
165 \r
166 }\r
167\r
168 /**\r
169 * Set the 'source' element of feed item\r
170 * \r
171 * @access public\r
172 * @param string The content of 'source' element\r
173 * @return void\r
174 */\r
175 public function setSource($link) \r
176 {\r
177 $this->setElement('source', $link);\r
178 }\r
179 \r
180 /**\r
181 * Set the 'encloser' element of feed item\r
182 * For RSS 2.0 only\r
183 * \r
184 * @access public\r
185 * @param string The url attribute of encloser tag\r
186 * @param string The length attribute of encloser tag\r
187 * @param string The type attribute of encloser tag\r
188 * @return void\r
189 */\r
190 public function setEncloser($url, $length, $type)\r
191 {\r
192 $attributes = array('url'=>$url, 'length'=>$length, 'type'=>$type);\r
193 $this->setElement('enclosure','',$attributes);\r
194 }\r
195 \r
42c80841 196 } // end of class FeedItem\r
f86784c2 197?>