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