|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace ApaiIO\ResponseTransformer; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
class ObjectToItem extends ObjectToArray implements ResponseTransformerInterface { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* @var type |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $data = array(); |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* |
|
22
|
|
|
* @var type |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $item = array(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* |
|
28
|
|
|
* @param type $response |
|
29
|
|
|
* @return type |
|
30
|
|
|
*/ |
|
31
|
|
|
public function transform($response) |
|
32
|
|
|
{ |
|
33
|
|
|
$response = parent::transform($response); |
|
34
|
|
|
|
|
35
|
|
|
if( !$this->get_item( $response ) ) |
|
36
|
|
|
{ |
|
37
|
|
|
return array(); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->set( 'asin', 'ASIN' ); |
|
|
|
|
|
|
41
|
|
|
$this->set( 'title', 'ItemAttributes', 'Title' ); |
|
|
|
|
|
|
42
|
|
|
$this->set( 'manufacturer', 'ItemAttributes', 'Manufacturer' ); |
|
|
|
|
|
|
43
|
|
|
$this->set( 'isbn', 'ItemAttributes', 'ISBN' ); |
|
|
|
|
|
|
44
|
|
|
$this->set( 'publisher', 'ItemAttributes', 'Publisher' ); |
|
|
|
|
|
|
45
|
|
|
$this->set( 'number_of_pages', 'ItemAttributes', 'NumberOfPages' ); |
|
|
|
|
|
|
46
|
|
|
$this->set( 'number_of_items', 'ItemAttributes', 'NumberOfItems' ); |
|
|
|
|
|
|
47
|
|
|
$this->set( 'number_of_issues', 'ItemAttributes', 'NumberOfIssues' ); |
|
|
|
|
|
|
48
|
|
|
$this->set( 'model', 'ItemAttributes', 'Model' ); |
|
|
|
|
|
|
49
|
|
|
$this->set( 'label', 'ItemAttributes', 'Label' ); |
|
|
|
|
|
|
50
|
|
|
$this->set( 'format', 'ItemAttributes', 'Format' ); |
|
|
|
|
|
|
51
|
|
|
$this->set( 'edition', 'ItemAttributes', 'Edition' ); |
|
|
|
|
|
|
52
|
|
|
$this->set( 'artist', 'ItemAttributes', 'Artist' ); |
|
|
|
|
|
|
53
|
|
|
$this->set( 'description', 'EditorialReviews', 'EditorialReview', 'Content' ); |
|
|
|
|
|
|
54
|
|
|
$this->set( 'lowest_new_price', 'OfferSummary', 'LowestNewPrice', 'Amount' ); |
|
|
|
|
|
|
55
|
|
|
$this->set( 'large_image', 'LargeImage', 'URL' ); |
|
|
|
|
|
|
56
|
|
|
$this->set( 'medium_image', 'MediumImage', 'URL' ); |
|
|
|
|
|
|
57
|
|
|
$this->set( 'small_image', 'SmallImage', 'URL' ); |
|
|
|
|
|
|
58
|
|
|
$this->set_array( 'author', 'ItemAttributes', 'Author' ); |
|
59
|
|
|
$this->set_array( 'features', 'ItemAttributes', 'Feature' ); |
|
60
|
|
|
|
|
61
|
|
|
$this->get_reviews(); |
|
62
|
|
|
$this->get_price(); |
|
63
|
|
|
$this->get_description(); |
|
64
|
|
|
$this->get_category(); |
|
65
|
|
|
$this->get_image_sets(); |
|
66
|
|
|
|
|
67
|
|
|
return $this->data; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* |
|
72
|
|
|
* @param type $response |
|
73
|
|
|
* @return mixed |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function get_item($response) |
|
76
|
|
|
{ |
|
77
|
|
|
if( isset( $response['Items']['Item'] ) AND is_array( $response['Items']['Item'] ) ) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
if( array_key_exists( 1, $response['Items']['Item'] ) ) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->item = $response['Items']['Item'][0]; |
|
82
|
|
|
} |
|
83
|
|
|
else |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->item = $response['Items']['Item']; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
else |
|
89
|
|
|
{ |
|
90
|
|
|
return FALSE; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* |
|
96
|
|
|
* @param type $data |
|
97
|
|
|
* @param type $key1 |
|
98
|
|
|
* @param type $key2 |
|
99
|
|
|
* @param type $key3 |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function set($data, $key1, $key2 = NULL, $key3 = NULL, $key4 = NULL) |
|
102
|
|
|
{ |
|
103
|
|
|
if( $key4 ) |
|
104
|
|
|
{ |
|
105
|
|
View Code Duplication |
if( isset( $this->item[$key1][$key2][$key3][$key4] ) ) |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$this->data[$data] = $this->item[$key1][$key2][$key3][$key4]; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
View Code Duplication |
elseif( $key3 ) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
if( isset( $this->item[$key1][$key2][$key3] ) ) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->data[$data] = $this->item[$key1][$key2][$key3]; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
elseif( $key2 ) |
|
118
|
|
|
{ |
|
119
|
|
|
if( isset( $this->item[$key1][$key2] ) ) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->data[$data] = $this->item[$key1][$key2]; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
else |
|
125
|
|
|
{ |
|
126
|
|
|
if( isset( $this->item[$key1] ) ) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->data[$data] = $this->item[$key1]; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
protected function set_array($data, $key1, $key2 = NULL, $key3 = NULL) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->set( $data, $key1, $key2, $key3 ); |
|
136
|
|
|
if( isset( $this->data[$data] ) AND !is_array( $this->data[$data] ) ) |
|
|
|
|
|
|
137
|
|
|
{ |
|
138
|
|
|
$this->data[$data] = array($this->data[$data]); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
private function get_price() |
|
143
|
|
|
{ |
|
144
|
|
|
$list_price = isset( $this->item['ItemAttributes']['ListPrice']['Amount'] ) ? $this->item['ItemAttributes']['ListPrice']['Amount'] : NULL; |
|
145
|
|
|
$amazon_price = isset( $this->item['Offers']['Offer']['OfferListing']['Price']['Amount'] ) ? $this->item['Offers']['Offer']['OfferListing']['Price']['Amount'] : NULL; |
|
146
|
|
|
$saved = isset( $this->item['Offers']['Offer']['OfferListing']['AmountSaved'] ) ? $this->item['Offers']['Offer']['OfferListing']['AmountSaved']['Amount'] : NULL; |
|
147
|
|
|
$price = ($list_price) ? $list_price : ($amazon_price ? ($amazon_price + $saved) : NULL ); |
|
148
|
|
|
$this->data['price'] = ($price) ? $price : (isset($this->data['lowest_new_price']) ? $this->data['lowest_new_price'] : 0); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
private function get_description() |
|
152
|
|
|
{ |
|
153
|
|
|
if(isset( $this->item['EditorialReviews']['EditorialReview'][1] ) ) |
|
154
|
|
|
{ |
|
155
|
|
|
$this->set( 'description', 'EditorialReviews', 'EditorialReview', 0, 'Content' ); |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
else |
|
158
|
|
|
{ |
|
159
|
|
|
$this->set( 'description', 'EditorialReviews', 'EditorialReview', 'Content' ); |
|
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
if( isset( $this->data['description'] ) ) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->data['description'] = $this->html2txt( $this->data['description'] ); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Parses the Amazon reviews iframe to get precise numeric review metrics |
|
169
|
|
|
*/ |
|
170
|
|
|
private function get_reviews() |
|
171
|
|
|
{ |
|
172
|
|
|
if( isset( $this->item['CustomerReviews']['IFrameURL'] ) ) |
|
173
|
|
|
{ |
|
174
|
|
|
// Load the iFrame HTML from the URL returned by the API |
|
175
|
|
|
$text = file_get_contents( $this->item['CustomerReviews']['IFrameURL'] ); |
|
176
|
|
|
|
|
177
|
|
|
// Clean up the HTML removing scripts and other unneeded data |
|
178
|
|
|
$text = preg_replace( '/(<style>.+?)+(<\/style>)/i', '', $text ); |
|
179
|
|
|
$search = array('@<script[^>]*?>.*?</script>@si', // javascript |
|
180
|
|
|
'@<style[^>]*?>.*?</style>@siU', // style tags properly |
|
181
|
|
|
'@<![\s\S]*?--[ \t\n\r]*>@', // multi-line comments including CDATA |
|
182
|
|
|
'/^\n+|^[\t\s]*\n+/m', // empty lines |
|
183
|
|
|
); |
|
184
|
|
|
$text = trim( preg_replace( $search, '', $text ) ); |
|
185
|
|
|
|
|
186
|
|
|
// Load HTML into SimpleXML for parsing |
|
187
|
|
|
libxml_use_internal_errors( TRUE ); |
|
188
|
|
|
$dom = new \DOMDocument(); |
|
189
|
|
|
$dom->strictErrorChecking = FALSE; |
|
190
|
|
|
$dom->recover = TRUE; |
|
191
|
|
|
$dom->loadHTML( $text ); |
|
192
|
|
|
$xml = simplexml_import_dom( $dom ); |
|
193
|
|
|
|
|
194
|
|
|
// Get the total amount of reviews |
|
195
|
|
|
$stars = $xml->xpath( "//span[@class='crAvgStars']" ); |
|
196
|
|
|
$total_reviews = isset( $stars[0] ) ? (int)preg_replace( '/[^\d]/', '', $stars[0]->a ) : 0; |
|
197
|
|
|
|
|
198
|
|
|
// Get the average rating |
|
199
|
|
|
$summary = $xml->xpath( "//span[@class='asinReviewsSummary']" ); |
|
200
|
|
|
if( isset($summary[0]) ) |
|
201
|
|
|
{ |
|
202
|
|
|
$img = $summary[0]->xpath('a/img'); |
|
203
|
|
|
$summary = preg_replace( '/[^\d]/', '', (string)$img[0]->attributes()->alt ); |
|
204
|
|
|
$summary = (float)((int)substr( $summary, 0, strlen($summary)-1 ))/10; |
|
205
|
|
|
} |
|
206
|
|
|
else |
|
207
|
|
|
{ |
|
208
|
|
|
$summary = 0; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
// Set data in response |
|
212
|
|
|
$this->data['reviews'] = array( |
|
213
|
|
|
'total' => $total_reviews, |
|
214
|
|
|
'average' => $summary, |
|
215
|
|
|
); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* |
|
221
|
|
|
*/ |
|
222
|
|
|
private function get_image_sets() |
|
223
|
|
|
{ |
|
224
|
|
|
if( isset( $this->item['ImageSets']['ImageSet'] ) AND is_array( $this->item['ImageSets']['ImageSet'] ) ) |
|
|
|
|
|
|
225
|
|
|
{ |
|
226
|
|
|
$this->data['image_sets'] = array(); |
|
227
|
|
|
|
|
228
|
|
|
$sets = $this->item['ImageSets']; |
|
229
|
|
|
|
|
230
|
|
View Code Duplication |
foreach( $sets as $set ) |
|
|
|
|
|
|
231
|
|
|
{ |
|
232
|
|
|
$row = array(); |
|
233
|
|
|
if( isset( $set['MediumImage']['URL'] ) ) |
|
234
|
|
|
{ |
|
235
|
|
|
$row['medium_image'] = $set['MediumImage']['URL']; |
|
236
|
|
|
} |
|
237
|
|
|
if( isset( $set['LargeImage']['URL'] ) ) |
|
238
|
|
|
{ |
|
239
|
|
|
$row['large_image'] = $set['LargeImage']['URL']; |
|
240
|
|
|
} |
|
241
|
|
|
$this->data['image_sets'][] = $row; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
private function get_category() |
|
247
|
|
|
{ |
|
248
|
|
|
if( isset( $this->item['BrowseNodes']['BrowseNode'] ) AND is_array( $this->item['BrowseNodes']['BrowseNode'] ) ) |
|
|
|
|
|
|
249
|
|
|
{ |
|
250
|
|
|
if( isset( $this->item['BrowseNodes']['BrowseNode'][0] ) ) |
|
251
|
|
|
{ |
|
252
|
|
|
$node = $this->item['BrowseNodes']['BrowseNode'][0]; |
|
253
|
|
|
} |
|
254
|
|
|
else |
|
255
|
|
|
{ |
|
256
|
|
|
$node = $this->item['BrowseNodes']['BrowseNode']; |
|
257
|
|
|
} |
|
258
|
|
|
$this->data['category'] = $this->get_ancestor( $node ); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
View Code Duplication |
private function get_ancestor($node) |
|
|
|
|
|
|
263
|
|
|
{ |
|
264
|
|
|
if( isset( $node['Ancestors'] ) AND is_array( $node['Ancestors'] ) ) |
|
|
|
|
|
|
265
|
|
|
{ |
|
266
|
|
|
return $this->get_ancestor( $node['Ancestors']['BrowseNode'] ); |
|
267
|
|
|
} |
|
268
|
|
|
else |
|
269
|
|
|
{ |
|
270
|
|
|
return isset( $node['Name'] ) ? $node['Name'] : 'Uncategorized'; |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
} |
|
275
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.