]>
Commit | Line | Data |
---|---|---|
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 | * Add an element to elements array | |
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 addElement($elementName, $content, $attributes = null) | |
36 | { | |
37 | $this->elements[$elementName]['name'] = $elementName; | |
38 | $this->elements[$elementName]['content'] = $content; | |
39 | $this->elements[$elementName]['attributes'] = $attributes; | |
40 | } | |
41 | ||
42 | /** | |
43 | * Set multiple feed elements from an array. | |
44 | * Elements which have attributes cannot be added by this method | |
45 | * | |
46 | * @access public | |
47 | * @param array array of elements in 'tagName' => 'tagContent' format. | |
48 | * @return void | |
49 | */ | |
50 | public function addElementArray($elementArray) | |
51 | { | |
52 | if(! is_array($elementArray)) return; | |
53 | foreach ($elementArray as $elementName => $content) | |
54 | { | |
55 | $this->addElement($elementName, $content); | |
56 | } | |
57 | } | |
58 | ||
59 | /** | |
60 | * Return the collection of elements in this feed item | |
61 | * | |
62 | * @access public | |
63 | * @return array | |
64 | */ | |
65 | public function getElements() | |
66 | { | |
67 | return $this->elements; | |
68 | } | |
69 | ||
70 | // Wrapper functions ------------------------------------------------------ | |
71 | ||
72 | /** | |
73 | * Set the 'dscription' element of feed item | |
74 | * | |
75 | * @access public | |
76 | * @param string The content of 'description' element | |
77 | * @return void | |
78 | */ | |
79 | public function setDescription($description) | |
80 | { | |
81 | $tag = ($this->version == ATOM)? 'summary' : 'description'; | |
82 | $this->addElement($tag, $description); | |
83 | } | |
84 | ||
85 | /** | |
86 | * @desc Set the 'title' element of feed item | |
87 | * @access public | |
88 | * @param string The content of 'title' element | |
89 | * @return void | |
90 | */ | |
91 | public function setTitle($title) | |
92 | { | |
93 | $this->addElement('title', $title); | |
94 | } | |
95 | ||
96 | /** | |
97 | * Set the 'date' element of feed item | |
98 | * | |
99 | * @access public | |
100 | * @param string The content of 'date' element | |
101 | * @return void | |
102 | */ | |
103 | public function setDate($date) | |
104 | { | |
105 | if(! is_numeric($date)) | |
106 | { | |
107 | $date = strtotime($date); | |
108 | } | |
109 | ||
110 | if($this->version == ATOM) | |
111 | { | |
112 | $tag = 'updated'; | |
113 | $value = date(DATE_ATOM, $date); | |
114 | } | |
115 | elseif($this->version == RSS2) | |
116 | { | |
117 | $tag = 'pubDate'; | |
118 | $value = date(DATE_RSS, $date); | |
119 | } | |
120 | else | |
121 | { | |
122 | $tag = 'dc:date'; | |
123 | $value = date("Y-m-d", $date); | |
124 | } | |
125 | ||
126 | $this->addElement($tag, $value); | |
127 | } | |
128 | ||
129 | /** | |
130 | * Set the 'link' element of feed item | |
131 | * | |
132 | * @access public | |
133 | * @param string The content of 'link' element | |
134 | * @return void | |
135 | */ | |
136 | public function setLink($link) | |
137 | { | |
138 | if($this->version == RSS2 || $this->version == RSS1) | |
139 | { | |
140 | $this->addElement('link', $link); | |
141 | } | |
142 | else | |
143 | { | |
144 | $this->addElement('link','',array('href'=>$link)); | |
145 | $this->addElement('id', FeedWriter::uuid($link,'urn:uuid:')); | |
146 | } | |
147 | ||
148 | } | |
149 | ||
150 | /** | |
151 | * Set the 'encloser' element of feed item | |
152 | * For RSS 2.0 only | |
153 | * | |
154 | * @access public | |
155 | * @param string The url attribute of encloser tag | |
156 | * @param string The length attribute of encloser tag | |
157 | * @param string The type attribute of encloser tag | |
158 | * @return void | |
159 | */ | |
160 | public function setEncloser($url, $length, $type) | |
161 | { | |
162 | $attributes = array('url'=>$url, 'length'=>$length, 'type'=>$type); | |
163 | $this->addElement('enclosure','',$attributes); | |
164 | } | |
165 | ||
166 | } // end of class FeedItem | |
167 | ?> |