ARCANEDEV /
Stripe
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php namespace Arcanedev\Stripe; |
||
| 2 | |||
| 3 | use Arcanedev\Stripe\Contracts\CollectionInterface; |
||
| 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 CollectionInterface |
||
| 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 | 230 | public function setRequestParams($params) |
|
| 39 | { |
||
| 40 | 230 | $this->requestParams = $params; |
|
| 41 | 230 | } |
|
| 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 | 65 | public function all($params = [], $options = null) |
|
| 58 | { |
||
| 59 | 65 | 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 | 70 | public function create($params = [], $options = null) |
|
| 73 | { |
||
| 74 | 70 | 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 | 25 | public function retrieve($id, $params = [], $options = null) |
|
| 89 | { |
||
| 90 | 25 | list($url, $params) = $this->extractPathAndUpdateParams($params); |
|
| 91 | 25 | $extn = urlencode(str_utf8($id)); |
|
| 92 | 25 | list($response, $opts) = $this->request('get', "$url/$extn", $params); |
|
| 93 | |||
| 94 | 25 | $this->setRequestParams($params); |
|
| 95 | |||
| 96 | 25 | return Util::convertToStripeObject($response, $opts); |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Make a request and convert to Stripe object. |
||
| 101 | * |
||
| 102 | * @param string $method |
||
| 103 | * @param array $params |
||
| 104 | * @param array|string|null $options |
||
| 105 | * |
||
| 106 | * @return self|\Arcanedev\Stripe\StripeObject|\Arcanedev\Stripe\StripeResource|array| |
||
| 107 | */ |
||
| 108 | 100 | private function requestAndConvertToStripeObject($method, $params, $options) |
|
| 109 | { |
||
| 110 | 100 | list($url, $params) = $this->extractPathAndUpdateParams($params); |
|
| 111 | 95 | list($response, $opts) = $this->request($method, $url, $params, $options); |
|
| 112 | |||
| 113 | 95 | $this->setRequestParams($params); |
|
| 114 | |||
| 115 | 95 | return Util::convertToStripeObject($response, $opts); |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get An iterator that can be used to iterate across all objects across all pages. |
||
| 120 | * As page boundaries are encountered, the next page will be fetched automatically |
||
| 121 | * for continued iteration. |
||
| 122 | * |
||
| 123 | * @return \Arcanedev\Stripe\Utilities\AutoPagingIterator |
||
| 124 | */ |
||
| 125 | 10 | public function autoPagingIterator() |
|
| 126 | { |
||
| 127 | 10 | return AutoPagingIterator::make($this, $this->requestParams); |
|
| 128 | } |
||
| 129 | |||
| 130 | /* ------------------------------------------------------------------------------------------------ |
||
| 131 | | Check Functions |
||
| 132 | | ------------------------------------------------------------------------------------------------ |
||
| 133 | */ |
||
| 134 | /** |
||
| 135 | * Check if Object is list. |
||
| 136 | * |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | 75 | public function isList() |
|
| 140 | { |
||
| 141 | 75 | return $this->object === 'list'; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Check if URL has path. |
||
| 146 | * |
||
| 147 | * @param array $url |
||
| 148 | * |
||
| 149 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 150 | */ |
||
| 151 | 100 | private function checkPath(array $url) |
|
| 152 | { |
||
| 153 | 100 | if ( ! isset($url['path']) || empty($url['path'])) { |
|
| 154 | 5 | throw new ApiException( |
|
| 155 | 5 | 'Could not parse list url into parts: ' . $this->url |
|
| 156 | 4 | ); |
|
| 157 | } |
||
| 158 | 95 | } |
|
| 159 | |||
| 160 | /* ------------------------------------------------------------------------------------------------ |
||
| 161 | | Other Functions |
||
| 162 | | ------------------------------------------------------------------------------------------------ |
||
| 163 | */ |
||
| 164 | /** |
||
| 165 | * Get items Count. |
||
| 166 | * |
||
| 167 | * @return int |
||
| 168 | */ |
||
| 169 | 25 | public function count() |
|
| 170 | { |
||
| 171 | 25 | return ($this->isList() && isset($this->total_count)) |
|
| 172 | 25 | ? $this->total_count |
|
| 173 | 25 | : 0; |
|
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Extract Path And Update Parameters. |
||
| 178 | * |
||
| 179 | * @param array|null $params |
||
| 180 | * |
||
| 181 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | 100 | private function extractPathAndUpdateParams($params) |
|
| 186 | { |
||
| 187 | 100 | $url = parse_url($this->url); |
|
| 188 | |||
| 189 | 100 | if ($url === false) { |
|
| 190 | $url = []; |
||
| 191 | } |
||
| 192 | |||
| 193 | 100 | $this->checkPath($url); |
|
| 194 | |||
| 195 | 95 | if (isset($url['query'])) { |
|
| 196 | // If the URL contains a query param, parse it out into $params so they |
||
| 197 | // don't interact weirdly with each other. |
||
| 198 | 5 | $query = []; |
|
| 199 | 5 | parse_str($url['query'], $query); |
|
| 200 | |||
| 201 | 5 | $params = array_merge($params ?: [], $query); |
|
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 202 | 4 | } |
|
| 203 | |||
| 204 | 95 | return [$url['path'], $params]; |
|
| 205 | } |
||
| 206 | } |
||
| 207 |