1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
*/ |
5
|
|
|
namespace ApaiIO\ResponseTransformer; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
class ObjectToPreview 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, 'large_image', 'LargeImage', 'URL' ); |
|
|
|
|
46
|
|
|
$this->set( $i, 'medium_image', 'MediumImage', 'URL' ); |
|
|
|
|
47
|
|
|
$this->set( $i, 'small_image', 'SmallImage', 'URL' ); |
|
|
|
|
48
|
|
|
$this->get_price($i); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->data; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @param type $response |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
protected function get_items($response) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
if( isset($response['Items']['Item']) AND is_array($response['Items']['Item']) ) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
return $this->items = $response['Items']['Item']; |
64
|
|
|
} |
65
|
|
|
else |
66
|
|
|
{ |
67
|
|
|
return FALSE; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @param type $data |
75
|
|
|
* @param type $key1 |
76
|
|
|
* @param type $key2 |
77
|
|
|
* @param type $key3 |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
protected function set($i, $data, $key1, $key2=NULL, $key3=NULL) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
if($key3) |
82
|
|
|
{ |
83
|
|
|
if( isset($this->items[$i][$key1][$key2][$key3]) ) |
84
|
|
|
{ |
85
|
|
|
$this->data[$i][$data] = $this->items[$i][$key1][$key2][$key3]; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
elseif($key2) |
89
|
|
|
{ |
90
|
|
|
if( isset($this->items[$i][$key1][$key2]) ) |
91
|
|
|
{ |
92
|
|
|
$this->data[$i][$data] = $this->items[$i][$key1][$key2]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
else |
96
|
|
|
{ |
97
|
|
|
if( isset($this->items[$i][$key1]) ) |
98
|
|
|
{ |
99
|
|
|
$this->data[$i][$data] = $this->items[$i][$key1]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
protected function set_array($i, $data, $key1, $key2=NULL, $key3=NULL) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$this->set($i, $data, $key1, $key2, $key3); |
107
|
|
|
if(isset($this->data[$i][$data]) AND !is_array( $this->data[$i][$data])) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$this->data[$i][$data] = array($this->data[$i][$data]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
private function get_price($i) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$list_price = isset( $this->items[$i]['ItemAttributes']['ListPrice']['Amount'] ) ? $this->items[$i]['ItemAttributes']['ListPrice']['Amount'] : NULL; |
116
|
|
|
$amazon_price = isset( $this->items[$i]['Offers']['Offer']['OfferListing']['Price']['Amount'] ) ? $this->items[$i]['Offers']['Offer']['OfferListing']['Price']['Amount'] : NULL; |
117
|
|
|
$saved = isset( $this->items[$i]['Offers']['Offer']['OfferListing']['AmountSaved'] ) ? $this->items[$i]['Offers']['Offer']['OfferListing']['AmountSaved']['Amount'] : NULL; |
118
|
|
|
$price = ($list_price) ? $list_price : ($amazon_price ? ($amazon_price + $saved) : NULL ); |
119
|
|
|
$this->data[$i]['price'] = ($price) ? $price : (isset($this->data[$i]['lowest_new_price']) ? $this->data[$i]['lowest_new_price'] : NULL); |
120
|
|
|
} |
121
|
|
|
} |
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.