Item.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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-2010 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-2010 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 DOMElement $dom
  96. * @return void
  97. */
  98. public function __construct(DOMElement $dom)
  99. {
  100. $xpath = new DOMXPath($dom->ownerDocument);
  101. $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
  102. $this->ASIN = $xpath->query('./az:ASIN/text()', $dom)->item(0)->data;
  103. $result = $xpath->query('./az:DetailPageURL/text()', $dom);
  104. if ($result->length == 1) {
  105. $this->DetailPageURL = $result->item(0)->data;
  106. }
  107. if ($xpath->query('./az:ItemAttributes/az:ListPrice', $dom)->length >= 1) {
  108. $this->CurrencyCode = (string) $xpath->query('./az:ItemAttributes/az:ListPrice/az:CurrencyCode/text()', $dom)->item(0)->data;
  109. $this->Amount = (int) $xpath->query('./az:ItemAttributes/az:ListPrice/az:Amount/text()', $dom)->item(0)->data;
  110. $this->FormattedPrice = (string) $xpath->query('./az:ItemAttributes/az:ListPrice/az:FormattedPrice/text()', $dom)->item(0)->data;
  111. }
  112. $result = $xpath->query('./az:ItemAttributes/az:*/text()', $dom);
  113. if ($result->length >= 1) {
  114. foreach ($result as $v) {
  115. if (isset($this->{$v->parentNode->tagName})) {
  116. if (is_array($this->{$v->parentNode->tagName})) {
  117. array_push($this->{$v->parentNode->tagName}, (string) $v->data);
  118. } else {
  119. $this->{$v->parentNode->tagName} = array($this->{$v->parentNode->tagName}, (string) $v->data);
  120. }
  121. } else {
  122. $this->{$v->parentNode->tagName} = (string) $v->data;
  123. }
  124. }
  125. }
  126. foreach (array('SmallImage', 'MediumImage', 'LargeImage') as $im) {
  127. $result = $xpath->query("./az:ImageSets/az:ImageSet[position() = 1]/az:$im", $dom);
  128. if ($result->length == 1) {
  129. /**
  130. * @see Zend_Service_Amazon_Image
  131. */
  132. require_once 'Zend/Service/Amazon/Image.php';
  133. $this->$im = new Zend_Service_Amazon_Image($result->item(0));
  134. }
  135. }
  136. $result = $xpath->query('./az:SalesRank/text()', $dom);
  137. if ($result->length == 1) {
  138. $this->SalesRank = (int) $result->item(0)->data;
  139. }
  140. $result = $xpath->query('./az:CustomerReviews/az:Review', $dom);
  141. if ($result->length >= 1) {
  142. /**
  143. * @see Zend_Service_Amazon_CustomerReview
  144. */
  145. require_once 'Zend/Service/Amazon/CustomerReview.php';
  146. foreach ($result as $review) {
  147. $this->CustomerReviews[] = new Zend_Service_Amazon_CustomerReview($review);
  148. }
  149. $this->AverageRating = (float) $xpath->query('./az:CustomerReviews/az:AverageRating/text()', $dom)->item(0)->data;
  150. $this->TotalReviews = (int) $xpath->query('./az:CustomerReviews/az:TotalReviews/text()', $dom)->item(0)->data;
  151. }
  152. $result = $xpath->query('./az:EditorialReviews/az:*', $dom);
  153. if ($result->length >= 1) {
  154. /**
  155. * @see Zend_Service_Amazon_EditorialReview
  156. */
  157. require_once 'Zend/Service/Amazon/EditorialReview.php';
  158. foreach ($result as $r) {
  159. $this->EditorialReviews[] = new Zend_Service_Amazon_EditorialReview($r);
  160. }
  161. }
  162. $result = $xpath->query('./az:SimilarProducts/az:*', $dom);
  163. if ($result->length >= 1) {
  164. /**
  165. * @see Zend_Service_Amazon_SimilarProduct
  166. */
  167. require_once 'Zend/Service/Amazon/SimilarProduct.php';
  168. foreach ($result as $r) {
  169. $this->SimilarProducts[] = new Zend_Service_Amazon_SimilarProduct($r);
  170. }
  171. }
  172. $result = $xpath->query('./az:ListmaniaLists/*', $dom);
  173. if ($result->length >= 1) {
  174. /**
  175. * @see Zend_Service_Amazon_ListmaniaList
  176. */
  177. require_once 'Zend/Service/Amazon/ListmaniaList.php';
  178. foreach ($result as $r) {
  179. $this->ListmaniaLists[] = new Zend_Service_Amazon_ListmaniaList($r);
  180. }
  181. }
  182. $result = $xpath->query('./az:Tracks/az:Disc', $dom);
  183. if ($result->length > 1) {
  184. foreach ($result as $disk) {
  185. foreach ($xpath->query('./*/text()', $disk) as $t) {
  186. // TODO: For consistency in a bugfix all tracks are appended to one single array
  187. // Erroreous line: $this->Tracks[$disk->getAttribute('number')] = (string) $t->data;
  188. $this->Tracks[] = (string) $t->data;
  189. }
  190. }
  191. } else if ($result->length == 1) {
  192. foreach ($xpath->query('./*/text()', $result->item(0)) as $t) {
  193. $this->Tracks[] = (string) $t->data;
  194. }
  195. }
  196. $result = $xpath->query('./az:Offers', $dom);
  197. $resultSummary = $xpath->query('./az:OfferSummary', $dom);
  198. if ($result->length > 1 || $resultSummary->length == 1) {
  199. /**
  200. * @see Zend_Service_Amazon_OfferSet
  201. */
  202. require_once 'Zend/Service/Amazon/OfferSet.php';
  203. $this->Offers = new Zend_Service_Amazon_OfferSet($dom);
  204. }
  205. $result = $xpath->query('./az:Accessories/*', $dom);
  206. if ($result->length > 1) {
  207. /**
  208. * @see Zend_Service_Amazon_Accessories
  209. */
  210. require_once 'Zend/Service/Amazon/Accessories.php';
  211. foreach ($result as $r) {
  212. $this->Accessories[] = new Zend_Service_Amazon_Accessories($r);
  213. }
  214. }
  215. $this->_dom = $dom;
  216. }
  217. /**
  218. * Returns the item's original XML
  219. *
  220. * @return string
  221. */
  222. public function asXml()
  223. {
  224. return $this->_dom->ownerDocument->saveXML($this->_dom);
  225. }
  226. }