Item.php
8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Service
* @subpackage Amazon
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Item.php 23775 2011-03-01 17:25:24Z ralph $
*/
/**
* @category Zend
* @package Zend_Service
* @subpackage Amazon
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Service_Amazon_Item
{
/**
* @var string
*/
public $ASIN;
/**
* @var string
*/
public $DetailPageURL;
/**
* @var int
*/
public $SalesRank;
/**
* @var int
*/
public $TotalReviews;
/**
* @var int
*/
public $AverageRating;
/**
* @var string
*/
public $SmallImage;
/**
* @var string
*/
public $MediumImage;
/**
* @var string
*/
public $LargeImage;
/**
* @var string
*/
public $Subjects;
/**
* @var Zend_Service_Amazon_OfferSet
*/
public $Offers;
/**
* @var Zend_Service_Amazon_CustomerReview[]
*/
public $CustomerReviews = array();
/**
* @var Zend_Service_Amazon_SimilarProducts[]
*/
public $SimilarProducts = array();
/**
* @var Zend_Service_Amazon_Accessories[]
*/
public $Accessories = array();
/**
* @var array
*/
public $Tracks = array();
/**
* @var Zend_Service_Amazon_ListmaniaLists[]
*/
public $ListmaniaLists = array();
protected $_dom;
/**
* Parse the given <Item> element
*
* @param null|DOMElement $dom
* @return void
* @throws Zend_Service_Amazon_Exception
*
* @group ZF-9547
*/
public function __construct($dom)
{
if (null === $dom) {
require_once 'Zend/Service/Amazon/Exception.php';
throw new Zend_Service_Amazon_Exception('Item element is empty');
}
if (!$dom instanceof DOMElement) {
require_once 'Zend/Service/Amazon/Exception.php';
throw new Zend_Service_Amazon_Exception('Item is not a valid DOM element');
}
$xpath = new DOMXPath($dom->ownerDocument);
$xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
$this->ASIN = $xpath->query('./az:ASIN/text()', $dom)->item(0)->data;
$result = $xpath->query('./az:DetailPageURL/text()', $dom);
if ($result->length == 1) {
$this->DetailPageURL = $result->item(0)->data;
}
if ($xpath->query('./az:ItemAttributes/az:ListPrice', $dom)->length >= 1) {
$this->CurrencyCode = (string) $xpath->query('./az:ItemAttributes/az:ListPrice/az:CurrencyCode/text()', $dom)->item(0)->data;
$this->Amount = (int) $xpath->query('./az:ItemAttributes/az:ListPrice/az:Amount/text()', $dom)->item(0)->data;
$this->FormattedPrice = (string) $xpath->query('./az:ItemAttributes/az:ListPrice/az:FormattedPrice/text()', $dom)->item(0)->data;
}
$result = $xpath->query('./az:ItemAttributes/az:*/text()', $dom);
if ($result->length >= 1) {
foreach ($result as $v) {
if (isset($this->{$v->parentNode->tagName})) {
if (is_array($this->{$v->parentNode->tagName})) {
array_push($this->{$v->parentNode->tagName}, (string) $v->data);
} else {
$this->{$v->parentNode->tagName} = array($this->{$v->parentNode->tagName}, (string) $v->data);
}
} else {
$this->{$v->parentNode->tagName} = (string) $v->data;
}
}
}
foreach (array('SmallImage', 'MediumImage', 'LargeImage') as $im) {
$result = $xpath->query("./az:ImageSets/az:ImageSet[position() = 1]/az:$im", $dom);
if ($result->length == 1) {
/**
* @see Zend_Service_Amazon_Image
*/
require_once 'Zend/Service/Amazon/Image.php';
$this->$im = new Zend_Service_Amazon_Image($result->item(0));
}
}
$result = $xpath->query('./az:SalesRank/text()', $dom);
if ($result->length == 1) {
$this->SalesRank = (int) $result->item(0)->data;
}
$result = $xpath->query('./az:CustomerReviews/az:Review', $dom);
if ($result->length >= 1) {
/**
* @see Zend_Service_Amazon_CustomerReview
*/
require_once 'Zend/Service/Amazon/CustomerReview.php';
foreach ($result as $review) {
$this->CustomerReviews[] = new Zend_Service_Amazon_CustomerReview($review);
}
$this->AverageRating = (float) $xpath->query('./az:CustomerReviews/az:AverageRating/text()', $dom)->item(0)->data;
$this->TotalReviews = (int) $xpath->query('./az:CustomerReviews/az:TotalReviews/text()', $dom)->item(0)->data;
}
$result = $xpath->query('./az:EditorialReviews/az:*', $dom);
if ($result->length >= 1) {
/**
* @see Zend_Service_Amazon_EditorialReview
*/
require_once 'Zend/Service/Amazon/EditorialReview.php';
foreach ($result as $r) {
$this->EditorialReviews[] = new Zend_Service_Amazon_EditorialReview($r);
}
}
$result = $xpath->query('./az:SimilarProducts/az:*', $dom);
if ($result->length >= 1) {
/**
* @see Zend_Service_Amazon_SimilarProduct
*/
require_once 'Zend/Service/Amazon/SimilarProduct.php';
foreach ($result as $r) {
$this->SimilarProducts[] = new Zend_Service_Amazon_SimilarProduct($r);
}
}
$result = $xpath->query('./az:ListmaniaLists/*', $dom);
if ($result->length >= 1) {
/**
* @see Zend_Service_Amazon_ListmaniaList
*/
require_once 'Zend/Service/Amazon/ListmaniaList.php';
foreach ($result as $r) {
$this->ListmaniaLists[] = new Zend_Service_Amazon_ListmaniaList($r);
}
}
$result = $xpath->query('./az:Tracks/az:Disc', $dom);
if ($result->length > 1) {
foreach ($result as $disk) {
foreach ($xpath->query('./*/text()', $disk) as $t) {
// TODO: For consistency in a bugfix all tracks are appended to one single array
// Erroreous line: $this->Tracks[$disk->getAttribute('number')] = (string) $t->data;
$this->Tracks[] = (string) $t->data;
}
}
} else if ($result->length == 1) {
foreach ($xpath->query('./*/text()', $result->item(0)) as $t) {
$this->Tracks[] = (string) $t->data;
}
}
$result = $xpath->query('./az:Offers', $dom);
$resultSummary = $xpath->query('./az:OfferSummary', $dom);
if ($result->length > 1 || $resultSummary->length == 1) {
/**
* @see Zend_Service_Amazon_OfferSet
*/
require_once 'Zend/Service/Amazon/OfferSet.php';
$this->Offers = new Zend_Service_Amazon_OfferSet($dom);
}
$result = $xpath->query('./az:Accessories/*', $dom);
if ($result->length > 1) {
/**
* @see Zend_Service_Amazon_Accessories
*/
require_once 'Zend/Service/Amazon/Accessories.php';
foreach ($result as $r) {
$this->Accessories[] = new Zend_Service_Amazon_Accessories($r);
}
}
$this->_dom = $dom;
}
/**
* Returns the item's original XML
*
* @return string
*/
public function asXml()
{
return $this->_dom->ownerDocument->saveXML($this->_dom);
}
}