1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
*/ |
5
|
|
|
namespace ApaiIO\ResponseTransformer; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
class ObjectToItems extends ObjectToArray implements ResponseTransformerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* @var type |
15
|
|
|
*/ |
16
|
|
|
protected $data = array(); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @var type |
21
|
|
|
*/ |
22
|
|
|
protected $items = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @param type $response |
27
|
|
|
* @return type |
28
|
|
|
*/ |
29
|
|
|
public function transform($response) |
30
|
|
|
{ |
31
|
|
|
$response = parent::transform($response); |
32
|
|
|
|
33
|
|
|
if( ! $this->get_items( $response ) ) |
34
|
|
|
{ |
35
|
|
|
return array(); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
$c = count( $this->items ); |
38
|
|
|
for( $i=0; $i < $c; $i++ ) |
39
|
|
|
{ |
40
|
|
|
$this->set( $i, 'asin', 'ASIN' ); |
|
|
|
|
41
|
|
|
$this->set( $i, 'sales_rank', 'SalesRank' ); |
|
|
|
|
42
|
|
|
$this->set( $i, 'title', 'ItemAttributes', 'Title' ); |
|
|
|
|
43
|
|
|
$this->set( $i, 'url', 'DetailPageURL'); |
|
|
|
|
44
|
|
|
$this->set( $i, 'manufacturer', 'ItemAttributes', 'Manufacturer' ); |
|
|
|
|
45
|
|
|
$this->set( $i, 'isbn', 'ItemAttributes', 'ISBN' ); |
|
|
|
|
46
|
|
|
$this->set( $i, 'publisher', 'ItemAttributes', 'Publisher' ); |
|
|
|
|
47
|
|
|
$this->set( $i, 'number_of_pages', 'ItemAttributes', 'NumberOfPages' ); |
|
|
|
|
48
|
|
|
$this->set( $i, 'number_of_items', 'ItemAttributes', 'NumberOfItems' ); |
|
|
|
|
49
|
|
|
$this->set( $i, 'number_of_issues', 'ItemAttributes', 'NumberOfIssues' ); |
|
|
|
|
50
|
|
|
$this->set( $i, 'model', 'ItemAttributes', 'Model' ); |
|
|
|
|
51
|
|
|
$this->set( $i, 'label', 'ItemAttributes', 'Label' ); |
|
|
|
|
52
|
|
|
$this->set( $i, 'format', 'ItemAttributes', 'Format' ); |
|
|
|
|
53
|
|
|
$this->set( $i, 'edition', 'ItemAttributes', 'Edition' ); |
|
|
|
|
54
|
|
|
$this->set( $i, 'artist', 'ItemAttributes', 'Artist' ); |
|
|
|
|
55
|
|
|
$this->set( $i, 'description', 'EditorialReviews', 'EditorialReview', 'Content' ); |
|
|
|
|
56
|
|
|
$this->set( $i, 'lowest_new_price', 'OfferSummary', 'LowestNewPrice', 'Amount' ); |
|
|
|
|
57
|
|
|
$this->set( $i, 'large_image', 'LargeImage', 'URL' ); |
|
|
|
|
58
|
|
|
$this->set( $i, 'medium_image', 'MediumImage', 'URL' ); |
|
|
|
|
59
|
|
|
$this->set( $i, 'small_image', 'SmallImage', 'URL' ); |
|
|
|
|
60
|
|
|
$this->set( $i, 'reviews', 'CustomerReviews', 'IFrameURL' ); |
|
|
|
|
61
|
|
|
$this->set_array( $i, 'author', 'ItemAttributes', 'Author' ); |
62
|
|
|
$this->set_array( $i, 'features', 'ItemAttributes', 'Feature' ); |
63
|
|
|
|
64
|
|
|
$this->get_price($i); |
65
|
|
|
$this->get_description($i); |
|
|
|
|
66
|
|
|
$this->get_category($i); |
67
|
|
|
$this->get_image_sets($i); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* |
75
|
|
|
* @param type $response |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
protected function get_items($response) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
if( isset($response['Items']['Item']) AND is_array($response['Items']['Item']) ) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
return $this->items = $response['Items']['Item']; |
83
|
|
|
} |
84
|
|
|
else |
85
|
|
|
{ |
86
|
|
|
return FALSE; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
* @param type $data |
94
|
|
|
* @param type $key1 |
95
|
|
|
* @param type $key2 |
96
|
|
|
* @param type $key3 |
97
|
|
|
*/ |
98
|
|
|
protected function set($i, $data, $key1, $key2=NULL, $key3=NULL) |
99
|
|
|
{ |
100
|
|
|
if($key3) |
101
|
|
|
{ |
102
|
|
|
if( isset($this->items[$i][$key1][$key2][$key3]) ) |
103
|
|
|
{ |
104
|
|
|
$this->data[$i][$data] = $this->items[$i][$key1][$key2][$key3]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
elseif($key2) |
108
|
|
|
{ |
109
|
|
|
if( isset($this->items[$i][$key1][$key2]) ) |
110
|
|
|
{ |
111
|
|
|
$this->data[$i][$data] = $this->items[$i][$key1][$key2]; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
else |
115
|
|
|
{ |
116
|
|
|
if( isset($this->items[$i][$key1]) ) |
117
|
|
|
{ |
118
|
|
|
$this->data[$i][$data] = $this->items[$i][$key1]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function set_array($i, $data, $key1, $key2=NULL, $key3=NULL) |
124
|
|
|
{ |
125
|
|
|
$this->set($i, $data, $key1, $key2, $key3); |
126
|
|
|
if(isset($this->data[$i][$data]) AND !is_array( $this->data[$i][$data])) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$this->data[$i][$data] = array($this->data[$i][$data]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function get_price($i) |
133
|
|
|
{ |
134
|
|
|
$list_price = isset( $this->items[$i]['ItemAttributes']['ListPrice']['Amount'] ) ? $this->items[$i]['ItemAttributes']['ListPrice']['Amount'] : NULL; |
135
|
|
|
$amazon_price = isset( $this->items[$i]['Offers']['Offer']['OfferListing']['Price']['Amount'] ) ? $this->items[$i]['Offers']['Offer']['OfferListing']['Price']['Amount'] : NULL; |
136
|
|
|
$saved = isset( $this->items[$i]['Offers']['Offer']['OfferListing']['AmountSaved'] ) ? $this->items[$i]['Offers']['Offer']['OfferListing']['AmountSaved']['Amount'] : NULL; |
137
|
|
|
$price = ($list_price) ? $list_price : ($amazon_price ? ($amazon_price + $saved) : NULL ); |
138
|
|
|
$this->data[$i]['price'] = ($price) ? $price : (isset($this->data[$i]['lowest_new_price']) ? $this->data[$i]['lowest_new_price'] : NULL); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* |
143
|
|
|
* @param type $i |
144
|
|
|
*/ |
145
|
|
|
private function get_description($i) |
146
|
|
|
{ |
147
|
|
|
$this->set($i, 'description', 'EditorialReviews', 'EditorialReview', 'Content'); |
|
|
|
|
148
|
|
|
if(isset($this->data[$i]['description'])) |
149
|
|
|
{ |
150
|
|
|
$this->data[$i]['description'] = $this->html2txt( $this->data[$i]['description'] ); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* |
156
|
|
|
*/ |
157
|
|
|
private function get_image_sets($i) |
158
|
|
|
{ |
159
|
|
|
if( isset($this->items[$i]['ImageSets']['ImageSet']) |
160
|
|
|
AND is_array($this->items[$i]['ImageSets']['ImageSet']) ) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$this->data[$i]['image_sets'] = array(); |
163
|
|
|
|
164
|
|
|
$sets = $this->items[$i]['ImageSets']; |
165
|
|
|
|
166
|
|
View Code Duplication |
foreach( $sets as $set ) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$row = array(); |
169
|
|
|
if( isset($set['MediumImage']['URL']) ) |
170
|
|
|
{ |
171
|
|
|
$row['medium_image'] = $set['MediumImage']['URL']; |
172
|
|
|
} |
173
|
|
|
if( isset($set['LargeImage']['URL']) ) |
174
|
|
|
{ |
175
|
|
|
$row['large_image'] = $set['LargeImage']['URL']; |
176
|
|
|
} |
177
|
|
|
$this->data[$i]['image_sets'][] = $row; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
private function get_category($i) |
183
|
|
|
{ |
184
|
|
|
if( isset($this->items[$i]['BrowseNodes']['BrowseNode']) |
185
|
|
|
AND is_array($this->items[$i]['BrowseNodes']['BrowseNode']) ) |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
if( isset($this->items[$i]['BrowseNodes']['BrowseNode'][0]) ) |
188
|
|
|
{ |
189
|
|
|
$node = $this->items[$i]['BrowseNodes']['BrowseNode'][0]; |
190
|
|
|
} |
191
|
|
|
else |
192
|
|
|
{ |
193
|
|
|
$node = $this->items[$i]['BrowseNodes']['BrowseNode']; |
194
|
|
|
} |
195
|
|
|
$this->data[$i]['category'] = $this->get_ancestor( $node ); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
View Code Duplication |
private function get_ancestor($node) |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
if(isset($node['Ancestors']) AND is_array( $node['Ancestors'] )) |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
return $this->get_ancestor($node['Ancestors']['BrowseNode']); |
204
|
|
|
} |
205
|
|
|
else |
206
|
|
|
{ |
207
|
|
|
return $node['Name']; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.