/zf/library/Zend/Feed/Reader/Extension/Syndication/Feed.php

http://github.com/eryx/php-framework-benchmark · PHP · 168 lines · 90 code · 20 blank · 58 comment · 10 complexity · 8f76f384133439b237ddc4beb613d6bf MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed_Reader
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Feed.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Feed_Reader_Extension_FeedAbstract
  23. */
  24. require_once 'Zend/Feed/Reader/Extension/FeedAbstract.php';
  25. require_once 'Zend/Date.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Feed_Reader
  29. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Feed_Reader_Extension_Syndication_Feed
  33. extends Zend_Feed_Reader_Extension_FeedAbstract
  34. {
  35. /**
  36. * Get update period
  37. * @return string
  38. */
  39. public function getUpdatePeriod()
  40. {
  41. $name = 'updatePeriod';
  42. $period = $this->_getData($name);
  43. if ($period === null) {
  44. $this->_data[$name] = 'daily';
  45. return 'daily'; //Default specified by spec
  46. }
  47. switch ($period)
  48. {
  49. case 'hourly':
  50. case 'daily':
  51. case 'weekly':
  52. case 'yearly':
  53. return $period;
  54. default:
  55. throw new Zend_Feed_Exception("Feed specified invalid update period: '$period'."
  56. . " Must be one of hourly, daily, weekly or yearly"
  57. );
  58. }
  59. }
  60. /**
  61. * Get update frequency
  62. * @return int
  63. */
  64. public function getUpdateFrequency()
  65. {
  66. $name = 'updateFrequency';
  67. $freq = $this->_getData($name, 'number');
  68. if (!$freq || $freq < 1) {
  69. $this->_data[$name] = 1;
  70. return 1;
  71. }
  72. return $freq;
  73. }
  74. /**
  75. * Get update frequency as ticks
  76. * @return int
  77. */
  78. public function getUpdateFrequencyAsTicks()
  79. {
  80. $name = 'updateFrequency';
  81. $freq = $this->_getData($name, 'number');
  82. if (!$freq || $freq < 1) {
  83. $this->_data[$name] = 1;
  84. $freq = 1;
  85. }
  86. $period = $this->getUpdatePeriod();
  87. $ticks = 1;
  88. switch ($period)
  89. {
  90. //intentional fall through
  91. case 'yearly':
  92. $ticks *= 52; //TODO: fix generalisation, how?
  93. case 'weekly':
  94. $ticks *= 7;
  95. case 'daily':
  96. $ticks *= 24;
  97. case 'hourly':
  98. $ticks *= 3600;
  99. break;
  100. default: //Never arrive here, exception thrown in getPeriod()
  101. break;
  102. }
  103. return $ticks / $freq;
  104. }
  105. /**
  106. * Get update base
  107. *
  108. * @return Zend_Date|null
  109. */
  110. public function getUpdateBase()
  111. {
  112. $updateBase = $this->_getData('updateBase');
  113. $date = null;
  114. if ($updateBase) {
  115. $date = new Zend_Date;
  116. $date->set($updateBase, Zend_Date::W3C);
  117. }
  118. return $date;
  119. }
  120. /**
  121. * Get the entry data specified by name
  122. *
  123. * @param string $name
  124. * @param string $type
  125. * @return mixed|null
  126. */
  127. private function _getData($name, $type = 'string')
  128. {
  129. if (array_key_exists($name, $this->_data)) {
  130. return $this->_data[$name];
  131. }
  132. $data = $this->_xpath->evaluate($type . '(' . $this->getXpathPrefix() . '/syn10:' . $name . ')');
  133. if (!$data) {
  134. $data = null;
  135. }
  136. $this->_data[$name] = $data;
  137. return $data;
  138. }
  139. /**
  140. * Register Syndication namespaces
  141. *
  142. * @return void
  143. */
  144. protected function _registerNamespaces()
  145. {
  146. $this->_xpath->registerNamespace('syn10', 'http://purl.org/rss/1.0/modules/syndication/');
  147. }
  148. }