Result.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * Represents a single Technorati Search query result object.
  24. * It is never returned as a standalone object,
  25. * but it always belongs to a valid Zend_Service_Technorati_SearchResultSet object.
  26. *
  27. * @category Zend
  28. * @package Zend_Service
  29. * @subpackage Technorati
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @abstract
  33. */
  34. abstract class Zend_Service_Technorati_Result
  35. {
  36. /**
  37. * An associative array of 'fieldName' => 'xmlfieldtag'
  38. *
  39. * @var array
  40. * @access protected
  41. */
  42. protected $_fields;
  43. /**
  44. * The ReST fragment for this result object
  45. *
  46. * @var DomElement
  47. * @access protected
  48. */
  49. protected $_dom;
  50. /**
  51. * Object for $this->_dom
  52. *
  53. * @var DOMXpath
  54. * @access protected
  55. */
  56. protected $_xpath;
  57. /**
  58. * Constructs a new object from DOM Element.
  59. * Properties are automatically fetched from XML
  60. * according to array of $_fields to be read.
  61. *
  62. * @param DomElement $result the ReST fragment for this object
  63. */
  64. public function __construct(DomElement $dom)
  65. {
  66. $this->_xpath = new DOMXPath($dom->ownerDocument);
  67. $this->_dom = $dom;
  68. // default fields for all search results
  69. $fields = array();
  70. // merge with child's object fields
  71. $this->_fields = array_merge($this->_fields, $fields);
  72. // add results to appropriate fields
  73. foreach($this->_fields as $phpName => $xmlName) {
  74. $query = "./$xmlName/text()";
  75. $node = $this->_xpath->query($query, $this->_dom);
  76. if ($node->length == 1) {
  77. $this->{$phpName} = (string) $node->item(0)->data;
  78. }
  79. }
  80. }
  81. /**
  82. * Parses weblog node and sets weblog object.
  83. *
  84. * @return void
  85. */
  86. protected function _parseWeblog()
  87. {
  88. // weblog object field
  89. $result = $this->_xpath->query('./weblog', $this->_dom);
  90. if ($result->length == 1) {
  91. /**
  92. * @see Zend_Service_Technorati_Weblog
  93. */
  94. require_once 'Zend/Service/Technorati/Weblog.php';
  95. $this->_weblog = new Zend_Service_Technorati_Weblog($result->item(0));
  96. } else {
  97. $this->_weblog = null;
  98. }
  99. }
  100. /**
  101. * Returns the document fragment for this object as XML string.
  102. *
  103. * @return string the document fragment for this object
  104. * converted into XML format
  105. */
  106. public function getXml()
  107. {
  108. return $this->_dom->ownerDocument->saveXML($this->_dom);
  109. }
  110. }