1
|
|
|
<?php |
2
|
|
|
namespace ApaiIO\ResponseTransformer; |
3
|
|
|
|
4
|
|
|
class ObjectToPreview extends ObjectToArray implements ResponseTransformerInterface |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
protected $data = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $items = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $categories = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @param string $response |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function transform($response) |
30
|
|
|
{ |
31
|
|
|
$response = parent::transform($response); |
32
|
|
|
|
33
|
|
|
if (!$this->getItems($response)) { |
34
|
|
|
return []; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->getCategories($response); |
38
|
|
|
|
39
|
|
|
$c = count($this->items); |
40
|
|
|
for ($i = 0; $i < $c; $i++) { |
41
|
|
|
$this->set($i, 'asin', 'ASIN'); |
42
|
|
|
$this->set($i, 'category', 'BrowseNodes', 'BrowseNode', 0, 'Name'); |
43
|
|
|
$this->set($i, 'sales_rank', 'SalesRank'); |
44
|
|
|
$this->set($i, 'title', 'ItemAttributes', 'Title'); |
45
|
|
|
$this->set($i, 'url', 'DetailPageURL'); |
46
|
|
|
$this->set($i, 'manufacturer', 'ItemAttributes', 'Manufacturer'); |
47
|
|
|
$this->set($i, 'large_image', 'LargeImage', 'URL'); |
48
|
|
|
$this->set($i, 'medium_image', 'MediumImage', 'URL'); |
49
|
|
|
$this->set($i, 'small_image', 'SmallImage', 'URL'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return [ |
53
|
|
|
'items' => $this->data, |
54
|
|
|
'categories' => $this->categories |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
* @param type $response |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
protected function getItems($response) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
if (isset($response['Items']['Item']) AND is_array($response['Items']['Item'])) { |
|
|
|
|
66
|
|
|
return $this->items = $response['Items']['Item']; |
67
|
|
|
} else { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @param array $response |
75
|
|
|
*/ |
76
|
|
|
protected function getCategories($response) |
77
|
|
|
{ |
78
|
|
|
if (isset($response['Items']['SearchBinSets']['SearchBinSet']['Bin'])) { |
79
|
|
|
foreach ($response['Items']['SearchBinSets']['SearchBinSet']['Bin'] as $bin) { |
80
|
|
|
if (isset($bin['BinParameter']['Name']) && $bin['BinParameter']['Name'] == 'SearchIndex') { |
81
|
|
|
$this->categories[] = $bin['BinParameter']['Value']; |
82
|
|
|
} |
83
|
|
|
if (count($this->categories) >= 10) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* |
92
|
|
|
* @param int $i |
93
|
|
|
* @param string $data |
94
|
|
|
* @param string $key1 |
95
|
|
|
* @param string $key2 |
96
|
|
|
* @param string $key3 |
97
|
|
|
* @param string $key4 |
98
|
|
|
*/ |
99
|
|
|
protected function set($i, $data, $key1, $key2 = null, $key3 = null, $key4 = null) |
100
|
|
|
{ |
101
|
|
|
if ($key4 !== null) { |
102
|
|
View Code Duplication |
if (isset($this->items[$i][$key1][$key2][$key3][$key4])) { |
|
|
|
|
103
|
|
|
$this->data[$i][$data] = $this->items[$i][$key1][$key2][$key3][$key4]; |
104
|
|
|
} |
105
|
|
View Code Duplication |
} elseif ($key3 !== null) { |
|
|
|
|
106
|
|
|
if (isset($this->items[$i][$key1][$key2][$key3])) { |
107
|
|
|
$this->data[$i][$data] = $this->items[$i][$key1][$key2][$key3]; |
108
|
|
|
} |
109
|
|
|
} elseif ($key2 !== null) { |
110
|
|
|
if (isset($this->items[$i][$key1][$key2])) { |
111
|
|
|
$this->data[$i][$data] = $this->items[$i][$key1][$key2]; |
112
|
|
|
} |
113
|
|
|
} else { |
114
|
|
|
if (isset($this->items[$i][$key1])) { |
115
|
|
|
$this->data[$i][$data] = $this->items[$i][$key1]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.