Abstract.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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-2015 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-2015 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. * @var array
  49. */
  50. protected $_attributes = array();
  51. /**
  52. * @param DOMElement $dom
  53. * @return void
  54. */
  55. public function __construct(DOMElement $dom)
  56. {
  57. $this->_dom = $dom;
  58. $this->_initXPath();
  59. $this->_init();
  60. }
  61. /**
  62. * @param string $tag
  63. * @param string $attribute
  64. * @return mixed
  65. */
  66. public function attributes($tag, $attribute = null)
  67. {
  68. if (null === $attribute) {
  69. // all attributes
  70. if (array_key_exists($tag, $this->_attributes)) {
  71. return $this->_attributes[$tag];
  72. }
  73. return array();
  74. }
  75. // a specific attribute
  76. if (isset($this->_attributes[$tag][$attribute])) {
  77. return $this->_attributes[$tag][$attribute];
  78. }
  79. return null;
  80. }
  81. /**
  82. * Initialize object.
  83. *
  84. * Post construct logic, classes must read their members here. Called from
  85. * {@link __construct()} as final step of object initialization.
  86. *
  87. * @return void
  88. */
  89. protected function _init()
  90. {
  91. }
  92. /**
  93. * Load DOMXPath for current DOM object.
  94. *
  95. * @see Zend_Service_Ebay_Finding::_parseResponse()
  96. * @return void
  97. */
  98. protected function _initXPath()
  99. {
  100. $document = $this->_dom->ownerDocument;
  101. if (!isset($document->ebayFindingXPath)) {
  102. $xpath = new DOMXPath($document);
  103. foreach (Zend_Service_Ebay_Finding::getXmlNamespaces() as $alias => $uri) {
  104. $xpath->registerNamespace($alias, $uri);
  105. }
  106. $document->ebayFindingXPath = $xpath;
  107. }
  108. $this->_xPath = $document->ebayFindingXPath;
  109. }
  110. /**
  111. * @return DOMElement
  112. */
  113. public function getDom()
  114. {
  115. return $this->_dom;
  116. }
  117. /**
  118. * @return DOMXPath
  119. */
  120. public function getXPath()
  121. {
  122. return $this->_xPath;
  123. }
  124. /**
  125. * @param string $path
  126. * @param string $type
  127. * @param string $array When true means it expects more than one node occurence
  128. * @return mixed
  129. */
  130. protected function _query($path, $type, $array = false)
  131. {
  132. // find values
  133. $values = array();
  134. $nodes = $this->_xPath->query($path, $this->_dom);
  135. foreach ($nodes as $node) {
  136. $value = (string) $node->nodeValue;
  137. $values[] = Zend_Service_Ebay_Abstract::toPhpValue($value, $type);
  138. if (!$array) {
  139. break;
  140. }
  141. }
  142. // array
  143. if ($array) {
  144. return $values;
  145. }
  146. // single value
  147. if (count($values)) {
  148. return reset($values);
  149. }
  150. // no nodes fount
  151. return null;
  152. }
  153. }