1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Radowoj\Yaah; |
4
|
|
|
|
5
|
|
|
use Radowoj\Yaah\Constants\SellFormOpts; |
6
|
|
|
|
7
|
|
|
class Helper |
8
|
|
|
{ |
9
|
|
|
protected $client = null; |
10
|
|
|
|
11
|
16 |
|
public function __construct(Client $client) |
12
|
|
|
{ |
13
|
16 |
|
$this->client = $client; |
14
|
16 |
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get simplified fields list for given category |
18
|
|
|
* @param integer $idCategory id of category in question |
19
|
|
|
* @return array of fields metadata |
20
|
|
|
*/ |
21
|
2 |
|
public function getFieldsByCategory($idCategory) |
22
|
|
|
{ |
23
|
2 |
|
$data = $this->client->doGetSellFormFieldsForCategory(['categoryId' => $idCategory]); |
|
|
|
|
24
|
|
|
|
25
|
2 |
|
if (!isset($data->sellFormFieldsForCategory->sellFormFieldsList->item)) { |
26
|
1 |
|
throw new Exception('Invalid WebAPI response: ' . print_r($data, 1)); |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
$items = $data->sellFormFieldsForCategory->sellFormFieldsList->item; |
30
|
|
|
|
31
|
|
|
return array_map(function ($item) { |
32
|
|
|
return [ |
33
|
1 |
|
'fid' => $item->sellFormId, |
34
|
1 |
|
'title' => $item->sellFormTitle, |
35
|
1 |
|
'required' => ($item->sellFormOpt == SellFormOpts::SELL_FORM_OPT_REQUIRED), |
36
|
1 |
|
'options' => $item->sellFormOptsValues, |
37
|
1 |
|
'optionsDesc' => $item->sellFormDesc, |
38
|
1 |
|
]; |
39
|
1 |
|
}, $items); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create new auction |
44
|
|
|
* |
45
|
|
|
* @throws Exception on failure |
46
|
|
|
* |
47
|
|
|
* @param Auction $auction Auction to create |
48
|
|
|
* @param integer $localId - local item id, required by WebAPI |
49
|
|
|
* @return integer id of created auction |
50
|
|
|
*/ |
51
|
2 |
|
public function newAuction(AuctionInterface $auction, $localId) |
52
|
|
|
{ |
53
|
2 |
|
$auctionArray = $auction->toApiRepresentation(); |
54
|
2 |
|
$auctionArray['localId'] = $localId; |
55
|
|
|
|
56
|
2 |
|
$resultNewAuction = $this->client->doNewAuctionExt($auctionArray); |
|
|
|
|
57
|
2 |
|
$resultVerify = $this->client->doVerifyItem(['localId' => $localId]); |
|
|
|
|
58
|
|
|
|
59
|
2 |
|
if (!is_object($resultVerify) || !isset($resultVerify->itemId)) { |
60
|
1 |
|
throw new Exception("Auction has not been created: " . print_r($resultVerify, 1)); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
return $resultVerify->itemId; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Finish a single auction |
69
|
|
|
* @param integer $auctionId itemId of auction to finish |
70
|
|
|
* @param integer $cancelAllBids whether to cancel all bids |
71
|
|
|
* @param string $finishCancelReason reason |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
4 |
|
public function finishAuction($auctionId, $cancelAllBids = 0, $finishCancelReason = '') |
75
|
|
|
{ |
76
|
4 |
|
return $this->finishAuctions((array)($auctionId), $cancelAllBids, $finishCancelReason); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Finish a single auction |
82
|
|
|
* @param array $auctionIds itemIds of auctions to finish |
83
|
|
|
* @param integer $cancelAllBids whether to cancel all bids |
84
|
|
|
* @param string $finishCancelReason reason |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function finishAuctions(array $auctionIds, $cancelAllBids = 0, $finishCancelReason = '') |
88
|
|
|
{ |
89
|
8 |
|
$finishItemsList = array_map(function ($auctionId) use ($cancelAllBids, $finishCancelReason) { |
90
|
|
|
return [ |
91
|
8 |
|
'finishItemId' => $auctionId, |
92
|
8 |
|
'finishCancelAllBids' => $cancelAllBids, |
93
|
8 |
|
'finishCancelReason' => $finishCancelReason, |
94
|
8 |
|
]; |
95
|
8 |
|
}, $auctionIds); |
96
|
|
|
|
97
|
8 |
|
return $this->client->doFinishItems(['finishItemsList' => $finishItemsList]); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Change quantity of items available in auction |
102
|
|
|
* @param integer $auctionId itemId of auction to change quantity |
103
|
|
|
* @param integer $newQuantity new quantity to set |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
1 |
|
public function changeQuantity($auctionId, $newQuantity) |
107
|
|
|
{ |
108
|
1 |
|
return $this->client->doChangeQuantityItem([ |
|
|
|
|
109
|
1 |
|
'itemId' => $auctionId, |
110
|
|
|
'newItemQuantity' => $newQuantity |
111
|
1 |
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return site journal deals |
117
|
|
|
* @param integer $journalStart start point (dealEventId) |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function getSiteJournalDeals($journalStart) |
121
|
|
|
{ |
122
|
|
|
$response = $this->client->doGetSiteJournalDeals(['journalStart' => $journalStart]); |
|
|
|
|
123
|
|
|
if (isset($response->siteJournalDeals->item)) { |
124
|
|
|
return $response->siteJournalDeals->item; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
throw new Exception("Unable to get site journal deals: " . print_r($response, 1)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param integer $itemId id of auction to get |
133
|
|
|
* @return Auction | null |
134
|
|
|
*/ |
135
|
|
|
public function getAuctionByItemId($itemId) |
136
|
|
|
{ |
137
|
|
|
$response = $this->client->doGetItemFields(['itemId' => $itemId]); |
|
|
|
|
138
|
|
|
if (isset($response->itemFields->item)) { |
139
|
|
|
$auction = new Auction(); |
140
|
|
|
$auction->fromApiRepresentation($response->itemFields->item); |
141
|
|
|
return $auction; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Directly call WebAPI method (prefixed by "do") |
150
|
|
|
* @param string $name method name |
151
|
|
|
* @param [type] $args method arguments |
|
|
|
|
152
|
|
|
* @return [type] [description] |
|
|
|
|
153
|
|
|
*/ |
154
|
3 |
|
public function __call($name, $args) |
155
|
|
|
{ |
156
|
3 |
|
if (strpos($name, 'do') !== 0) { |
157
|
1 |
|
throw new Exception("Method {$name} is not implemented in " . get_class($this)); |
158
|
|
|
} |
159
|
|
|
|
160
|
2 |
|
return call_user_func_array([$this->client, $name], $args); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: