NestedResource.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Stripe\ApiOperations;
  3. /**
  4. * Trait for resources that have nested resources.
  5. *
  6. * This trait should only be applied to classes that derive from StripeObject.
  7. */
  8. trait NestedResource
  9. {
  10. /**
  11. * @param string $method
  12. * @param string $url
  13. * @param null|array $params
  14. * @param null|array|string $options
  15. *
  16. * @return \Stripe\StripeObject
  17. */
  18. protected static function _nestedResourceOperation($method, $url, $params = null, $options = null)
  19. {
  20. self::_validateParams($params);
  21. list($response, $opts) = static::_staticRequest($method, $url, $params, $options);
  22. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  23. $obj->setLastResponse($response);
  24. return $obj;
  25. }
  26. /**
  27. * @param string $id
  28. * @param string $nestedPath
  29. * @param null|string $nestedId
  30. *
  31. * @return string
  32. */
  33. protected static function _nestedResourceUrl($id, $nestedPath, $nestedId = null)
  34. {
  35. $url = static::resourceUrl($id) . $nestedPath;
  36. if (null !== $nestedId) {
  37. $url .= "/{$nestedId}";
  38. }
  39. return $url;
  40. }
  41. /**
  42. * @param string $id
  43. * @param string $nestedPath
  44. * @param null|array $params
  45. * @param null|array|string $options
  46. *
  47. * @throws \Stripe\Exception\ApiErrorException if the request fails
  48. *
  49. * @return \Stripe\StripeObject
  50. */
  51. protected static function _createNestedResource($id, $nestedPath, $params = null, $options = null)
  52. {
  53. $url = static::_nestedResourceUrl($id, $nestedPath);
  54. return self::_nestedResourceOperation('post', $url, $params, $options);
  55. }
  56. /**
  57. * @param string $id
  58. * @param string $nestedPath
  59. * @param null|string $nestedId
  60. * @param null|array $params
  61. * @param null|array|string $options
  62. *
  63. * @throws \Stripe\Exception\ApiErrorException if the request fails
  64. *
  65. * @return \Stripe\StripeObject
  66. */
  67. protected static function _retrieveNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null)
  68. {
  69. $url = static::_nestedResourceUrl($id, $nestedPath, $nestedId);
  70. return self::_nestedResourceOperation('get', $url, $params, $options);
  71. }
  72. /**
  73. * @param string $id
  74. * @param string $nestedPath
  75. * @param null|string $nestedId
  76. * @param null|array $params
  77. * @param null|array|string $options
  78. *
  79. * @throws \Stripe\Exception\ApiErrorException if the request fails
  80. *
  81. * @return \Stripe\StripeObject
  82. */
  83. protected static function _updateNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null)
  84. {
  85. $url = static::_nestedResourceUrl($id, $nestedPath, $nestedId);
  86. return self::_nestedResourceOperation('post', $url, $params, $options);
  87. }
  88. /**
  89. * @param string $id
  90. * @param string $nestedPath
  91. * @param null|string $nestedId
  92. * @param null|array $params
  93. * @param null|array|string $options
  94. *
  95. * @throws \Stripe\Exception\ApiErrorException if the request fails
  96. *
  97. * @return \Stripe\StripeObject
  98. */
  99. protected static function _deleteNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null)
  100. {
  101. $url = static::_nestedResourceUrl($id, $nestedPath, $nestedId);
  102. return self::_nestedResourceOperation('delete', $url, $params, $options);
  103. }
  104. /**
  105. * @param string $id
  106. * @param string $nestedPath
  107. * @param null|array $params
  108. * @param null|array|string $options
  109. *
  110. * @throws \Stripe\Exception\ApiErrorException if the request fails
  111. *
  112. * @return \Stripe\StripeObject
  113. */
  114. protected static function _allNestedResources($id, $nestedPath, $params = null, $options = null)
  115. {
  116. $url = static::_nestedResourceUrl($id, $nestedPath);
  117. return self::_nestedResourceOperation('get', $url, $params, $options);
  118. }
  119. }