PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Zend/Feed/Writer/Extension/ITunes/Feed.php

https://github.com/quarkness/piwik
PHP | 361 lines | 194 code | 18 blank | 149 comment | 31 complexity | b8e03c90284bb238714f4da52a3c6002 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_Writer
  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. * @category Zend
  23. * @package Zend_Feed_Writer
  24. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_Feed_Writer_Extension_ITunes_Feed
  28. {
  29. /**
  30. * Array of Feed data for rendering by Extension's renderers
  31. *
  32. * @var array
  33. */
  34. protected $_data = array();
  35. /**
  36. * Encoding of all text values
  37. *
  38. * @var string
  39. */
  40. protected $_encoding = 'UTF-8';
  41. /**
  42. * Set feed encoding
  43. *
  44. * @param string $enc
  45. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  46. */
  47. public function setEncoding($enc)
  48. {
  49. $this->_encoding = $enc;
  50. return $this;
  51. }
  52. /**
  53. * Get feed encoding
  54. *
  55. * @return string
  56. */
  57. public function getEncoding()
  58. {
  59. return $this->_encoding;
  60. }
  61. /**
  62. * Set a block value of "yes" or "no". You may also set an empty string.
  63. *
  64. * @param string
  65. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  66. */
  67. public function setItunesBlock($value)
  68. {
  69. if (!ctype_alpha($value) && strlen($value) > 0) {
  70. // require_once 'Zend/Feed/Exception.php';
  71. throw new Zend_Feed_Exception('invalid parameter: "block" may only'
  72. . ' contain alphabetic characters');
  73. }
  74. if (iconv_strlen($value, $this->getEncoding()) > 255) {
  75. // require_once 'Zend/Feed/Exception.php';
  76. throw new Zend_Feed_Exception('invalid parameter: "block" may only'
  77. . ' contain a maximum of 255 characters');
  78. }
  79. $this->_data['block'] = $value;
  80. return $this;
  81. }
  82. /**
  83. * Add feed authors
  84. *
  85. * @param array $values
  86. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  87. */
  88. public function addItunesAuthors(array $values)
  89. {
  90. foreach ($values as $value) {
  91. $this->addItunesAuthor($value);
  92. }
  93. return $this;
  94. }
  95. /**
  96. * Add feed author
  97. *
  98. * @param string $value
  99. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  100. */
  101. public function addItunesAuthor($value)
  102. {
  103. if (iconv_strlen($value, $this->getEncoding()) > 255) {
  104. // require_once 'Zend/Feed/Exception.php';
  105. throw new Zend_Feed_Exception('invalid parameter: any "author" may only'
  106. . ' contain a maximum of 255 characters each');
  107. }
  108. if (!isset($this->_data['authors'])) {
  109. $this->_data['authors'] = array();
  110. }
  111. $this->_data['authors'][] = $value;
  112. return $this;
  113. }
  114. /**
  115. * Set feed categories
  116. *
  117. * @param array $values
  118. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  119. */
  120. public function setItunesCategories(array $values)
  121. {
  122. if (!isset($this->_data['categories'])) {
  123. $this->_data['categories'] = array();
  124. }
  125. foreach ($values as $key=>$value) {
  126. if (!is_array($value)) {
  127. if (iconv_strlen($value, $this->getEncoding()) > 255) {
  128. // require_once 'Zend/Feed/Exception.php';
  129. throw new Zend_Feed_Exception('invalid parameter: any "category" may only'
  130. . ' contain a maximum of 255 characters each');
  131. }
  132. $this->_data['categories'][] = $value;
  133. } else {
  134. if (iconv_strlen($key, $this->getEncoding()) > 255) {
  135. // require_once 'Zend/Feed/Exception.php';
  136. throw new Zend_Feed_Exception('invalid parameter: any "category" may only'
  137. . ' contain a maximum of 255 characters each');
  138. }
  139. $this->_data['categories'][$key] = array();
  140. foreach ($value as $val) {
  141. if (iconv_strlen($val, $this->getEncoding()) > 255) {
  142. // require_once 'Zend/Feed/Exception.php';
  143. throw new Zend_Feed_Exception('invalid parameter: any "category" may only'
  144. . ' contain a maximum of 255 characters each');
  145. }
  146. $this->_data['categories'][$key][] = $val;
  147. }
  148. }
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Set feed image (icon)
  154. *
  155. * @param string $value
  156. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  157. */
  158. public function setItunesImage($value)
  159. {
  160. if (!Zend_Uri::check($value)) {
  161. // require_once 'Zend/Feed/Exception.php';
  162. throw new Zend_Feed_Exception('invalid parameter: "image" may only'
  163. . ' be a valid URI/IRI');
  164. }
  165. if (!in_array(substr($value, -3), array('jpg','png'))) {
  166. // require_once 'Zend/Feed/Exception.php';
  167. throw new Zend_Feed_Exception('invalid parameter: "image" may only'
  168. . ' use file extension "jpg" or "png" which must be the last three'
  169. . ' characters of the URI (i.e. no query string or fragment)');
  170. }
  171. $this->_data['image'] = $value;
  172. return $this;
  173. }
  174. /**
  175. * Set feed cumulative duration
  176. *
  177. * @param string $value
  178. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  179. */
  180. public function setItunesDuration($value)
  181. {
  182. $value = (string) $value;
  183. if (!ctype_digit($value)
  184. && !preg_match("/^\d+:[0-5]{1}[0-9]{1}$/", $value)
  185. && !preg_match("/^\d+:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/", $value)
  186. ) {
  187. // require_once 'Zend/Feed/Exception.php';
  188. throw new Zend_Feed_Exception('invalid parameter: "duration" may only'
  189. . ' be of a specified [[HH:]MM:]SS format');
  190. }
  191. $this->_data['duration'] = $value;
  192. return $this;
  193. }
  194. /**
  195. * Set "explicit" flag
  196. *
  197. * @param bool $value
  198. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  199. */
  200. public function setItunesExplicit($value)
  201. {
  202. if (!in_array($value, array('yes','no','clean'))) {
  203. // require_once 'Zend/Feed/Exception.php';
  204. throw new Zend_Feed_Exception('invalid parameter: "explicit" may only'
  205. . ' be one of "yes", "no" or "clean"');
  206. }
  207. $this->_data['explicit'] = $value;
  208. return $this;
  209. }
  210. /**
  211. * Set feed keywords
  212. *
  213. * @param array $value
  214. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  215. */
  216. public function setItunesKeywords(array $value)
  217. {
  218. if (count($value) > 12) {
  219. // require_once 'Zend/Feed/Exception.php';
  220. throw new Zend_Feed_Exception('invalid parameter: "keywords" may only'
  221. . ' contain a maximum of 12 terms');
  222. }
  223. $concat = implode(',', $value);
  224. if (iconv_strlen($concat, $this->getEncoding()) > 255) {
  225. // require_once 'Zend/Feed/Exception.php';
  226. throw new Zend_Feed_Exception('invalid parameter: "keywords" may only'
  227. . ' have a concatenated length of 255 chars where terms are delimited'
  228. . ' by a comma');
  229. }
  230. $this->_data['keywords'] = $value;
  231. return $this;
  232. }
  233. /**
  234. * Set new feed URL
  235. *
  236. * @param string $value
  237. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  238. */
  239. public function setItunesNewFeedUrl($value)
  240. {
  241. if (!Zend_Uri::check($value)) {
  242. // require_once 'Zend/Feed/Exception.php';
  243. throw new Zend_Feed_Exception('invalid parameter: "newFeedUrl" may only'
  244. . ' be a valid URI/IRI');
  245. }
  246. $this->_data['newFeedUrl'] = $value;
  247. return $this;
  248. }
  249. /**
  250. * Add feed owners
  251. *
  252. * @param array $values
  253. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  254. */
  255. public function addItunesOwners(array $values)
  256. {
  257. foreach ($values as $value) {
  258. $this->addItunesOwner($value);
  259. }
  260. return $this;
  261. }
  262. /**
  263. * Add feed owner
  264. *
  265. * @param string $value
  266. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  267. */
  268. public function addItunesOwner(array $value)
  269. {
  270. if (!isset($value['name']) || !isset($value['email'])) {
  271. // require_once 'Zend/Feed/Exception.php';
  272. throw new Zend_Feed_Exception('invalid parameter: any "owner" must'
  273. . ' be an array containing keys "name" and "email"');
  274. }
  275. if (iconv_strlen($value['name'], $this->getEncoding()) > 255
  276. || iconv_strlen($value['email'], $this->getEncoding()) > 255
  277. ) {
  278. // require_once 'Zend/Feed/Exception.php';
  279. throw new Zend_Feed_Exception('invalid parameter: any "owner" may only'
  280. . ' contain a maximum of 255 characters each for "name" and "email"');
  281. }
  282. if (!isset($this->_data['owners'])) {
  283. $this->_data['owners'] = array();
  284. }
  285. $this->_data['owners'][] = $value;
  286. return $this;
  287. }
  288. /**
  289. * Set feed subtitle
  290. *
  291. * @param string $value
  292. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  293. */
  294. public function setItunesSubtitle($value)
  295. {
  296. if (iconv_strlen($value, $this->getEncoding()) > 255) {
  297. // require_once 'Zend/Feed/Exception.php';
  298. throw new Zend_Feed_Exception('invalid parameter: "subtitle" may only'
  299. . ' contain a maximum of 255 characters');
  300. }
  301. $this->_data['subtitle'] = $value;
  302. return $this;
  303. }
  304. /**
  305. * Set feed summary
  306. *
  307. * @param string $value
  308. * @return Zend_Feed_Writer_Extension_ITunes_Feed
  309. */
  310. public function setItunesSummary($value)
  311. {
  312. if (iconv_strlen($value, $this->getEncoding()) > 4000) {
  313. // require_once 'Zend/Feed/Exception.php';
  314. throw new Zend_Feed_Exception('invalid parameter: "summary" may only'
  315. . ' contain a maximum of 4000 characters');
  316. }
  317. $this->_data['summary'] = $value;
  318. return $this;
  319. }
  320. /**
  321. * Overloading: proxy to internal setters
  322. *
  323. * @param string $method
  324. * @param array $params
  325. * @return mixed
  326. */
  327. public function __call($method, array $params)
  328. {
  329. $point = Zend_Feed_Writer::lcfirst(substr($method, 9));
  330. if (!method_exists($this, 'setItunes' . ucfirst($point))
  331. && !method_exists($this, 'addItunes' . ucfirst($point))
  332. ) {
  333. // require_once 'Zend/Feed/Writer/Exception/InvalidMethodException.php';
  334. throw new Zend_Feed_Writer_Exception_InvalidMethodException(
  335. 'invalid method: ' . $method
  336. );
  337. }
  338. if (!array_key_exists($point, $this->_data) || empty($this->_data[$point])) {
  339. return null;
  340. }
  341. return $this->_data[$point];
  342. }
  343. }