]> git.immae.eu Git - github/wallabag/wallabag.git/blame_incremental - inc/3rdparty/libraries/feedwriter/FeedItem.php
Merge pull request #761 from wallabag/dev
[github/wallabag/wallabag.git] / inc / 3rdparty / libraries / feedwriter / FeedItem.php
... / ...
CommitLineData
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
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
25\r
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
104 $tag = ($this->version == ATOM)? 'summary' : 'description';\r
105 $this->setElement($tag, $description);\r
106 }\r
107\r
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
114 public function setTitle($title)\r
115 {\r
116 $this->setElement('title', $title);\r
117 }\r
118\r
119 /**\r
120 * Set the 'date' element of feed item\r
121 *\r
122 * @access public\r
123 * @param string The content of 'date' element\r
124 * @return void\r
125 */\r
126 public function setDate($date)\r
127 {\r
128 if(! is_numeric($date))\r
129 {\r
130 $date = strtotime($date);\r
131 }\r
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
139 {\r
140 $tag = 'pubDate';\r
141 $value = date(DATE_RSS, $date);\r
142 }\r
143 else\r
144 {\r
145 $tag = 'dc:date';\r
146 $value = date("Y-m-d", $date);\r
147 }\r
148\r
149 $this->setElement($tag, $value);\r
150 }\r
151\r
152 /**\r
153 * Set the 'link' element of feed item\r
154 *\r
155 * @access public\r
156 * @param string The content of 'link' element\r
157 * @return void\r
158 */\r
159 public function setLink($link)\r
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
170 }\r
171\r
172 }\r
173\r
174 /**\r
175 * Set the 'source' element of feed item\r
176 *\r
177 * @access public\r
178 * @param string The content of 'source' element\r
179 * @return void\r
180 */\r
181 public function setSource($link)\r
182 {\r
183 $attributes = array('url'=>$link);\r
184 $this->setElement('source', "wallabag",$attributes);\r
185 }\r
186\r
187 /**\r
188 * Set the 'encloser' element of feed item\r
189 * For RSS 2.0 only\r
190 *\r
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
202\r
203 } // end of class FeedItem\r
204?>