PageRenderTime 65ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Feed/Writer/Renderer/Feed/Atom/AbstractAtom.php

https://github.com/pborreli/zf2
PHP | 406 lines | 251 code | 23 blank | 132 comment | 35 complexity | 6f1291c9edc9ae051d27103db1e97216 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Feed
  9. */
  10. namespace Zend\Feed\Writer\Renderer\Feed\Atom;
  11. use Datetime;
  12. use DOMDocument;
  13. use DOMElement;
  14. use Zend\Feed;
  15. use Zend\Version\Version;
  16. /**
  17. * @category Zend
  18. * @package Zend_Feed_Writer
  19. */
  20. class AbstractAtom extends Feed\Writer\Renderer\AbstractRenderer
  21. {
  22. /**
  23. * Constructor
  24. *
  25. * @param \Zend\Feed\Writer\Feed $container
  26. * @return void
  27. */
  28. public function __construct ($container)
  29. {
  30. parent::__construct($container);
  31. }
  32. /**
  33. * Set feed language
  34. *
  35. * @param DOMDocument $dom
  36. * @param DOMElement $root
  37. * @return void
  38. */
  39. protected function _setLanguage(DOMDocument $dom, DOMElement $root)
  40. {
  41. if ($this->getDataContainer()->getLanguage()) {
  42. $root->setAttribute('xml:lang', $this->getDataContainer()
  43. ->getLanguage());
  44. }
  45. }
  46. /**
  47. * Set feed title
  48. *
  49. * @param DOMDocument $dom
  50. * @param DOMElement $root
  51. * @return void
  52. * @throws Feed\Exception\InvalidArgumentException
  53. */
  54. protected function _setTitle(DOMDocument $dom, DOMElement $root)
  55. {
  56. if(!$this->getDataContainer()->getTitle()) {
  57. $message = 'Atom 1.0 feed elements MUST contain exactly one'
  58. . ' atom:title element but a title has not been set';
  59. $exception = new Feed\Exception\InvalidArgumentException($message);
  60. if (!$this->ignoreExceptions) {
  61. throw $exception;
  62. } else {
  63. $this->exceptions[] = $exception;
  64. return;
  65. }
  66. }
  67. $title = $dom->createElement('title');
  68. $root->appendChild($title);
  69. $title->setAttribute('type', 'text');
  70. $text = $dom->createTextNode($this->getDataContainer()->getTitle());
  71. $title->appendChild($text);
  72. }
  73. /**
  74. * Set feed description
  75. *
  76. * @param DOMDocument $dom
  77. * @param DOMElement $root
  78. * @return void
  79. */
  80. protected function _setDescription(DOMDocument $dom, DOMElement $root)
  81. {
  82. if(!$this->getDataContainer()->getDescription()) {
  83. return;
  84. }
  85. $subtitle = $dom->createElement('subtitle');
  86. $root->appendChild($subtitle);
  87. $subtitle->setAttribute('type', 'text');
  88. $text = $dom->createTextNode($this->getDataContainer()->getDescription());
  89. $subtitle->appendChild($text);
  90. }
  91. /**
  92. * Set date feed was last modified
  93. *
  94. * @param DOMDocument $dom
  95. * @param DOMElement $root
  96. * @return void
  97. * @throws Feed\Exception\InvalidArgumentException
  98. */
  99. protected function _setDateModified(DOMDocument $dom, DOMElement $root)
  100. {
  101. if(!$this->getDataContainer()->getDateModified()) {
  102. $message = 'Atom 1.0 feed elements MUST contain exactly one'
  103. . ' atom:updated element but a modification date has not been set';
  104. $exception = new Feed\Exception\InvalidArgumentException($message);
  105. if (!$this->ignoreExceptions) {
  106. throw $exception;
  107. } else {
  108. $this->exceptions[] = $exception;
  109. return;
  110. }
  111. }
  112. $updated = $dom->createElement('updated');
  113. $root->appendChild($updated);
  114. $text = $dom->createTextNode(
  115. $this->getDataContainer()->getDateModified()->format(DateTime::ISO8601)
  116. );
  117. $updated->appendChild($text);
  118. }
  119. /**
  120. * Set feed generator string
  121. *
  122. * @param DOMDocument $dom
  123. * @param DOMElement $root
  124. * @return void
  125. */
  126. protected function _setGenerator(DOMDocument $dom, DOMElement $root)
  127. {
  128. if(!$this->getDataContainer()->getGenerator()) {
  129. $this->getDataContainer()->setGenerator('Zend_Feed_Writer',
  130. Version::VERSION, 'http://framework.zend.com');
  131. }
  132. $gdata = $this->getDataContainer()->getGenerator();
  133. $generator = $dom->createElement('generator');
  134. $root->appendChild($generator);
  135. $text = $dom->createTextNode($gdata['name']);
  136. $generator->appendChild($text);
  137. if (array_key_exists('uri', $gdata)) {
  138. $generator->setAttribute('uri', $gdata['uri']);
  139. }
  140. if (array_key_exists('version', $gdata)) {
  141. $generator->setAttribute('version', $gdata['version']);
  142. }
  143. }
  144. /**
  145. * Set link to feed
  146. *
  147. * @param DOMDocument $dom
  148. * @param DOMElement $root
  149. * @return void
  150. */
  151. protected function _setLink(DOMDocument $dom, DOMElement $root)
  152. {
  153. if(!$this->getDataContainer()->getLink()) {
  154. return;
  155. }
  156. $link = $dom->createElement('link');
  157. $root->appendChild($link);
  158. $link->setAttribute('rel', 'alternate');
  159. $link->setAttribute('type', 'text/html');
  160. $link->setAttribute('href', $this->getDataContainer()->getLink());
  161. }
  162. /**
  163. * Set feed links
  164. *
  165. * @param DOMDocument $dom
  166. * @param DOMElement $root
  167. * @return void
  168. * @throws Feed\Exception\InvalidArgumentException
  169. */
  170. protected function _setFeedLinks(DOMDocument $dom, DOMElement $root)
  171. {
  172. $flinks = $this->getDataContainer()->getFeedLinks();
  173. if(!$flinks || !array_key_exists('atom', $flinks)) {
  174. $message = 'Atom 1.0 feed elements SHOULD contain one atom:link '
  175. . 'element with a rel attribute value of "self". This is the '
  176. . 'preferred URI for retrieving Atom Feed Documents representing '
  177. . 'this Atom feed but a feed link has not been set';
  178. $exception = new Feed\Exception\InvalidArgumentException($message);
  179. if (!$this->ignoreExceptions) {
  180. throw $exception;
  181. } else {
  182. $this->exceptions[] = $exception;
  183. return;
  184. }
  185. }
  186. foreach ($flinks as $type => $href) {
  187. $mime = 'application/' . strtolower($type) . '+xml';
  188. $flink = $dom->createElement('link');
  189. $root->appendChild($flink);
  190. $flink->setAttribute('rel', 'self');
  191. $flink->setAttribute('type', $mime);
  192. $flink->setAttribute('href', $href);
  193. }
  194. }
  195. /**
  196. * Set feed authors
  197. *
  198. * @param DOMDocument $dom
  199. * @param DOMElement $root
  200. * @return void
  201. */
  202. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  203. {
  204. $authors = $this->container->getAuthors();
  205. if (!$authors || empty($authors)) {
  206. /**
  207. * Technically we should defer an exception until we can check
  208. * that all entries contain an author. If any entry is missing
  209. * an author, then a missing feed author element is invalid
  210. */
  211. return;
  212. }
  213. foreach ($authors as $data) {
  214. $author = $this->dom->createElement('author');
  215. $name = $this->dom->createElement('name');
  216. $author->appendChild($name);
  217. $root->appendChild($author);
  218. $text = $dom->createTextNode($data['name']);
  219. $name->appendChild($text);
  220. if (array_key_exists('email', $data)) {
  221. $email = $this->dom->createElement('email');
  222. $author->appendChild($email);
  223. $text = $dom->createTextNode($data['email']);
  224. $email->appendChild($text);
  225. }
  226. if (array_key_exists('uri', $data)) {
  227. $uri = $this->dom->createElement('uri');
  228. $author->appendChild($uri);
  229. $text = $dom->createTextNode($data['uri']);
  230. $uri->appendChild($text);
  231. }
  232. }
  233. }
  234. /**
  235. * Set feed identifier
  236. *
  237. * @param DOMDocument $dom
  238. * @param DOMElement $root
  239. * @return void
  240. * @throws Feed\Exception\InvalidArgumentException
  241. */
  242. protected function _setId(DOMDocument $dom, DOMElement $root)
  243. {
  244. if(!$this->getDataContainer()->getId()
  245. && !$this->getDataContainer()->getLink()) {
  246. $message = 'Atom 1.0 feed elements MUST contain exactly one '
  247. . 'atom:id element, or as an alternative, we can use the same '
  248. . 'value as atom:link however neither a suitable link nor an '
  249. . 'id have been set';
  250. $exception = new Feed\Exception\InvalidArgumentException($message);
  251. if (!$this->ignoreExceptions) {
  252. throw $exception;
  253. } else {
  254. $this->exceptions[] = $exception;
  255. return;
  256. }
  257. }
  258. if (!$this->getDataContainer()->getId()) {
  259. $this->getDataContainer()->setId(
  260. $this->getDataContainer()->getLink());
  261. }
  262. $id = $dom->createElement('id');
  263. $root->appendChild($id);
  264. $text = $dom->createTextNode($this->getDataContainer()->getId());
  265. $id->appendChild($text);
  266. }
  267. /**
  268. * Set feed copyright
  269. *
  270. * @param DOMDocument $dom
  271. * @param DOMElement $root
  272. * @return void
  273. */
  274. protected function _setCopyright(DOMDocument $dom, DOMElement $root)
  275. {
  276. $copyright = $this->getDataContainer()->getCopyright();
  277. if (!$copyright) {
  278. return;
  279. }
  280. $copy = $dom->createElement('rights');
  281. $root->appendChild($copy);
  282. $text = $dom->createTextNode($copyright);
  283. $copy->appendChild($text);
  284. }
  285. /**
  286. * Set feed level logo (image)
  287. *
  288. * @param DOMDocument $dom
  289. * @param DOMElement $root
  290. * @return void
  291. */
  292. protected function _setImage(DOMDocument $dom, DOMElement $root)
  293. {
  294. $image = $this->getDataContainer()->getImage();
  295. if (!$image) {
  296. return;
  297. }
  298. $img = $dom->createElement('logo');
  299. $root->appendChild($img);
  300. $text = $dom->createTextNode($image['uri']);
  301. $img->appendChild($text);
  302. }
  303. /**
  304. * Set date feed was created
  305. *
  306. * @param DOMDocument $dom
  307. * @param DOMElement $root
  308. * @return void
  309. */
  310. protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
  311. {
  312. if(!$this->getDataContainer()->getDateCreated()) {
  313. return;
  314. }
  315. if(!$this->getDataContainer()->getDateModified()) {
  316. $this->getDataContainer()->setDateModified(
  317. $this->getDataContainer()->getDateCreated()
  318. );
  319. }
  320. }
  321. /**
  322. * Set base URL to feed links
  323. *
  324. * @param DOMDocument $dom
  325. * @param DOMElement $root
  326. * @return void
  327. */
  328. protected function _setBaseUrl(DOMDocument $dom, DOMElement $root)
  329. {
  330. $baseUrl = $this->getDataContainer()->getBaseUrl();
  331. if (!$baseUrl) {
  332. return;
  333. }
  334. $root->setAttribute('xml:base', $baseUrl);
  335. }
  336. /**
  337. * Set hubs to which this feed pushes
  338. *
  339. * @param DOMDocument $dom
  340. * @param DOMElement $root
  341. * @return void
  342. */
  343. protected function _setHubs(DOMDocument $dom, DOMElement $root)
  344. {
  345. $hubs = $this->getDataContainer()->getHubs();
  346. if (!$hubs) {
  347. return;
  348. }
  349. foreach ($hubs as $hubUrl) {
  350. $hub = $dom->createElement('link');
  351. $hub->setAttribute('rel', 'hub');
  352. $hub->setAttribute('href', $hubUrl);
  353. $root->appendChild($hub);
  354. }
  355. }
  356. /**
  357. * Set feed cateories
  358. *
  359. * @param DOMDocument $dom
  360. * @param DOMElement $root
  361. * @return void
  362. */
  363. protected function _setCategories(DOMDocument $dom, DOMElement $root)
  364. {
  365. $categories = $this->getDataContainer()->getCategories();
  366. if (!$categories) {
  367. return;
  368. }
  369. foreach ($categories as $cat) {
  370. $category = $dom->createElement('category');
  371. $category->setAttribute('term', $cat['term']);
  372. if (isset($cat['label'])) {
  373. $category->setAttribute('label', $cat['label']);
  374. } else {
  375. $category->setAttribute('label', $cat['term']);
  376. }
  377. if (isset($cat['scheme'])) {
  378. $category->setAttribute('scheme', $cat['scheme']);
  379. }
  380. $root->appendChild($category);
  381. }
  382. }
  383. }