Item.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 Amazon
  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. * @category Zend
  24. * @package Zend_Service
  25. * @subpackage Amazon
  26. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Service_Amazon_Item
  30. {
  31. /**
  32. * @var string
  33. */
  34. public $ASIN;
  35. /**
  36. * @var string
  37. */
  38. public $DetailPageURL;
  39. /**
  40. * @var int
  41. */
  42. public $SalesRank;
  43. /**
  44. * @var int
  45. */
  46. public $TotalReviews;
  47. /**
  48. * @var int
  49. */
  50. public $AverageRating;
  51. /**
  52. * @var string
  53. */
  54. public $SmallImage;
  55. /**
  56. * @var string
  57. */
  58. public $MediumImage;
  59. /**
  60. * @var string
  61. */
  62. public $LargeImage;
  63. /**
  64. * @var string
  65. */
  66. public $Subjects;
  67. /**
  68. * @var Zend_Service_Amazon_OfferSet
  69. */
  70. public $Offers;
  71. /**
  72. * @var Zend_Service_Amazon_CustomerReview[]
  73. */
  74. public $CustomerReviews = array();
  75. /**
  76. * @var Zend_Service_Amazon_SimilarProducts[]
  77. */
  78. public $SimilarProducts = array();
  79. /**
  80. * @var Zend_Service_Amazon_Accessories[]
  81. */
  82. public $Accessories = array();
  83. /**
  84. * @var array
  85. */
  86. public $Tracks = array();
  87. /**
  88. * @var Zend_Service_Amazon_ListmaniaLists[]
  89. */
  90. public $ListmaniaLists = array();
  91. protected $_dom;
  92. /**
  93. * Parse the given <Item> element
  94. *
  95. * @param null|DOMElement $dom
  96. * @return void
  97. * @throws Zend_Service_Amazon_Exception
  98. *
  99. * @group ZF-9547
  100. */
  101. public function __construct($dom)
  102. {
  103. if (null === $dom) {
  104. require_once 'Zend/Service/Amazon/Exception.php';
  105. throw new Zend_Service_Amazon_Exception('Item element is empty');
  106. }
  107. if (!$dom instanceof DOMElement) {
  108. require_once 'Zend/Service/Amazon/Exception.php';
  109. throw new Zend_Service_Amazon_Exception('Item is not a valid DOM element');
  110. }
  111. $xpath = new DOMXPath($dom->ownerDocument);
  112. $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2011-08-01');
  113. $this->ASIN = $xpath->query('./az:ASIN/text()', $dom)->item(0)->data;
  114. $result = $xpath->query('./az:DetailPageURL/text()', $dom);
  115. if ($result->length == 1) {
  116. $this->DetailPageURL = $result->item(0)->data;
  117. }
  118. if ($xpath->query('./az:ItemAttributes/az:ListPrice', $dom)->length >= 1) {
  119. $this->CurrencyCode = (string) $xpath->query('./az:ItemAttributes/az:ListPrice/az:CurrencyCode/text()', $dom)->item(0)->data;
  120. $this->Amount = (int) $xpath->query('./az:ItemAttributes/az:ListPrice/az:Amount/text()', $dom)->item(0)->data;
  121. $this->FormattedPrice = (string) $xpath->query('./az:ItemAttributes/az:ListPrice/az:FormattedPrice/text()', $dom)->item(0)->data;
  122. }
  123. $result = $xpath->query('./az:ItemAttributes/az:*/text()', $dom);
  124. if ($result->length >= 1) {
  125. foreach ($result as $v) {
  126. if (isset($this->{$v->parentNode->tagName})) {
  127. if (is_array($this->{$v->parentNode->tagName})) {
  128. array_push($this->{$v->parentNode->tagName}, (string) $v->data);
  129. } else {
  130. $this->{$v->parentNode->tagName} = array($this->{$v->parentNode->tagName}, (string) $v->data);
  131. }
  132. } else {
  133. $this->{$v->parentNode->tagName} = (string) $v->data;
  134. }
  135. }
  136. }
  137. foreach (array('SmallImage', 'MediumImage', 'LargeImage') as $im) {
  138. $result = $xpath->query("./az:ImageSets/az:ImageSet[position() = 1]/az:$im", $dom);
  139. if ($result->length == 1) {
  140. /**
  141. * @see Zend_Service_Amazon_Image
  142. */
  143. require_once 'Zend/Service/Amazon/Image.php';
  144. $this->$im = new Zend_Service_Amazon_Image($result->item(0));
  145. }
  146. }
  147. $result = $xpath->query('./az:SalesRank/text()', $dom);
  148. if ($result->length == 1) {
  149. $this->SalesRank = (int) $result->item(0)->data;
  150. }
  151. $result = $xpath->query('./az:CustomerReviews/az:Review', $dom);
  152. if ($result->length >= 1) {
  153. /**
  154. * @see Zend_Service_Amazon_CustomerReview
  155. */
  156. require_once 'Zend/Service/Amazon/CustomerReview.php';
  157. foreach ($result as $review) {
  158. $this->CustomerReviews[] = new Zend_Service_Amazon_CustomerReview($review);
  159. }
  160. $this->AverageRating = (float) $xpath->query('./az:CustomerReviews/az:AverageRating/text()', $dom)->item(0)->data;
  161. $this->TotalReviews = (int) $xpath->query('./az:CustomerReviews/az:TotalReviews/text()', $dom)->item(0)->data;
  162. }
  163. $result = $xpath->query('./az:EditorialReviews/az:*', $dom);
  164. if ($result->length >= 1) {
  165. /**
  166. * @see Zend_Service_Amazon_EditorialReview
  167. */
  168. require_once 'Zend/Service/Amazon/EditorialReview.php';
  169. foreach ($result as $r) {
  170. $this->EditorialReviews[] = new Zend_Service_Amazon_EditorialReview($r);
  171. }
  172. }
  173. $result = $xpath->query('./az:SimilarProducts/az:*', $dom);
  174. if ($result->length >= 1) {
  175. /**
  176. * @see Zend_Service_Amazon_SimilarProduct
  177. */
  178. require_once 'Zend/Service/Amazon/SimilarProduct.php';
  179. foreach ($result as $r) {
  180. $this->SimilarProducts[] = new Zend_Service_Amazon_SimilarProduct($r);
  181. }
  182. }
  183. $result = $xpath->query('./az:ListmaniaLists/*', $dom);
  184. if ($result->length >= 1) {
  185. /**
  186. * @see Zend_Service_Amazon_ListmaniaList
  187. */
  188. require_once 'Zend/Service/Amazon/ListmaniaList.php';
  189. foreach ($result as $r) {
  190. $this->ListmaniaLists[] = new Zend_Service_Amazon_ListmaniaList($r);
  191. }
  192. }
  193. $result = $xpath->query('./az:Tracks/az:Disc', $dom);
  194. if ($result->length > 1) {
  195. foreach ($result as $disk) {
  196. foreach ($xpath->query('./*/text()', $disk) as $t) {
  197. // TODO: For consistency in a bugfix all tracks are appended to one single array
  198. // Erroreous line: $this->Tracks[$disk->getAttribute('number')] = (string) $t->data;
  199. $this->Tracks[] = (string) $t->data;
  200. }
  201. }
  202. } else if ($result->length == 1) {
  203. foreach ($xpath->query('./*/text()', $result->item(0)) as $t) {
  204. $this->Tracks[] = (string) $t->data;
  205. }
  206. }
  207. $result = $xpath->query('./az:Offers', $dom);
  208. $resultSummary = $xpath->query('./az:OfferSummary', $dom);
  209. if ($result->length > 1 || $resultSummary->length == 1) {
  210. /**
  211. * @see Zend_Service_Amazon_OfferSet
  212. */
  213. require_once 'Zend/Service/Amazon/OfferSet.php';
  214. $this->Offers = new Zend_Service_Amazon_OfferSet($dom);
  215. }
  216. $result = $xpath->query('./az:Accessories/*', $dom);
  217. if ($result->length > 1) {
  218. /**
  219. * @see Zend_Service_Amazon_Accessories
  220. */
  221. require_once 'Zend/Service/Amazon/Accessories.php';
  222. foreach ($result as $r) {
  223. $this->Accessories[] = new Zend_Service_Amazon_Accessories($r);
  224. }
  225. }
  226. $this->_dom = $dom;
  227. }
  228. /**
  229. * Returns the item's original XML
  230. *
  231. * @return string
  232. */
  233. public function asXml()
  234. {
  235. return $this->_dom->ownerDocument->saveXML($this->_dom);
  236. }
  237. }