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