CosmosResultSet.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_Service
  17. * @subpackage Technorati
  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_Service_Technorati_ResultSet
  24. */
  25. require_once 'Zend/Service/Technorati/ResultSet.php';
  26. /**
  27. * Represents a Technorati Cosmos query result set.
  28. *
  29. * @category Zend
  30. * @package Zend_Service
  31. * @subpackage Technorati
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Service_Technorati_CosmosResultSet extends Zend_Service_Technorati_ResultSet
  36. {
  37. /**
  38. * Technorati weblog url, if queried URL is a valid weblog.
  39. *
  40. * @var Zend_Uri_Http
  41. * @access protected
  42. */
  43. protected $_url;
  44. /**
  45. * Technorati weblog, if queried URL is a valid weblog.
  46. *
  47. * @var Zend_Service_Technorati_Weblog
  48. * @access protected
  49. */
  50. protected $_weblog;
  51. /**
  52. * Number of unique blogs linking this blog
  53. *
  54. * @var integer
  55. * @access protected
  56. */
  57. protected $_inboundBlogs;
  58. /**
  59. * Number of incoming links to this blog
  60. *
  61. * @var integer
  62. * @access protected
  63. */
  64. protected $_inboundLinks;
  65. /**
  66. * Parses the search response and retrieve the results for iteration.
  67. *
  68. * @param DomDocument $dom the ReST fragment for this object
  69. * @param array $options query options as associative array
  70. */
  71. public function __construct(DomDocument $dom, $options = array())
  72. {
  73. parent::__construct($dom, $options);
  74. $result = $this->_xpath->query('/tapi/document/result/inboundlinks/text()');
  75. if ($result->length == 1) $this->_inboundLinks = (int) $result->item(0)->data;
  76. $result = $this->_xpath->query('/tapi/document/result/inboundblogs/text()');
  77. if ($result->length == 1) $this->_inboundBlogs = (int) $result->item(0)->data;
  78. $result = $this->_xpath->query('/tapi/document/result/weblog');
  79. if ($result->length == 1) {
  80. /**
  81. * @see Zend_Service_Technorati_Weblog
  82. */
  83. require_once 'Zend/Service/Technorati/Weblog.php';
  84. $this->_weblog = new Zend_Service_Technorati_Weblog($result->item(0));
  85. }
  86. $result = $this->_xpath->query('/tapi/document/result/url/text()');
  87. if ($result->length == 1) {
  88. try {
  89. // fetched URL often doens't include schema
  90. // and this issue causes the following line to fail
  91. $this->_url = Zend_Service_Technorati_Utils::normalizeUriHttp($result->item(0)->data);
  92. } catch(Zend_Service_Technorati_Exception $e) {
  93. if ($this->getWeblog() instanceof Zend_Service_Technorati_Weblog) {
  94. $this->_url = $this->getWeblog()->getUrl();
  95. }
  96. }
  97. }
  98. $this->_totalResultsReturned = (int) $this->_xpath->evaluate("count(/tapi/document/item)");
  99. // total number of results depends on query type
  100. // for now check only getInboundLinks() and getInboundBlogs() value
  101. if ((int) $this->getInboundLinks() > 0) {
  102. $this->_totalResultsAvailable = $this->getInboundLinks();
  103. } elseif ((int) $this->getInboundBlogs() > 0) {
  104. $this->_totalResultsAvailable = $this->getInboundBlogs();
  105. } else {
  106. $this->_totalResultsAvailable = 0;
  107. }
  108. }
  109. /**
  110. * Returns the weblog URL.
  111. *
  112. * @return Zend_Uri_Http
  113. */
  114. public function getUrl() {
  115. return $this->_url;
  116. }
  117. /**
  118. * Returns the weblog.
  119. *
  120. * @return Zend_Service_Technorati_Weblog
  121. */
  122. public function getWeblog() {
  123. return $this->_weblog;
  124. }
  125. /**
  126. * Returns number of unique blogs linking this blog.
  127. *
  128. * @return integer the number of inbound blogs
  129. */
  130. public function getInboundBlogs()
  131. {
  132. return $this->_inboundBlogs;
  133. }
  134. /**
  135. * Returns number of incoming links to this blog.
  136. *
  137. * @return integer the number of inbound links
  138. */
  139. public function getInboundLinks()
  140. {
  141. return $this->_inboundLinks;
  142. }
  143. /**
  144. * Implements Zend_Service_Technorati_ResultSet::current().
  145. *
  146. * @return Zend_Service_Technorati_CosmosResult current result
  147. */
  148. public function current()
  149. {
  150. /**
  151. * @see Zend_Service_Technorati_CosmosResult
  152. */
  153. require_once 'Zend/Service/Technorati/CosmosResult.php';
  154. return new Zend_Service_Technorati_CosmosResult($this->_results->item($this->_currentIndex));
  155. }
  156. }