]>
Commit | Line | Data |
---|---|---|
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 | |
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 | |
9e7c840b | 104 | $tag = 'description'; \r |
42c80841 NL |
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 | |
9e7c840b NL |
132 | \r |
133 | if($this->version == RSS2) \r | |
42c80841 NL |
134 | {\r |
135 | $tag = 'pubDate';\r | |
136 | $value = date(DATE_RSS, $date);\r | |
137 | }\r | |
138 | else \r | |
139 | {\r | |
140 | $tag = 'dc:date';\r | |
141 | $value = date("Y-m-d", $date);\r | |
142 | }\r | |
143 | \r | |
144 | $this->setElement($tag, $value); \r | |
145 | }\r | |
146 | \r | |
147 | /**\r | |
148 | * Set the 'link' element of feed item\r | |
149 | * \r | |
150 | * @access public\r | |
151 | * @param string The content of 'link' element\r | |
152 | * @return void\r | |
153 | */\r | |
154 | public function setLink($link) \r | |
155 | {\r | |
156 | if($this->version == RSS2 || $this->version == RSS1)\r | |
157 | {\r | |
158 | $this->setElement('link', $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 'encloser' element of feed item\r | |
170 | * For RSS 2.0 only\r | |
171 | * \r | |
172 | * @access public\r | |
173 | * @param string The url attribute of encloser tag\r | |
174 | * @param string The length attribute of encloser tag\r | |
175 | * @param string The type attribute of encloser tag\r | |
176 | * @return void\r | |
177 | */\r | |
178 | public function setEncloser($url, $length, $type)\r | |
179 | {\r | |
180 | $attributes = array('url'=>$url, 'length'=>$length, 'type'=>$type);\r | |
181 | $this->setElement('enclosure','',$attributes);\r | |
182 | }\r | |
183 | \r | |
184 | } // end of class FeedItem\r | |
185 | ?>\r |