PageRenderTime 62ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/zfw-MediaRSS/library/Zend/Feed/Writer/Extension/MediaRSS/Elements.php

https://bitbucket.org/onyxraven/zend-contrib
PHP | 1118 lines | 535 code | 66 blank | 517 comment | 91 complexity | 0ed3f18a52f69c7805b8be4c340c005f 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. * @subpackage Zend_Feed_Writer_Extensions_MediaRSS
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once 'Zend/Feed/Exception.php';
  23. /**
  24. * Zend Feed Writer Extension for MediaRSS Optional elements
  25. *
  26. * contains methods for the mediarss optional elements that can live in many places
  27. *
  28. * @link http://video.search.yahoo.com/mrss
  29. * @link http://www.rssboard.org/media-rss
  30. *
  31. * @category Zend
  32. * @package Zend_Feed_Writer
  33. * @subpackage Zend_Feed_Writer_Extensions_MediaRSS
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Feed_Writer_Extension_MediaRSS_Elements
  38. {
  39. /**
  40. * Internal array containing all data associated with this entry or item.
  41. *
  42. * @var array
  43. */
  44. protected $_data = array();
  45. /**
  46. * Encoding of all text values
  47. *
  48. * @var string
  49. */
  50. protected $_encoding = 'UTF-8';
  51. /**
  52. * Set the feed character encoding
  53. *
  54. * @return void
  55. */
  56. public function setEncoding($encoding)
  57. {
  58. if (empty($encoding) || !is_string($encoding)) {
  59. // require_once 'Zend/Feed/Exception.php';
  60. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
  61. }
  62. $this->_data['encoding'] = $encoding;
  63. }
  64. /**
  65. * Get the feed character encoding
  66. *
  67. * @return string|null
  68. */
  69. public function getEncoding()
  70. {
  71. if (!array_key_exists('encoding', $this->_data)) {
  72. return 'UTF-8';
  73. }
  74. return $this->_data['encoding'];
  75. }
  76. /**
  77. * Unset a specific data point
  78. *
  79. * @param string $name
  80. */
  81. public function remove($name)
  82. {
  83. if (isset($this->_data[$name])) {
  84. unset($this->_data[$name]);
  85. }
  86. }
  87. /**
  88. * Overloading: proxy to internal setters
  89. *
  90. * @param string $method
  91. * @param array $params
  92. * @return mixed
  93. */
  94. public function __call($method, array $params)
  95. {
  96. if (!method_exists($this, $method) && !method_exists($this, $method)) {
  97. require_once 'Zend/Feed/Writer/Exception/InvalidMethodException.php';
  98. throw new Zend_Feed_Writer_Exception_InvalidMethodException(
  99. 'invalid method: ' . $method
  100. );
  101. }
  102. if (!array_key_exists($point, $this->_data) || empty($this->_data[$point])) {
  103. return null;
  104. }
  105. return $this->_data[$point];
  106. }
  107. /**
  108. * Media rating scheme simple types (simple being default scheme)
  109. *
  110. * @var array
  111. */
  112. public static $rating_scheme_simple_types = array('adult','nonadult');
  113. /**
  114. * This allows the permissible audience to be declared. If this element
  115. * is not included, it assumes that no restrictions are necessary.
  116. * It has one optional attribute.
  117. *
  118. * @param string $value
  119. * @param string $scheme the URI that identifies the rating scheme.
  120. * It is an optional attribute. If this attribute is not included,
  121. * the default scheme is urn:simple (adult | nonadult)
  122. * @return void
  123. */
  124. public function addMediaRating($value, $scheme=null)
  125. {
  126. if ($scheme === 'urn:simple' || is_null($scheme)) {
  127. if (!in_array($value, self::$rating_scheme_simple_types)) {
  128. throw new Zend_Feed_Exception('simple rating not valid');
  129. }
  130. }
  131. //todo validate scheme
  132. if ($value === '') {
  133. throw new Zend_Feed_Exception('rating must not be empty');
  134. }
  135. $this->_data['rating'][$scheme] = $value;
  136. }
  137. /**
  138. * add many media ratings at once
  139. *
  140. * @param array $vals each represented as $scheme => $rating
  141. * @return void
  142. */
  143. public function addMediaRatings(array $vals)
  144. {
  145. foreach ($vals as $scheme => $rating) {
  146. $this->addMediaRating($rating, $scheme);
  147. }
  148. }
  149. /**
  150. * The title of the particular media object
  151. *
  152. * @param string $value
  153. * @return void
  154. */
  155. public function setMediaTitle($value)
  156. {
  157. if ($value === '') {
  158. throw new Zend_Feed_Exception('title must not be empty');
  159. }
  160. $this->_data['title'] = $value;
  161. }
  162. /**
  163. * Short description describing the media object typically a sentence in length
  164. *
  165. * @param string $value
  166. * @return void
  167. */
  168. public function setMediaDescription($value)
  169. {
  170. if ($value === '') {
  171. throw new Zend_Feed_Exception('description must not be empty');
  172. }
  173. $this->_data['description'] = $value;
  174. }
  175. /**
  176. * Highly relevant keywords describing the media object with
  177. * typically a maximum of ten words.
  178. *
  179. * @param string|array $value
  180. * @return void
  181. */
  182. public function setMediaKeywords($value)
  183. {
  184. if (is_array($value)) {
  185. $value = implode(',', $value);
  186. }
  187. if ($value === '') {
  188. throw new Zend_Feed_Exception('keywords must not be empty');
  189. }
  190. $this->_data['keywords'] = $value;
  191. }
  192. /**
  193. * Allows particular images to be used as representative images for the
  194. * media object. If multiple thumbnails are included, and time coding is
  195. * not at play, it is assumed that the images are in order of importance.
  196. *
  197. * @param string $value the url of the thumbnail
  198. * @param int $height the height of the thumbnail. It is an optional attribute.
  199. * @param int $width the width of the thumbnail. It is an optional attribute
  200. * @param string $time the time offset in relation to the media object.
  201. * Typically this is used when creating multiple keyframes within a
  202. * single video. The format for this attribute should be in the DSM-CC's
  203. * Normal Play Time (NTP) as used in RTSP [RFC 2326 3.6 Normal Play Time].
  204. * It is an optional attribute
  205. * @return void
  206. */
  207. public function addMediaThumbnail($value, $height=null, $width=null, $time=null)
  208. {
  209. if (!Zend_Uri::check($value)) {
  210. throw new Zend_Feed_Exception('invalid url');
  211. }
  212. $thumb = array('url' => $value);
  213. if (!is_null($height)) {
  214. if (!is_int($height)) {
  215. throw new Zend_Feed_Exception('height must be int');
  216. }
  217. $thumb['height'] = $height;
  218. }
  219. if (!is_null($width)) {
  220. if (!is_int($width)) {
  221. throw new Zend_Feed_Exception('width must be int');
  222. }
  223. $thumb['width'] = $width;
  224. }
  225. if (!is_null($time)) {
  226. //todo validate NTP
  227. $thumb['time'] = $time;
  228. }
  229. $this->_data['thumbnail'][] = $thumb;
  230. }
  231. /**
  232. * add many thumbnails at once
  233. *
  234. * @param array $thumbs array of thumbs to add. thumbs are each represented as
  235. * an array('url'=>$url, 'height'=>$height, 'width'=>$width, 'time'=>$time)
  236. * @return void
  237. */
  238. public function addMediaThumbnails(array $thumbs)
  239. {
  240. foreach ($thumbs as $th) {
  241. $height = (empty($th['height'])) ? null : $th['height'];
  242. $width = (empty($th['width'])) ? null : $th['width'];
  243. $time = (empty($th['time'])) ? null : $th['time'];
  244. $this->addMediaThumbnail($th['url'], $height, $width, $time);
  245. }
  246. }
  247. /**
  248. * Allows a taxonomy to be set that gives an indication of the
  249. * type of media content, and its particular contents
  250. *
  251. * @param string $value
  252. * @param string $scheme the URI that identifies the categorization scheme.
  253. * It is an optional attribute. If this attribute is not included,
  254. * the default scheme is 'http://search.yahoo.com/mrss/category_schema'.
  255. * @param string $label the human readable label that can be displayed in
  256. * end user applications. It is an optional attribute.
  257. * @return void
  258. */
  259. public function addMediaCategory($value, $scheme = null, $label = null)
  260. {
  261. if ($value === '') {
  262. throw new Zend_Feed_Exception('category must not be empty');
  263. }
  264. $cat = array('category' => $value);
  265. if (!is_null($scheme)) {
  266. //todo validate scheme
  267. $cat['scheme'] = $scheme;
  268. }
  269. if (!is_null($label)) {
  270. $cat['label'] = $label;
  271. }
  272. $this->_data['category'][] = $cat;
  273. }
  274. /**
  275. * add many categories at once
  276. *
  277. * @param array $cats array of categories to add. categories are each represented as
  278. * an array('category'=>$cat, 'scheme'=>$scheme, 'label'=>$label)
  279. * @return void
  280. */
  281. public function addMediaCategories(array $cats)
  282. {
  283. foreach ($cats as $c) {
  284. $scheme = (empty($c['scheme'])) ? null : $c['scheme'];
  285. $label = (empty($c['label'])) ? null : $c['label'];
  286. $this->addMediaCategory($c['category'], $scheme, $label);
  287. }
  288. }
  289. /**
  290. * This is the hash of the binary media file. It can appear multiple times
  291. * as long as each instance is a different algo
  292. *
  293. * @param string $value hash value
  294. * @param mixed $algo hash algorithm (one of md5, sha1)
  295. * @return void
  296. */
  297. public function addMediaHash($value, $algo=null)
  298. {
  299. if ($value === '') {
  300. throw new Zend_Feed_Exception('hash must not be empty');
  301. }
  302. //todo validate hash (len, chars)?
  303. //todo validate algo?
  304. $this->_data['hash'][$algo] = $value;
  305. }
  306. /**
  307. * add many media hashes at once
  308. *
  309. * @param array $hashes array of hashes to add represented by an
  310. * array($algo => $hash)
  311. * @return void
  312. */
  313. public function addMediaHashes(array $hashes)
  314. {
  315. foreach ($hashes as $algo => $value) {
  316. $this->addMediaHash($value, $algo);
  317. }
  318. }
  319. /**
  320. * Allows the media object to be accessed through a web browser media
  321. * player console. This element is required only if a direct media url
  322. * attribute is not specified in the <media:content> element
  323. *
  324. * @param string $value the url of the player console that plays the media.
  325. * @param int $height the height of the browser window that the url should be opened in.
  326. * It is an optional attribute.
  327. * @param int $width the width of the browser window that the url should be opened in.
  328. * It is an optional attribute.
  329. * @return void
  330. */
  331. public function setMediaPlayer($value, $height = null, $width = null)
  332. {
  333. if (!Zend_Uri::check($value)) {
  334. throw new Zend_Feed_Exception('invalid url');
  335. }
  336. $player = array('url' => $value);
  337. if (!is_null($height)) {
  338. if (!is_int($height)) {
  339. throw new Zend_Feed_Exception('height must be int');
  340. }
  341. $player['height'] = $height;
  342. }
  343. if (!is_null($width)) {
  344. if (!is_int($width)) {
  345. throw new Zend_Feed_Exception('width must be int');
  346. }
  347. $player['width'] = $width;
  348. }
  349. $this->_data['player'] = $player;
  350. }
  351. /**
  352. * media credit schemes
  353. *
  354. * @var array
  355. */
  356. public static $credit_schemes = array('urn:ebu','urn:yvs');
  357. /**
  358. * media credit role 'urn:yvs' possible values
  359. *
  360. * @var array
  361. */
  362. public static $credit_role_yvs = array('uploader','owner');
  363. /**
  364. * Notable entity and the contribution to the creation of the media object.
  365. * Current entities can include people, companies, locations, etc. Specific
  366. * entities can have multiple roles, and several entities can have the same role.
  367. * These should appear as distinct <media:credit> elements.
  368. *
  369. * @param string $value
  370. * @param string $role role specifies the role the entity played.
  371. * It is an optional attribute.
  372. * @param string $scheme scheme is the URI that identifies the role scheme.
  373. * It is an optional attribute and possible values for this attribute are
  374. * ( urn:ebu | urn:yvs ) . The default scheme is 'urn:ebu'. The list of
  375. * roles supported under urn:ebu scheme can be found at:
  376. * http://www.ebu.ch/en/technical/metadata/specifications/role_codes.php
  377. * The roles supported under urn:yvs scheme are ( uploader | owner )
  378. * @return void
  379. */
  380. public function addMediaCredit($value, $role=null, $scheme=null)
  381. {
  382. if ($value === '') {
  383. throw new Zend_Feed_Exception('entity must not be empty');
  384. }
  385. $credit = array('entity' => $value);
  386. if (!is_null($role)) {
  387. if ($scheme === 'urn:yvs' && !in_array($role, self::$credit_role_yvs)) {
  388. throw new Zend_Feed_Exception('unsupported yvs role');
  389. }
  390. //todo validate urn:ebu
  391. $credit['role'] = $role;
  392. }
  393. if (!is_null($scheme)) {
  394. if (!in_array($scheme, self::$credit_schemes)) {
  395. throw new Zend_Feed_Exception('scheme unsupported');
  396. }
  397. $credit['scheme'] = $scheme;
  398. }
  399. $this->_data['credit'][] = $credit;
  400. }
  401. /**
  402. * add many media credits at once
  403. *
  404. * @param array $credits array of credits represented as an
  405. * array('name'=>$name, 'role'=>$role, 'scheme'=>$scheme)
  406. * @return void
  407. */
  408. public function addMediaCredits(array $credits)
  409. {
  410. foreach ($credits as $c) {
  411. $role = (empty($c['role'])) ? null : $c['role'];
  412. $scheme = (empty($c['scheme'])) ? null : $c['scheme'];
  413. $this->addMediaCredit($c['entity'], $role, $scheme);
  414. }
  415. }
  416. /**
  417. * Copyright information for media object.
  418. *
  419. * @param string $value
  420. * @param string $url the url for a terms of use page or additional copyright
  421. * information. If the media is operating under a Creative Commons license,
  422. * the Creative Commons module should be used instead. It is an optional attribute.
  423. * @return void
  424. */
  425. public function setMediaCopyright($value, $url=null)
  426. {
  427. if ($value === '') {
  428. throw new Zend_Feed_Exception('copyright value must not be empty');
  429. }
  430. $copy = array('value' => $value);
  431. if (!is_null($url)) {
  432. if (!Zend_Uri::check($url)) {
  433. throw new Zend_Feed_Exception('invalid url');
  434. }
  435. $copy['url'] = $url;
  436. }
  437. $this->_data['copyright'] = $copy;
  438. }
  439. /**
  440. * Allows the inclusion of a text transcript, closed captioning, or
  441. * lyrics of the media content. Many of these elements are permitted
  442. * to provide a time series of text. In such cases, it is encouraged,
  443. * but not required, that the elements be grouped by language and appear
  444. * in time sequence order based on the start time. Elements can have
  445. * overlapping start and end times.
  446. *
  447. * @param string $value
  448. * @param string $lang the primary language encapsulated in the media object.
  449. * Language codes possible are detailed in RFC 3066. This attribute is
  450. * used similar to the xml:lang attribute detailed in the XML 1.0
  451. * Specification (Third Edition). It is an optional attribute.
  452. * @param string $start the start time offset that the text starts being
  453. * relevant to the media object. An example of this would be for closed
  454. * captioning. It uses the NTP time code format (see: the time attribute
  455. * used in <media:thumbnail>). It is an optional attribute.
  456. * @param string $end the end time that the text is relevant. If this attribute
  457. * is not provided, and a start time is used, it is expected that the end
  458. * time is either the end of the clip or the start of the next <media:text> element.
  459. * @return void
  460. */
  461. public function addMediaText($value, $lang=null, $start = null, $end = null)
  462. {
  463. if ($value === '') {
  464. throw new Zend_Feed_Exception('text must not be empty');
  465. }
  466. $txt = array('text' => $value);
  467. if (!is_null($lang)) {
  468. //todo validate lang
  469. $txt['lang'] = $lang;
  470. }
  471. if (!is_null($start)) {
  472. //todo validate ntp
  473. $txt['start'] = $start;
  474. }
  475. if (!is_null($end)) {
  476. //todo validate ntp
  477. $txt['end'] = $end;
  478. }
  479. $this->_data['text'][] = $txt;
  480. }
  481. /**
  482. * add many media texts at once
  483. *
  484. * @param array $texts array of text entries represented with an
  485. * array('text'=>$text, 'lang'=>$lang, 'start'=>$start, 'end'=>$end)
  486. * @return void
  487. */
  488. public function addMediaTexts(array $texts)
  489. {
  490. foreach ($texts as $t) {
  491. $lang = (empty($t['lang'])) ? null : $t['lang'];
  492. $start = (empty($t['start'])) ? null : $t['start'];
  493. $end = (empty($t['end'])) ? null : $t['end'];
  494. $this->addMediaText($t['text'], $lang, $start, $end);
  495. }
  496. }
  497. /**
  498. * media restriction relationships
  499. *
  500. * @var array
  501. */
  502. public static $restriction_relationships = array('allow','deny');
  503. /**
  504. * media restriction types
  505. *
  506. * @var array
  507. */
  508. public static $restriction_types = array('country','uri','sharing');
  509. /**
  510. * Allows restrictions to be placed on the aggregator rendering the
  511. * media in the feed. Currently, restrictions are based on distributor
  512. * (uri), country codes and sharing of a media object. This element is
  513. * purely informational and no obligation can be assumed or implied. Only
  514. * one <media:restriction> element of the same type can be applied to a media
  515. * object - all others will be ignored. Entities in this element should be space
  516. * separated. To allow the producer to explicitly declare his/her intentions,
  517. * two literals are reserved: 'all', 'none'. These literals can only be used once.
  518. *
  519. * @param string $value
  520. * @param string $relationship ndicates the type of relationship that
  521. * the restriction represents (allow | deny).
  522. * @param string $type the type of restriction (country | uri | sharing )
  523. * that the media can be syndicated. It is an optional attribute;
  524. * however can only be excluded when using one of the literal
  525. * values "all" or "none".
  526. * * "country" allows restrictions to be placed based on
  527. * country code. [ISO 3166]
  528. * * "uri" allows restrictions based on URI.
  529. * Examples: urn:apple, http://images.google.com, urn:yahoo, etc.
  530. * * "sharing" allows restriction on sharing.deny means content
  531. * cannot be shared - e.g. via embed tags. If the sharing type
  532. * is not present, the default functionality is to allow sharing
  533. * @return void
  534. */
  535. public function addMediaRestriction($relationship, $type, $value=null)
  536. {
  537. if (!in_array($relationship, self::$restriction_relationships)) {
  538. throw new Zend_Feed_Exception('relationship unsupported');
  539. }
  540. $rel = array('relationship' => $relationship);
  541. if (!is_null($value)) {
  542. $rel['value'] = $value;
  543. }
  544. if (!is_null($type)) {
  545. if (!in_array($type, self::$restriction_types)) {
  546. throw new Zend_Feed_Exception('type unsupported');
  547. }
  548. } else {
  549. if (!in_array($value, array('all', 'none'))) {
  550. throw new Zend_Feed_Exception('value must be all or none if no type');
  551. }
  552. }
  553. $this->_data['restriction'][$type] = $rel;
  554. }
  555. /**
  556. * add many media restrictions at once
  557. *
  558. * @param array $restricts array of restrictions represented by an
  559. * array('relationship'=>$relationship, 'type'=>$type, 'value'=>$value)
  560. * @return void
  561. */
  562. public function addMediaRestrictions(array $restricts)
  563. {
  564. foreach ($restricts as $type => $r) {
  565. $value = (empty($r['value'])) ? null : $r['value'];
  566. $type = (empty($r['type'])) ? null : $r['type'];
  567. $this->addMediaRestriction($r['relationship'], $type, $value);
  568. }
  569. }
  570. /**
  571. * specifies the rating related information about a media object.
  572. *
  573. * @param float $avg
  574. * @param int $count
  575. * @param int $min
  576. * @param int $max
  577. * @return void
  578. */
  579. public function setMediaStarRating($avg, $count=null, $min=null, $max=null)
  580. {
  581. if (!is_numeric($avg)) {
  582. throw new Zend_Feed_Exception('avg should be float');
  583. }
  584. $star = array('average' => $avg);
  585. if (!is_null($count)) {
  586. if (!is_int($count)) {
  587. throw new Zend_Feed_Exception('count should be int');
  588. }
  589. $star['count'] = $count;
  590. }
  591. if (!is_null($min)) {
  592. if (!is_int($min)) {
  593. throw new Zend_Feed_Exception('min should be int');
  594. }
  595. $star['min'] = $min;
  596. }
  597. if (!is_null($max)) {
  598. if (!is_int($max)) {
  599. throw new Zend_Feed_Exception('max should be int');
  600. }
  601. $star['max'] = $max;
  602. }
  603. $this->_data['starRating'] = $star;
  604. }
  605. /**
  606. * specifies various statistics about a media object like
  607. * the view count and the favorite count.
  608. *
  609. * @param int $views
  610. * @param int $favorites
  611. * @return void
  612. */
  613. public function setMediaStatistics($views=null, $favorites=null)
  614. {
  615. $stat = array();
  616. if (!is_null($views)) {
  617. if (!is_int($views)) {
  618. throw new Zend_Feed_Exception('views should be int');
  619. }
  620. $stat['views'] = $views;
  621. }
  622. if (!is_null($favorites)) {
  623. if (!is_int($favorites)) {
  624. throw new Zend_Feed_Exception('favorites should be int');
  625. }
  626. $stat['favorites'] = $favorites;
  627. }
  628. $this->_data['statistics'] = $stat;
  629. }
  630. /**
  631. * contains user generated tags separated by commas in the decreasing
  632. * order of each tag's weight. Each tag can be assigned an integer
  633. * weight in <tag_name>:<weight> format. It's up to the provider to
  634. * choose the way weight is determined for a tag, for example, number
  635. * of occurence can be one way to decide weight of a particular tag.
  636. * Default weight is 1.
  637. *
  638. * @param array $params key=tag, value=weight
  639. * @return void
  640. */
  641. public function setMediaTags(array $params)
  642. {
  643. $strArray = array();
  644. foreach ($params as $tag => $weight) {
  645. //fixup non-weight items
  646. if (!is_numeric($weight)) {
  647. $params[$weight] = 1;
  648. unset($params[$tag]);
  649. }
  650. }
  651. arsort($params);
  652. foreach ($params as $tag => $weight) {
  653. if ($tag === '') {
  654. throw new Zend_Feed_Exception('tag should be not empty');
  655. }
  656. $strArray[] = $tag.':'.$weight;
  657. }
  658. $this->_data['tags'] = implode(',', $strArray);
  659. }
  660. /**
  661. * Allows inclusion of all the comments media object has received.
  662. *
  663. * @param string $value
  664. * @return void
  665. */
  666. public function addMediaComment($value)
  667. {
  668. if ($value === '') {
  669. throw new Zend_Feed_Exception('comment should be not empty');
  670. }
  671. $this->_data['comment'][] = $value;
  672. }
  673. /**
  674. * add many media comments
  675. *
  676. * @param array $comments
  677. * @return void
  678. */
  679. public function addMediaComments(array $comments)
  680. {
  681. foreach ($comments as $c) {
  682. $this->addMediaComment($c);
  683. }
  684. }
  685. /**
  686. * Sometimes player specific embed code is needed for a player to play
  687. * any video. <media:embed> allows inclusion of such information in the
  688. * form of key value pairs
  689. *
  690. * @param string $value player url
  691. * @param int $height height in px
  692. * @param int $width width in px
  693. * @param array $params key-value params
  694. * @return void
  695. */
  696. public function setMediaEmbed($value, $height=null, $width=null, $params=array())
  697. {
  698. if (!Zend_Uri::check($value)) {
  699. throw new Zend_Feed_Exception('invalid url');
  700. }
  701. $embed = array('url' => $value);
  702. if (!is_null($height)) {
  703. if (!is_int($height)) {
  704. throw new Zend_Feed_Exception('height must be int');
  705. }
  706. $embed['height'] = $height;
  707. }
  708. if (!is_null($width)) {
  709. if (!is_int($width)) {
  710. throw new Zend_Feed_Exception('width must be int');
  711. }
  712. $embed['width'] = $width;
  713. }
  714. if (!is_null($params)) {
  715. $embed['param'] = $params;
  716. }
  717. $this->_data['embed'] = $embed;
  718. }
  719. /**
  720. * Allows inclusion of a list of all media responses a media object has received.
  721. *
  722. * @param string $value
  723. * @return void
  724. */
  725. public function addMediaResponse($value)
  726. {
  727. if ($value === '') {
  728. throw new Zend_Feed_Exception('response should be not empty');
  729. }
  730. $this->_data['response'][] = $value;
  731. }
  732. /**
  733. * add many media responses at once
  734. *
  735. * @param array $responses array of responses
  736. * @return void
  737. */
  738. public function addMediaResponses(array $responses)
  739. {
  740. foreach ($responses as $r) {
  741. $this->addMediaResponse($r);
  742. }
  743. }
  744. /**
  745. * Allows inclusion of all the urls pointing to a media object.
  746. *
  747. * @param int $value
  748. * @return void
  749. */
  750. public function addMediaBackLink($value)
  751. {
  752. if (!Zend_Uri::check($value)) {
  753. throw new Zend_Feed_Exception('invalid url');
  754. }
  755. $this->_data['backLink'][] = $value;
  756. }
  757. public function addMediaBackLinks(array $links)
  758. {
  759. foreach ($links as $l) {
  760. $this->addMediaBackLink($l);
  761. }
  762. }
  763. /**
  764. * media status states
  765. *
  766. * @var array
  767. */
  768. public static $status_states = array('active','blocked','deleted');
  769. /**
  770. * specify the status of a media object - whether it's still active
  771. * or it has been blocked/deleted.
  772. *
  773. * @param string $value can have values "active", "blocked" or "deleted".
  774. * "active" means a media object is active in the system,
  775. * "blocked" means a media object is blocked by the publisher,
  776. * "deleted" means a media object has been deleted by the publisher.
  777. * @param string $reason a reason explaining why a media object has been
  778. * blocked/deleted. It can be plain text or a url.
  779. * @return void
  780. */
  781. public function setMediaStatus($value, $reason=null)
  782. {
  783. if (!in_array($value, self::$status_states)) {
  784. throw new Zend_Feed_Exception('unsupported state');
  785. }
  786. $status = array('state' => $value);
  787. if (!is_null($reason)) {
  788. $status['reason'] = $reason;
  789. }
  790. $this->_data['status'] = $status;
  791. }
  792. public static $price_types = array('rent', 'purchase', 'package', 'subscription');
  793. /**
  794. * tag to include pricing information about a media object. If this
  795. * tag is not present, the media object is supposed to be free.
  796. * One media object can have multiple instances of this tag for including
  797. * different pricing structures. The presence of this tag would mean that
  798. * media object is not free.
  799. *
  800. * @param string $type Valid values are "rent", "purchase", "package" or "subscription".
  801. * If nothing is specified, then the media is free.
  802. * @param string $info if the type is "package" or "subscription", then info
  803. * is a url ponting to package or subscription information.
  804. * This is an optional attribute.
  805. * @param float $price the price of the media object. This is an optional attribute.
  806. * @param string $currency use [ISO 4217] for currency codes.
  807. * This is an optional attribute.
  808. * @return void
  809. */
  810. public function addMediaPrice($type=null, $info=null, $value=null, $currency=null)
  811. {
  812. $price = array();
  813. if (!is_null($type)) {
  814. if (!in_array($type, self::$price_types)) {
  815. throw new Zend_Feed_Exception('invalid price type');
  816. }
  817. $price['type'] = $type;
  818. }
  819. if (!is_null($info)) {
  820. if (!Zend_Uri::check($info)) {
  821. throw new Zend_Feed_Exception('invalid url');
  822. }
  823. $price['info'] = $info;
  824. }
  825. if (!is_null($value)) {
  826. if (!is_numeric($value)) {
  827. throw new Zend_Feed_Exception('price must be float: '.$value);
  828. }
  829. $price['price'] = $value;
  830. }
  831. if (!is_null($currency)) {
  832. //todo validate currency
  833. $price['currency'] = $currency;
  834. }
  835. $this->_data['price'][] = $price;
  836. }
  837. /**
  838. * add many media prices at once
  839. *
  840. * @param array $prices array of price elements represented by an
  841. * array('type'=>$type, '$info'=>$info, 'price'=>$price, 'currency'=>$currency)
  842. * @return void
  843. */
  844. public function addMediaPrices(array $prices)
  845. {
  846. foreach ($prices as $p) {
  847. $type = (empty($p['type'])) ? null : $p['type'];
  848. $info = (empty($p['info'])) ? null : $p['info'];
  849. $price = (empty($p['price'])) ? null : $p['price'];
  850. $currency = (empty($p['currency'])) ? null : $p['currency'];
  851. $this->addMediaPrice($type, $info, $price, $currency);
  852. }
  853. }
  854. /**
  855. * link to specify the machine readable license associated with the content.
  856. *
  857. * @param string $value
  858. * @param string $href
  859. * @param string $type
  860. * @return void
  861. */
  862. public function setMediaLicense($value, $href, $type)
  863. {
  864. //todo validate mime type
  865. if (!Zend_Uri::check($href)) {
  866. throw new Zend_Feed_Exception('invalid url');
  867. }
  868. $this->_data['license'] = array('value' => $value,
  869. 'href' => $href,
  870. 'type' => $type);
  871. }
  872. /**
  873. * element for subtitle/CC link. It contains type and language attributes.
  874. * Language is based on RFC 3066. There can be more than one such tag per
  875. * media element e.g. one per language. Please refer to Timed Text spec -
  876. * W3C for more information on Timed Text and Real Time Subtitling
  877. *
  878. * @param string $type
  879. * @param string $lang
  880. * @param string $href
  881. * @return void
  882. */
  883. public function addMediaSubTitle($type, $lang, $href)
  884. {
  885. //todo validate mime type
  886. //todo validate lang
  887. if (!Zend_Uri::check($href)) {
  888. throw new Zend_Feed_Exception('invalid url');
  889. }
  890. $this->_data['subTitle'][] = array('type' => $type,
  891. 'lang' => $lang,
  892. 'href' => $href);
  893. }
  894. /**
  895. * add many media subtitle links at once
  896. *
  897. * @param array $subs array of subtitle entries represented as an
  898. * array('type'=>$type, 'lang'=>$lang, 'href'=>$href)
  899. * @return void
  900. */
  901. public function addMediaSubTitles(array $subs)
  902. {
  903. foreach ($subs as $s) {
  904. $this->addMediaSubTitle($s['type'], $s['lang'], $s['href']);
  905. }
  906. }
  907. /**
  908. * element for P2P link.
  909. *
  910. * @param string $type
  911. * @param string $href
  912. * @return void
  913. */
  914. public function addMediaPeerLink($type, $href)
  915. {
  916. //todo validate mime type
  917. if (!Zend_Uri::check($href)) {
  918. throw new Zend_Feed_Exception('invalid url');
  919. }
  920. $this->_data['peerLink'][] = array('href' => $href,
  921. 'type' => $type);
  922. }
  923. /**
  924. * add many peer links at once
  925. *
  926. * @param array $links as array('type'=>$type, 'href'=>$href);
  927. * @return void
  928. */
  929. public function addMediaPeerLinks(array $links)
  930. {
  931. foreach ($links as $l) {
  932. $this->addMediaPeerLink($l['type'], $l['href']);
  933. }
  934. }
  935. /**
  936. * element to specify geographical information about various
  937. * locations captured in the content of a media object.
  938. * The format conforms to geoRSS.
  939. *
  940. * @param string $value description of the place whose location is being specified.
  941. * @param string $start time at which the reference to a particular location
  942. * starts in the media object.
  943. * @param string $end time at which the reference to a particular location
  944. * ends in the media object.
  945. * @param mixed $params //todo georss element
  946. * @return void
  947. */
  948. public function addMediaLocation($value, $geo, $start=null, $end=null)
  949. {
  950. //todo validate geo
  951. $loc = array('description' => $value,
  952. 'georss' => $geo);
  953. if (!is_null($start)) {
  954. //todo validate ntp
  955. $loc['start'] = $start;
  956. }
  957. if (!is_null($end)) {
  958. //todo validate ntp
  959. $loc['end'] = $end;
  960. }
  961. $this->_data['location'][] = $loc;
  962. }
  963. /**
  964. * media right status
  965. *
  966. * @var array
  967. */
  968. public static $rights_status = array('userCreated','official');
  969. /**
  970. * element to specify the rights information of a media object.
  971. *
  972. * @param string $value the status of the media object saying whether
  973. * a media object has been created by the publisher or they have
  974. * rights to circulate it. Supported values are "userCreated" and "official".
  975. * @return void
  976. */
  977. public function setMediaRights($value)
  978. {
  979. if (!in_array($value, self::$rights_status)) {
  980. throw new Zend_Feed_Exception('unsupported status');
  981. }
  982. $this->_data['rights'] = $value;
  983. }
  984. /**
  985. * element to specify various scenes within a media object. It can
  986. * have multiple child <media:scene> elements, where each <media:scene>
  987. * element contains information about a particular scene. <media:scene>
  988. * has optional sub-elements as "sceneTitle","sceneDescription",
  989. * "sceneStartTime" and "sceneEndTime", which contains title, description,
  990. * start and end time of a particular scene in the media respectively.
  991. *
  992. * @param string $title
  993. * @param string $description
  994. * @param string $start
  995. * @param string $end
  996. * @return void
  997. */
  998. public function addMediaScene($title=null, $description=null, $start=null, $end=null)
  999. {
  1000. $scene = array();
  1001. if (!is_null($title)) {
  1002. $scene['title'] = $title;
  1003. }
  1004. if (!is_null($description)) {
  1005. $scene['description'] = $description;
  1006. }
  1007. if (!is_null($start)) {
  1008. //todo validate ntp
  1009. $scene['start'] = $start;
  1010. }
  1011. if (!is_null($end)) {
  1012. //todo validate ntp
  1013. $scene['end'] = $end;
  1014. }
  1015. $this->_data['scene'][] = $scene;
  1016. }
  1017. /**
  1018. * add many media scenes at once
  1019. *
  1020. * @param array $scenes represented as an
  1021. * array('title'=>$title, 'description'=>$description, 'start'=>$start, 'end'=>$end)
  1022. * @return void
  1023. */
  1024. public function addMediaScenes(array $scenes)
  1025. {
  1026. foreach ($scenes as $s) {
  1027. $title = (empty($s['title'])) ? null : $s['title'];
  1028. $description = (empty($s['description'])) ? null : $s['description'];
  1029. $start = (empty($s['start'])) ? null : $s['start'];
  1030. $end = (empty($s['end'])) ? null : $s['end'];
  1031. $this->addMediaScene($title, $description, $start, $end);
  1032. }
  1033. }
  1034. /**
  1035. * getData
  1036. *
  1037. * @return array
  1038. */
  1039. public function getData()
  1040. {
  1041. return $this->_data;
  1042. }
  1043. /**
  1044. * lets us know if this object has any elements
  1045. *
  1046. * @return bool
  1047. */
  1048. public function isEmpty()
  1049. {
  1050. return empty($this->_data);
  1051. }
  1052. }