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

## Source for file dublincore_module.php

Documentation is available at dublincore_module.php

1. <?php
2. /**
3. * File containing the ezcFeedDublinCoreModule class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @package Feed
23. * @version //autogentag//
24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
25. * @filesource
26. */
28. /**
29. * Support for the DublinCore module: data container, generator, parser.
30. *
31. * Specifications: {@link http://dublincore.org/documents/dces/}.
32. *
33. * Each DublinCore property can appear multiple times both on the feed-level
34. * or the item-level.
35. *
36. * Each DublinCore property can have the language attribute, which appears in
37. * the generated XML file as a 'xml:lang' attribute.
38. *
39. * Create example:
40. *
41. * <code>
42. * <?php
43. * // $feed is an ezcFeed object
44. * $item = $feed->add( 'item' );
45. * $module = $item->addModule( 'DublinCore' );
46. * $creator = $module->add( 'creator' );
47. * $creator->name = 'Creator name';
48. * $creator->language = 'en'; // optional
49. * // more elements of the same type can be added
50. * ?>
51. * </code>
52. *
53. * Parse example:
54. *
55. * <code>
56. * <?php
57. * // $item is an ezcFeedEntryElement object
58. * foreach ( $item->DublinCore->creator as $creator )
59. * {
60. * echo $creator->name;
61. * echo $creator->language;
62. * }
63. * ?>
64. * </code>
65. *
66. * @property array(ezcFeedPersonElement) $contributor
67. * An entity responsible for making contributions to
68. * the resource.
69. * Usually the name of a person, organization or service.
70. * @property array(ezcFeedTextElement) $coverage
71. * The spatial or temporal topic of the resource, the
72. * spatial applicability of the resource, or the
73. * jurisdiction under which the resource is relevant.
74. * A recommended practice is to use a controlled
75. * vocabulary such as
76. * {@link http://www.getty.edu/research/tools/vocabulary/tgn/index.html TGN}.
77. * @property array(ezcFeedPersonElement) $creator
78. * An entity responsible for making the resource.
79. * Usually the name of a person or organization.
80. * @property array(ezcFeedDateElement) $date
81. * A point or period of time associated with an event
82. * in the lifecycle of the resource. It is a Unix
83. * timestamp, which will be converted to an
84. * {@link http://www.w3.org/TR/NOTE-datetime ISO 8601}
85. * date when generating the feed.
86. * @property array(ezcFeedTextElement) $description
87. * A description of the resource.
88. * @property array(ezcFeedTextElement) $format
89. * The file format, physical medium, or dimensions of
90. * the resource.
91. * Recommended best practices is to use a controlled
92. * vocabulary such as the list of
93. * {@link http://www.iana.org/assignments/media-types/ Internet Media Types}
94. * (MIME).
95. * @property array(ezcFeedIdElement) $identifier
96. * An unambiguous reference to the resource within a
97. * given context.
98. * @property array(ezcFeedTextElement) $language
99. * A language of the resource.
100. * Recommended best practice is to use a controlled
101. * vocabulary such as
102. * {@link http://www.faqs.org/rfcs/rfc4646.html RFC 4646}.
103. * @property array(ezcFeedPersonElement) $publisher
104. * An entity responsible for making the resource available.
105. * Usually the name of a person, organization or service.
106. * @property array(ezcFeedTextElement) $relation
107. * A related resource.
108. * @property array(ezcFeedTextElement) $rights
109. * Information about rights held in and over the resource.
110. * @property array(ezcFeedSourceElement) $source
111. * A related resource from which the described resource
112. * is derived.
113. * @property array(ezcFeedTextElement) $subject
114. * The topic of the resource.
115. * @property array(ezcFeedTextElement) $title
116. * The name given to the resource.
117. * @property array(ezcFeedTextElement) $type
118. * The nature or genre of the resource.
119. * Recommended best practice is to use a controlled
120. * vocabulary such as the
121. * {@link http://dublincore.org/documents/dcmi-type-vocabulary/ DCMI Type Vocabulary}
122. *
123. * @package Feed
124. * @version //autogentag//
125. */
126. class ezcFeedDublinCoreModule extends ezcFeedModule
127. {
128. /**
129. * Constructs a new ezcFeedDublinCoreModule object.
130. *
131. * @param string $level The level of the data container ('feed' or 'item')
132. */
133. public function __construct ( $level = 'feed' )
134. {
135. parent:: __construct ( $level );
136. }
138. /**
139. * Sets the property $name to $value.
140. *
141. * @throws ezcBasePropertyNotFoundException
142. * if the property $name is not defined
143. *
144. * @param string $name The property name
145. * @param mixed $value The property value
146. * @ignore
147. */
148. public function __set( $name, $value )
149. {
150. if ( $this-> isElementAllowed ( $name ) )
151. {
152. switch ( $name )
153. {
154. case 'date':
155. $node = $this-> add ( $name );
156. $node->date = $value;
157. break;
159. case 'contributor':
160. case 'creator':
161. case 'publisher':
162. $node = $this-> add ( $name );
163. $node->name = $value;
164. break;
166. case 'identifier':
167. $node = $this-> add ( $name );
168. $node->id = $value;
169. break;
171. case 'source':
172. $node = $this-> add ( $name );
173. $node->source = $value;
174. break;
176. default:
177. $node = new ezcFeedTextElement ();
178. break;
179. }
180. }
181. else
182. {
183. parent::__set( $name, $value );
184. }
185. }
187. /**
188. * Returns the value of property $name.
189. *
190. * @throws ezcBasePropertyNotFoundException
191. * if the property $name is not defined
192. *
193. * @param string $name The property name
194. * @return mixed
195. * @ignore
196. */
197. public function __get( $name )
198. {
199. if ( $this-> isElementAllowed ( $name ) )
200. {
201. return $this-> properties [$name];
202. }
203. else
204. {
205. return parent::__get( $name );
206. }
207. }
209. /**
210. * Returns if the property $name is set.
211. *
212. * @param string $name The property name
213. * @return bool
214. * @ignore
215. */
216. public function __isset( $name )
217. {
218. if ( $this-> isElementAllowed ( $name ) )
219. {
220. return isset( $this-> properties [$name] );
221. }
222. else
223. {
224. return parent::__isset( $name );
225. }
226. }
228. /**
229. * Returns true if the element $name is allowed in the current module at the
230. * current level (feed or item), and false otherwise.
231. *
232. * @param string $name The element name to check if allowed in the current module and level (feed or item)
233. * @return bool
234. */
235. public function isElementAllowed ( $name )
236. {
237. switch ( $this-> level )
238. {
239. case 'feed':
240. if ( in_array ( $name, array( 'contributor', 'coverage', 'creator',
241. 'date', 'description', 'format',
242. 'identifier', 'language', 'publisher',
243. 'relation', 'rights', 'source',
244. 'subject', 'title', 'type' ) ) )
245. {
246. return true;
247. }
248. break;
250. case 'item':
251. if ( in_array ( $name, array( 'contributor', 'coverage', 'creator',
252. 'date', 'description', 'format',
253. 'identifier', 'language', 'publisher',
254. 'relation', 'rights', 'source',
255. 'subject', 'title', 'type' ) ) )
256. {
257. return true;
258. }
259. break;
260. }
261. return false;
262. }
264. /**
265. * Adds a new ezcFeedElement element with name $name to this module and
266. * returns it.
267. *
268. * @throws ezcFeedUnsupportedElementException
269. * if trying to add an element which is not supported.
270. *
271. * @param string $name The element name
272. * @return ezcFeedElement
273. */
274. public function add ( $name )
275. {
276. if ( $this-> isElementAllowed ( $name ) )
277. {
278. switch ( $name )
279. {
280. case 'date':
281. $node = new ezcFeedDateElement ();
282. break;
284. case 'contributor':
285. case 'creator':
286. case 'publisher':
287. $node = new ezcFeedPersonElement ();
288. break;
290. case 'identifier':
291. $node = new ezcFeedIdElement ();
292. break;
294. case 'source':
295. $node = new ezcFeedSourceElement ();
296. break;
298. default:
299. $node = new ezcFeedTextElement ();
300. break;
301. }
303. $this-> properties [$name][] = $node;
304. return $node;
305. }
306. else
307. {
308. throw new ezcFeedUnsupportedElementException ( $name );
309. }
310. }
312. /**
313. * Adds the module elements to the $xml XML document, in the container $root.
314. *
315. * @param DOMDocument $xml The XML document in which to add the module elements
316. * @param DOMNode $root The parent node which will contain the module elements
317. */
318. public function generate ( DOMDocument $xml, DOMNode $root )
319. {
320. $elements = array( 'contributor', 'coverage', 'creator',
321. 'date', 'description', 'format',
322. 'identifier', 'language', 'publisher',
323. 'relation', 'rights', 'source',
324. 'subject', 'title', 'type');
326. foreach ( $elements as $element )
327. {
328. if ( isset( $this->$element ) )
329. {
330. foreach ( $this->$element as $values )
331. {
332. $elementTag = $xml->createElement( $this-> getNamespacePrefix () . ':' . $element );
333. $root->appendChild( $elementTag );
335. switch ( $element )
336. {
337. case 'date':
338. $elementTag->nodeValue = $values->date->format( 'c' );
339. break;
341. default:
342. $elementTag->nodeValue = $values->__toString();
343. break;
344. }
346. if ( isset( $values->language ) )
347. {
348. $this->addAttribute( $xml, $elementTag, 'xml:lang', $values->language );
349. }
350. }
351. }
352. }
353. }
355. /**
356. * Parses the XML element $node and creates a feed element in the current
357. * module with name $name.
358. *
359. * @param string $name The name of the element belonging to the module
360. * @param DOMElement $node The XML child from which to take the values for $name
361. */
362. public function parse ( $name, DOMElement $node )
363. {
364. if ( $this-> isElementAllowed ( $name ) )
365. {
366. $element = $this-> add ( $name );
367. $value = $node->textContent;
369. switch ( $name )
370. {
371. case 'date':
372. $element->date = $value;
373. break;
375. case 'contributor':
376. case 'creator':
377. case 'publisher':
378. $element->name = $value;
379. break;
381. case 'identifier':
382. $element->id = $value;
383. break;
385. case 'source':
386. $element->source = $value;
387. break;
389. default:
390. $element->text = $value;
391. }
393. if ( $node->hasAttributes() )
394. {
395. foreach ( $node->attributes as $attribute )
396. {
397. switch ( $attribute->name )
398. {
399. case 'lang':
400. $element->language = $attribute->value;
401. break;
402. }
403. }
404. }
405. }
406. }
408. /**
409. * Returns the module name ('DublinCore').
410. *
411. * @return string
412. */
413. public static function getModuleName ()
414. {
415. return 'DublinCore';
416. }
418. /**
419. * Returns the namespace for this module ('http://purl.org/dc/elements/1.1/').
420. *
421. * @return string
422. */
423. public static function getNamespace ()
424. {
425. return 'http://purl.org/dc/elements/1.1/';
426. }
428. /**
429. * Returns the namespace prefix for this module ('dc').
430. *
431. * @return string
432. */
433. public static function getNamespacePrefix ()
434. {
435. return 'dc';
436. }
437. }
438. ?>

Documentation generated by phpDocumentor 1.4.3