Abstract.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 Ebay
  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: Abstract.php 22791 2010-08-04 16:11:47Z renanbr $
  21. */
  22. /**
  23. * @see Zend_Service_Ebay_Abstract
  24. */
  25. require_once 'Zend/Service/Ebay/Abstract.php';
  26. /**
  27. * @see Zend_Service_Ebay_Finding
  28. */
  29. require_once 'Zend/Service/Ebay/Finding.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Service
  33. * @subpackage Ebay
  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. abstract class Zend_Service_Ebay_Finding_Abstract
  38. {
  39. /**
  40. * @var DOMElement
  41. */
  42. protected $_dom;
  43. /**
  44. * @var DOMXPath
  45. */
  46. protected $_xPath;
  47. /**
  48. * @param DOMElement $dom
  49. * @return void
  50. */
  51. public function __construct(DOMElement $dom)
  52. {
  53. $this->_dom = $dom;
  54. $this->_initXPath();
  55. $this->_init();
  56. }
  57. /**
  58. * Initialize object.
  59. *
  60. * Post construct logic, classes must read their members here. Called from
  61. * {@link __construct()} as final step of object initialization.
  62. *
  63. * @return void
  64. */
  65. protected function _init()
  66. {
  67. }
  68. /**
  69. * Load DOMXPath for current DOM object.
  70. *
  71. * @see Zend_Service_Ebay_Finding::_parseResponse()
  72. * @return void
  73. */
  74. protected function _initXPath()
  75. {
  76. $document = $this->_dom->ownerDocument;
  77. if (!isset($document->ebayFindingXPath)) {
  78. $xpath = new DOMXPath($document);
  79. foreach (Zend_Service_Ebay_Finding::getXmlNamespaces() as $alias => $uri) {
  80. $xpath->registerNamespace($alias, $uri);
  81. }
  82. $document->ebayFindingXPath = $xpath;
  83. }
  84. $this->_xPath = $document->ebayFindingXPath;
  85. }
  86. /**
  87. * @return DOMElement
  88. */
  89. public function getDom()
  90. {
  91. return $this->_dom;
  92. }
  93. /**
  94. * @return DOMXPath
  95. */
  96. public function getXPath()
  97. {
  98. return $this->_xPath;
  99. }
  100. /**
  101. * @param string $path
  102. * @param string $type
  103. * @param string $array When true means it expects more than one node occurence
  104. * @return mixed
  105. */
  106. protected function _query($path, $type, $array = false)
  107. {
  108. // find values
  109. $values = array();
  110. $nodes = $this->_xPath->query($path, $this->_dom);
  111. foreach ($nodes as $node) {
  112. $value = (string) $node->nodeValue;
  113. $values[] = Zend_Service_Ebay_Abstract::toPhpValue($value, $type);
  114. if (!$array) {
  115. break;
  116. }
  117. }
  118. // array
  119. if ($array) {
  120. return $values;
  121. }
  122. // single value
  123. if (count($values)) {
  124. return reset($values);
  125. }
  126. // no nodes fount
  127. return null;
  128. }
  129. }