ActivityEntry.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_Gdata
  17. * @subpackage Health
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Gdata_Entry
  23. */
  24. require_once 'Zend/Gdata/Entry.php';
  25. /**
  26. * @see Zend_Gdata_YouTube_Extension_VideoId
  27. */
  28. require_once 'Zend/Gdata/YouTube/Extension/VideoId.php';
  29. /**
  30. * @see Zend_Gdata_YouTube_Extension_Username
  31. */
  32. require_once 'Zend/Gdata/YouTube/Extension/Username.php';
  33. /**
  34. * @see Zend_Gdata_YouTube_Extension_Rating
  35. */
  36. require_once 'Zend/Gdata/Extension/Rating.php';
  37. /**
  38. * A concrete class for working with YouTube user activity entries.
  39. *
  40. * @link http://code.google.com/apis/youtube/
  41. *
  42. * @category Zend
  43. * @package Zend_Gdata
  44. * @subpackage YouTube
  45. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  46. * @license http://framework.zend.com/license/new-bsd New BSD License
  47. */
  48. class Zend_Gdata_YouTube_ActivityEntry extends Zend_Gdata_Entry
  49. {
  50. const ACTIVITY_CATEGORY_SCHEME =
  51. 'http://gdata.youtube.com/schemas/2007/userevents.cat';
  52. /**
  53. * The classname for individual user activity entry elements.
  54. *
  55. * @var string
  56. */
  57. protected $_entryClassName = 'Zend_Gdata_YouTube_ActivityEntry';
  58. /**
  59. * The ID of the video that was part of the activity
  60. *
  61. * @var Zend_Gdata_YouTube_VideoId
  62. */
  63. protected $_videoId = null;
  64. /**
  65. * The username for the user that was part of the activity
  66. *
  67. * @var Zend_Gdata_YouTube_Username
  68. */
  69. protected $_username = null;
  70. /**
  71. * The rating element that was part of the activity
  72. *
  73. * @var Zend_Gdata_Extension_Rating
  74. */
  75. protected $_rating = null;
  76. /**
  77. * Constructs a new Zend_Gdata_YouTube_ActivityEntry object.
  78. * @param DOMElement $element (optional) The DOMElement on which to
  79. * base this object.
  80. */
  81. public function __construct($element = null)
  82. {
  83. $this->registerAllNamespaces(Zend_Gdata_YouTube::$namespaces);
  84. parent::__construct($element);
  85. }
  86. /**
  87. * Retrieves a DOMElement which corresponds to this element and all
  88. * child properties. This is used to build an entry back into a DOM
  89. * and eventually XML text for application storage/persistence.
  90. *
  91. * @param DOMDocument $doc The DOMDocument used to construct DOMElements
  92. * @return DOMElement The DOMElement representing this element and all
  93. * child properties.
  94. */
  95. public function getDOM($doc = null, $majorVersion = 1, $minorVersion = null)
  96. {
  97. $element = parent::getDOM($doc, $majorVersion, $minorVersion);
  98. if ($this->_videoId !== null) {
  99. $element->appendChild($this->_videoId->getDOM(
  100. $element->ownerDocument));
  101. }
  102. if ($this->_username !== null) {
  103. $element->appendChild($this->_username->getDOM(
  104. $element->ownerDocument));
  105. }
  106. if ($this->_rating !== null) {
  107. $element->appendChild($this->_rating->getDOM(
  108. $element->ownerDocument));
  109. }
  110. return $element;
  111. }
  112. /**
  113. * Creates individual Entry objects of the appropriate type and
  114. * stores them as members of this entry based upon DOM data.
  115. *
  116. * @param DOMNode $child The DOMNode to process
  117. */
  118. protected function takeChildFromDOM($child)
  119. {
  120. $absoluteNodeName = $child->namespaceURI . ':' . $child->localName;
  121. switch ($absoluteNodeName) {
  122. case $this->lookupNamespace('yt') . ':' . 'videoid':
  123. $videoId = new Zend_Gdata_YouTube_Extension_VideoId();
  124. $videoId->transferFromDOM($child);
  125. $this->_videoId = $videoId;
  126. break;
  127. case $this->lookupNamespace('yt') . ':' . 'username':
  128. $username = new Zend_Gdata_YouTube_Extension_Username();
  129. $username->transferFromDOM($child);
  130. $this->_username = $username;
  131. break;
  132. case $this->lookupNamespace('gd') . ':' . 'rating':
  133. $rating = new Zend_Gdata_Extension_Rating();
  134. $rating->transferFromDOM($child);
  135. $this->_rating = $rating;
  136. break;
  137. default:
  138. parent::takeChildFromDOM($child);
  139. break;
  140. }
  141. }
  142. /**
  143. * Returns the video ID for this activity entry.
  144. *
  145. * @return null|Zend_Gdata_YouTube_Extension_VideoId
  146. */
  147. public function getVideoId()
  148. {
  149. return $this->_videoId;
  150. }
  151. /**
  152. * Returns the username for this activity entry.
  153. *
  154. * @return null|Zend_Gdata_YouTube_Extension_Username
  155. */
  156. public function getUsername()
  157. {
  158. return $this->_username;
  159. }
  160. /**
  161. * Returns the rating for this activity entry.
  162. *
  163. * @return null|Zend_Gdata_YouTube_Extension_Rating
  164. */
  165. public function getRating()
  166. {
  167. return $this->_rating;
  168. }
  169. /**
  170. * Return the value of the rating for this video entry.
  171. *
  172. * Convenience method to save needless typing.
  173. *
  174. * @return integer|null The value of the rating that was created, if found.
  175. */
  176. public function getRatingValue()
  177. {
  178. $rating = $this->_rating;
  179. if ($rating) {
  180. return $rating->getValue();
  181. }
  182. return null;
  183. }
  184. /**
  185. * Return the activity type that was performed.
  186. *
  187. * Convenience method that inspects category where scheme is
  188. * http://gdata.youtube.com/schemas/2007/userevents.cat.
  189. *
  190. * @return string|null The activity category if found.
  191. */
  192. public function getActivityType()
  193. {
  194. $categories = $this->getCategory();
  195. foreach($categories as $category) {
  196. if ($category->getScheme() == self::ACTIVITY_CATEGORY_SCHEME) {
  197. return $category->getTerm();
  198. }
  199. }
  200. return null;
  201. }
  202. /**
  203. * Convenience method to quickly get access to the author of the activity
  204. *
  205. * @return string The author of the activity
  206. */
  207. public function getAuthorName()
  208. {
  209. $authors = $this->getAuthor();
  210. return $authors[0]->getName()->getText();
  211. }
  212. }