ActivityEntry.php 6.7 KB

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