/library/Zend/Feed/Reader/Extension/Syndication/Feed.php
PHP | 168 lines | 90 code | 20 blank | 58 comment | 10 complexity | 966be4cb1f00438556a97cd618073c98 MD5 | raw file
Possible License(s): AGPL-1.0
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-2012 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 24594 2012-01-05 21:27:01Z matthew $
20 */
21
22/**
23 * @see Zend_Feed_Reader_Extension_FeedAbstract
24 */
25require_once 'Zend/Feed/Reader/Extension/FeedAbstract.php';
26
27require_once 'Zend/Date.php';
28
29/**
30 * @category Zend
31 * @package Zend_Feed_Reader
32 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
34 */
35class Zend_Feed_Reader_Extension_Syndication_Feed
36 extends Zend_Feed_Reader_Extension_FeedAbstract
37{
38 /**
39 * Get update period
40 * @return string
41 */
42 public function getUpdatePeriod()
43 {
44 $name = 'updatePeriod';
45 $period = $this->_getData($name);
46
47 if ($period === null) {
48 $this->_data[$name] = 'daily';
49 return 'daily'; //Default specified by spec
50 }
51
52 switch ($period)
53 {
54 case 'hourly':
55 case 'daily':
56 case 'weekly':
57 case 'yearly':
58 return $period;
59 default:
60 throw new Zend_Feed_Exception("Feed specified invalid update period: '$period'."
61 . " Must be one of hourly, daily, weekly or yearly"
62 );
63 }
64 }
65
66 /**
67 * Get update frequency
68 * @return int
69 */
70 public function getUpdateFrequency()
71 {
72 $name = 'updateFrequency';
73 $freq = $this->_getData($name, 'number');
74
75 if (!$freq || $freq < 1) {
76 $this->_data[$name] = 1;
77 return 1;
78 }
79
80 return $freq;
81 }
82
83 /**
84 * Get update frequency as ticks
85 * @return int
86 */
87 public function getUpdateFrequencyAsTicks()
88 {
89 $name = 'updateFrequency';
90 $freq = $this->_getData($name, 'number');
91
92 if (!$freq || $freq < 1) {
93 $this->_data[$name] = 1;
94 $freq = 1;
95 }
96
97 $period = $this->getUpdatePeriod();
98 $ticks = 1;
99
100 switch ($period)
101 {
102 //intentional fall through
103 case 'yearly':
104 $ticks *= 52; //TODO: fix generalisation, how?
105 case 'weekly':
106 $ticks *= 7;
107 case 'daily':
108 $ticks *= 24;
109 case 'hourly':
110 $ticks *= 3600;
111 break;
112 default: //Never arrive here, exception thrown in getPeriod()
113 break;
114 }
115
116 return $ticks / $freq;
117 }
118
119 /**
120 * Get update base
121 *
122 * @return Zend_Date|null
123 */
124 public function getUpdateBase()
125 {
126 $updateBase = $this->_getData('updateBase');
127 $date = null;
128 if ($updateBase) {
129 $date = new Zend_Date;
130 $date->set($updateBase, Zend_Date::W3C);
131 }
132 return $date;
133 }
134
135 /**
136 * Get the entry data specified by name
137 *
138 * @param string $name
139 * @param string $type
140 * @return mixed|null
141 */
142 private function _getData($name, $type = 'string')
143 {
144 if (array_key_exists($name, $this->_data)) {
145 return $this->_data[$name];
146 }
147
148 $data = $this->_xpath->evaluate($type . '(' . $this->getXpathPrefix() . '/syn10:' . $name . ')');
149
150 if (!$data) {
151 $data = null;
152 }
153
154 $this->_data[$name] = $data;
155
156 return $data;
157 }
158
159 /**
160 * Register Syndication namespaces
161 *
162 * @return void
163 */
164 protected function _registerNamespaces()
165 {
166 $this->_xpath->registerNamespace('syn10', 'http://purl.org/rss/1.0/modules/syndication/');
167 }
168}