## Apache Zeta Components Manual :: File Source for georss_module.php

## Source for file georss_module.php

Documentation is available at georss_module.php

1. <?php
2. /**
3. * File containing the ezcFeedGeoRssModule class.
4. *
5. * @package Feed
6. * @version //autogentag//
7. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
8. * @license http://ez.no/licenses/new_bsd New BSD License
9. * @filesource
10. */
12. /**
13. * Support for the GeoRss module: data container, generator, parser.
14. *
15. * Specifications: {@link http://www.w3.org/2003/01/geo/}.
16. *
17. * Create example:
18. *
19. * <code>
20. * <?php
21. * // $feed is an ezcFeed object
22. * $item = $feed->add( 'item' );
23. * $module = $item->addModule( 'GeoRss' );
24. * $module->lat = 26.58;
25. * $module->long = -97.83;
26. * ?>
27. * </code>
28. *
29. * Parse example:
30. *
31. * <code>
32. * <?php
33. * // $item is an ezcFeedEntryElement object
34. * $lat = isset( $item->GeoRss->lat ) ? $item->GeoRss->lat->__toString() : null;
35. * $long = isset( $item->GeoRss->long ) ? $item->GeoRss->long->__toString() : null;
36. * ?>
37. * </code>
38. *
39. * @property ezcFeedTextElement $lat
40. * {@link http://en.wikipedia.org/wiki/WGS84 WGS84} latitude
41. * on the globe as decimal degrees
42. * (eg. 25.03358300). Can also be negative.
43. * @property ezcFeedTextElement $long
44. * {@link http://en.wikipedia.org/wiki/WGS84 WGS84} longitude
45. * on the globe as decimal degrees
46. * (eg. 121.56430000). Can also be negative.
47. *
48. * @package Feed
49. * @version //autogentag//
50. */
51. class ezcFeedGeoRssModule extends ezcFeedModule
52. {
53. /**
54. * Constructs a new ezcFeedContentModule object.
55. *
56. * @param string $level The level of the data container ('feed' or 'item')
57. */
58. public function __construct ( $level = 'feed' )
59. {
60. parent:: __construct ( $level );
61. }
63. /**
64. * Sets the property $name to $value.
65. *
66. * @throws ezcBasePropertyNotFoundException
67. * if the property $name is not defined
68. *
69. * @param string $name The property name
70. * @param mixed $value The property value
71. * @ignore
72. */
73. public function __set( $name, $value )
74. {
75. if ( $this-> isElementAllowed ( $name ) )
76. {
77. $node = $this-> add ( $name );
78. $node->text = $value;
79. }
80. else
81. {
82. parent::__set( $name, $value );
83. }
84. }
86. /**
87. * Returns the value of property $name.
88. *
89. * @throws ezcBasePropertyNotFoundException
90. * if the property $name is not defined
91. *
92. * @param string $name The property name
93. * @return mixed
94. * @ignore
95. */
96. public function __get( $name )
97. {
98. if ( $this-> isElementAllowed ( $name ) )
99. {
100. return $this-> properties [$name];
101. }
102. else
103. {
104. return parent::__get( $name );
105. }
106. }
108. /**
109. * Returns if the property $name is set.
110. *
111. * @param string $name The property name
112. * @return bool
113. * @ignore
114. */
115. public function __isset( $name )
116. {
117. if ( $this-> isElementAllowed ( $name ) )
118. {
119. return isset( $this-> properties [$name] );
120. }
121. else
122. {
123. return parent::__isset( $name );
124. }
125. }
127. /**
128. * Returns true if the element $name is allowed in the current module at the
129. * current level (feed or item), and false otherwise.
130. *
131. * @param string $name The element name to check if allowed in the current module and level (feed or item)
132. * @return bool
133. */
134. public function isElementAllowed ( $name )
135. {
136. switch ( $this-> level )
137. {
138. case 'feed':
139. if ( in_array ( $name, array( 'lat', 'long', 'point', 'where', 'box' ) ) )
140. {
141. return true;
142. }
143. break;
145. case 'item':
146. if ( in_array ( $name, array( 'lat', 'long', 'point', 'where', 'box' ) ) )
147. {
148. return true;
149. }
150. break;
151. }
152. return false;
153. }
155. /**
156. * Adds a new ezcFeedElement element with name $name to this module and
157. * returns it.
158. *
159. * @throws ezcFeedUnsupportedElementException
160. * if trying to add an element which is not supported.
161. *
162. * @param string $name The element name
163. * @return ezcFeedElement
164. */
165. public function add ( $name )
166. {
167. if ( $this-> isElementAllowed ( $name ) )
168. {
169. switch ( $name )
170. {
171. case 'lat':
172. case 'long':
173. case 'point':
174. case 'where':
175. case 'box':
176. $node = new ezcFeedTextElement ();
177. break;
178. }
180. $this-> properties [$name] = $node;
181. return $node;
182. }
183. else
184. {
185. throw new ezcFeedUnsupportedElementException ( $name );
186. }
187. }
189. /**
190. * Adds the module elements to the $xml XML document, in the container $root.
191. *
192. * @param DOMDocument $xml The XML document in which to add the module elements
193. * @param DOMNode $root The parent node which will contain the module elements
194. */
195. public function generate ( DOMDocument $xml, DOMNode $root )
196. {
197. if ( isset( $this->lat ) && isset( $this->long ) )
198. {
199. $elementTag = $xml->createElement( $this-> getNamespacePrefix () . ':' . 'point' );
200. $root->appendChild( $elementTag );
202. $elementTag->nodeValue = "{$this->lat} {$this->long}";
203. }
204. }
206. /**
207. * Parses the XML element $node and creates a feed element in the current
208. * module with name $name.
209. *
210. * @param string $name The name of the element belonging to the module
211. * @param DOMElement $node The XML child from which to take the values for $name
212. */
213. public function parse( $name, DOMElement $node )
214. {
215. if ( $this-> isElementAllowed ( $name ) )
216. {
217. switch ( $name )
218. {
219. case 'where':
220. $children = $node->childNodes;
221. $value = trim( $children->item(1)->textContent );
222. list( $lat, $long ) = explode( ' ', $value );
224. $element = $this-> add ( 'lat' );
225. $element->text = $lat;
226. $element = $this-> add ( 'long' );
227. $element->text = $long;
228. break;
229. case 'point':
230. $value = $node->textContent;
231. list( $lat, $long ) = explode( ' ', $value );
232. $element = $this-> add ( 'lat' );
233. $element->text = $lat;
234. $element = $this-> add ( 'long' );
235. $element->text = $long;
236. break;
237. case 'box':
238. $value = $node->textContent;
239. list( $lat1, $long1, $lat2, $long2 ) = explode( ' ', $value );
240. $element = $this-> add ( 'lat' );
241. $element->text = ( $lat1 + $lat2 ) / 2;
242. $element = $this-> add ( 'long' );
243. $element->text = ( $long1 + $long2 ) /2;
244. break;
245. }
246. }
247. }
249. /**
250. * Returns the module name (GeoRss').
251. *
252. * @return string
253. */
254. public static function getModuleName()
255. {
256. return 'GeoRss';
257. }
259. /**
260. * Returns the namespace for this module ('http://www.w3.org/2003/01/geo/wgs84_pos#').
261. *
262. * @return string
263. */
264. public static function getNamespace()
265. {
266. return 'http://www.georss.org/georss';
267. }
269. /**
270. * Returns the namespace prefix for this module ('georss').
271. *
272. * @return string
273. */
274. public static function getNamespacePrefix()
275. {
276. return 'georss';
277. }
278. }

Documentation generated by phpDocumentor 1.4.3