Collection.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Stripe;
  3. /**
  4. * Class Collection
  5. *
  6. * @property string $object
  7. * @property string $url
  8. * @property bool $has_more
  9. * @property mixed $data
  10. *
  11. * @package Stripe
  12. */
  13. class Collection extends StripeObject
  14. {
  15. const OBJECT_NAME = "list";
  16. use ApiOperations\Request;
  17. protected $_requestParams = [];
  18. /**
  19. * @return string The base URL for the given class.
  20. */
  21. public static function baseUrl()
  22. {
  23. return Stripe::$apiBase;
  24. }
  25. public function setRequestParams($params)
  26. {
  27. $this->_requestParams = $params;
  28. }
  29. public function all($params = null, $opts = null)
  30. {
  31. list($url, $params) = $this->extractPathAndUpdateParams($params);
  32. list($response, $opts) = $this->_request('get', $url, $params, $opts);
  33. $this->_requestParams = $params;
  34. return Util\Util::convertToStripeObject($response, $opts);
  35. }
  36. public function create($params = null, $opts = null)
  37. {
  38. list($url, $params) = $this->extractPathAndUpdateParams($params);
  39. list($response, $opts) = $this->_request('post', $url, $params, $opts);
  40. $this->_requestParams = $params;
  41. return Util\Util::convertToStripeObject($response, $opts);
  42. }
  43. public function retrieve($id, $params = null, $opts = null)
  44. {
  45. list($url, $params) = $this->extractPathAndUpdateParams($params);
  46. $id = Util\Util::utf8($id);
  47. $extn = urlencode($id);
  48. list($response, $opts) = $this->_request(
  49. 'get',
  50. "$url/$extn",
  51. $params,
  52. $opts
  53. );
  54. $this->_requestParams = $params;
  55. return Util\Util::convertToStripeObject($response, $opts);
  56. }
  57. /**
  58. * @return Util\AutoPagingIterator An iterator that can be used to iterate
  59. * across all objects across all pages. As page boundaries are
  60. * encountered, the next page will be fetched automatically for
  61. * continued iteration.
  62. */
  63. public function autoPagingIterator()
  64. {
  65. return new Util\AutoPagingIterator($this, $this->_requestParams);
  66. }
  67. private function extractPathAndUpdateParams($params)
  68. {
  69. $url = parse_url($this->url);
  70. if (!isset($url['path'])) {
  71. throw new Error\Api("Could not parse list url into parts: $url");
  72. }
  73. if (isset($url['query'])) {
  74. // If the URL contains a query param, parse it out into $params so they
  75. // don't interact weirdly with each other.
  76. $query = [];
  77. parse_str($url['query'], $query);
  78. $params = array_merge($params ?: [], $query);
  79. }
  80. return [$url['path'], $params];
  81. }
  82. }