PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

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