Collection.php 2.5 KB

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