1
|
|
|
<?php namespace Arcanedev\Stripe; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Stripe\Contracts\Collection as CollectionContract; |
4
|
|
|
use Arcanedev\Stripe\Exceptions\ApiException; |
5
|
|
|
use Arcanedev\Stripe\Utilities\AutoPagingIterator; |
6
|
|
|
use Arcanedev\Stripe\Utilities\Util; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Collection |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\Stripe |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
* |
14
|
|
|
* @property string object |
15
|
|
|
* @property array data |
16
|
|
|
* @property int total_count |
17
|
|
|
* @property bool has_more |
18
|
|
|
* @property string url |
19
|
|
|
*/ |
20
|
|
|
class Collection extends StripeResource implements CollectionContract |
21
|
|
|
{ |
22
|
|
|
/* ------------------------------------------------------------------------------------------------ |
23
|
|
|
| Properties |
24
|
|
|
| ------------------------------------------------------------------------------------------------ |
25
|
|
|
*/ |
26
|
|
|
/** @var array */ |
27
|
|
|
protected $requestParams = []; |
28
|
|
|
|
29
|
|
|
/* ------------------------------------------------------------------------------------------------ |
30
|
|
|
| Getters & Setters |
31
|
|
|
| ------------------------------------------------------------------------------------------------ |
32
|
|
|
*/ |
33
|
|
|
/** |
34
|
|
|
* Set the request parameters. |
35
|
|
|
* |
36
|
|
|
* @param array $params |
37
|
|
|
*/ |
38
|
98 |
|
public function setRequestParams($params) |
39
|
|
|
{ |
40
|
98 |
|
$this->requestParams = $params; |
41
|
98 |
|
} |
42
|
|
|
|
43
|
|
|
/* ------------------------------------------------------------------------------------------------ |
44
|
|
|
| CRUD Functions |
45
|
|
|
| ------------------------------------------------------------------------------------------------ |
46
|
|
|
*/ |
47
|
|
|
/** |
48
|
|
|
* List Function. |
49
|
|
|
* |
50
|
|
|
* @param array $params |
51
|
|
|
* @param array|string|null $options |
52
|
|
|
* |
53
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiException |
54
|
|
|
* |
55
|
|
|
* @return self|array |
56
|
|
|
*/ |
57
|
32 |
|
public function all($params = [], $options = null) |
58
|
|
|
{ |
59
|
32 |
|
return $this->requestAndConvertToStripeObject('get', $params, $options); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create Function. |
64
|
|
|
* |
65
|
|
|
* @param array $params |
66
|
|
|
* @param array|string|null $options |
67
|
|
|
* |
68
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiException |
69
|
|
|
* |
70
|
|
|
* @return \Arcanedev\Stripe\StripeObject|\Arcanedev\Stripe\StripeResource|array |
71
|
|
|
*/ |
72
|
32 |
|
public function create($params = [], $options = null) |
73
|
|
|
{ |
74
|
32 |
|
return $this->requestAndConvertToStripeObject('post', $params, $options); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Retrieve Function. |
79
|
|
|
* |
80
|
|
|
* @param string $id |
81
|
|
|
* @param array $params |
82
|
|
|
* @param array|string|null $options |
83
|
|
|
* |
84
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiException |
85
|
|
|
* |
86
|
|
|
* @return \Arcanedev\Stripe\StripeObject|\Arcanedev\Stripe\StripeResource|array |
87
|
|
|
*/ |
88
|
12 |
|
public function retrieve($id, $params = [], $options = null) |
89
|
|
|
{ |
90
|
12 |
|
list($url, $params) = $this->extractPathAndUpdateParams($params); |
91
|
12 |
|
list($response, $opts) = $this->request('get', $url.'/'.urlencode(str_utf8($id)), $params); |
92
|
|
|
|
93
|
12 |
|
$this->setRequestParams($params); |
94
|
|
|
|
95
|
12 |
|
return Util::convertToStripeObject($response, $opts); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Make a request and convert to Stripe object. |
100
|
|
|
* |
101
|
|
|
* @param string $method |
102
|
|
|
* @param array $params |
103
|
|
|
* @param array|string|null $options |
104
|
|
|
* |
105
|
|
|
* @return self|\Arcanedev\Stripe\StripeObject|\Arcanedev\Stripe\StripeResource|array| |
106
|
|
|
*/ |
107
|
46 |
|
private function requestAndConvertToStripeObject($method, $params, $options) |
108
|
|
|
{ |
109
|
46 |
|
list($url, $params) = $this->extractPathAndUpdateParams($params); |
110
|
44 |
|
list($response, $opts) = $this->request($method, $url, $params, $options); |
111
|
|
|
|
112
|
44 |
|
$this->setRequestParams($params); |
113
|
|
|
|
114
|
44 |
|
return Util::convertToStripeObject($response, $opts); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get An iterator that can be used to iterate across all objects across all pages. |
119
|
|
|
* As page boundaries are encountered, the next page will be fetched automatically |
120
|
|
|
* for continued iteration. |
121
|
|
|
* |
122
|
|
|
* @return \Arcanedev\Stripe\Utilities\AutoPagingIterator |
123
|
|
|
*/ |
124
|
6 |
|
public function autoPagingIterator() |
125
|
|
|
{ |
126
|
6 |
|
return AutoPagingIterator::make($this, $this->requestParams); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/* ------------------------------------------------------------------------------------------------ |
130
|
|
|
| Check Functions |
131
|
|
|
| ------------------------------------------------------------------------------------------------ |
132
|
|
|
*/ |
133
|
|
|
/** |
134
|
|
|
* Check if Object is list. |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
28 |
|
public function isList() |
139
|
|
|
{ |
140
|
28 |
|
return $this->object === 'list'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Check if URL has path. |
145
|
|
|
* |
146
|
|
|
* @param array $url |
147
|
|
|
* |
148
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiException |
149
|
|
|
*/ |
150
|
46 |
|
private function checkPath(array $url) |
151
|
|
|
{ |
152
|
46 |
|
if ( ! isset($url['path']) || empty($url['path'])) |
153
|
2 |
|
throw new ApiException("Could not parse list url into parts: {$this->url}"); |
154
|
44 |
|
} |
155
|
|
|
|
156
|
|
|
/* ------------------------------------------------------------------------------------------------ |
157
|
|
|
| Other Functions |
158
|
|
|
| ------------------------------------------------------------------------------------------------ |
159
|
|
|
*/ |
160
|
|
|
/** |
161
|
|
|
* Get items count. |
162
|
|
|
* |
163
|
|
|
* @return int |
164
|
|
|
*/ |
165
|
10 |
|
public function count() |
166
|
|
|
{ |
167
|
10 |
|
return ($this->isList() && isset($this->total_count)) ? $this->total_count : 0; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Extract Path And Update Parameters. |
172
|
|
|
* |
173
|
|
|
* @param array|null $params |
174
|
|
|
* |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
46 |
|
private function extractPathAndUpdateParams($params) |
178
|
|
|
{ |
179
|
46 |
|
$url = parse_url($this->url); |
180
|
|
|
|
181
|
46 |
|
if ($url === false) |
182
|
|
|
$url = []; |
183
|
|
|
|
184
|
46 |
|
$this->checkPath($url); |
185
|
|
|
|
186
|
44 |
|
if (isset($url['query'])) { |
187
|
|
|
// If the URL contains a query param, parse it out into $params so they |
188
|
|
|
// don't interact weirdly with each other. |
189
|
2 |
|
$query = []; |
190
|
2 |
|
parse_str($url['query'], $query); |
191
|
|
|
|
192
|
2 |
|
$params = array_merge($params ?: [], $query); |
193
|
|
|
} |
194
|
|
|
|
195
|
44 |
|
return [$url['path'], $params]; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|