AbstractService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Stripe\Service;
  3. /**
  4. * Abstract base class for all services.
  5. */
  6. abstract class AbstractService
  7. {
  8. /**
  9. * @var \Stripe\StripeClientInterface
  10. */
  11. protected $client;
  12. /**
  13. * @var \Stripe\StripeStreamingClientInterface
  14. */
  15. protected $streamingClient;
  16. /**
  17. * Initializes a new instance of the {@link AbstractService} class.
  18. *
  19. * @param \Stripe\StripeClientInterface $client
  20. */
  21. public function __construct($client)
  22. {
  23. $this->client = $client;
  24. $this->streamingClient = $client;
  25. }
  26. /**
  27. * Gets the client used by this service to send requests.
  28. *
  29. * @return \Stripe\StripeClientInterface
  30. */
  31. public function getClient()
  32. {
  33. return $this->client;
  34. }
  35. /**
  36. * Gets the client used by this service to send requests.
  37. *
  38. * @return \Stripe\StripeStreamingClientInterface
  39. */
  40. public function getStreamingClient()
  41. {
  42. return $this->streamingClient;
  43. }
  44. /**
  45. * Translate null values to empty strings. For service methods,
  46. * we interpret null as a request to unset the field, which
  47. * corresponds to sending an empty string for the field to the
  48. * API.
  49. *
  50. * @param null|array $params
  51. */
  52. private static function formatParams($params)
  53. {
  54. if (null === $params) {
  55. return null;
  56. }
  57. \array_walk_recursive($params, function (&$value, $key) {
  58. if (null === $value) {
  59. $value = '';
  60. }
  61. });
  62. return $params;
  63. }
  64. protected function request($method, $path, $params, $opts)
  65. {
  66. return $this->getClient()->request($method, $path, self::formatParams($params), $opts);
  67. }
  68. protected function requestStream($method, $path, $readBodyChunkCallable, $params, $opts)
  69. {
  70. // TODO (MAJOR): Add this method to StripeClientInterface
  71. // @phpstan-ignore-next-line
  72. return $this->getStreamingClient()->requestStream($method, $path, $readBodyChunkCallable, self::formatParams($params), $opts);
  73. }
  74. protected function requestCollection($method, $path, $params, $opts)
  75. {
  76. // TODO (MAJOR): Add this method to StripeClientInterface
  77. // @phpstan-ignore-next-line
  78. return $this->getClient()->requestCollection($method, $path, self::formatParams($params), $opts);
  79. }
  80. protected function requestSearchResult($method, $path, $params, $opts)
  81. {
  82. // TODO (MAJOR): Add this method to StripeClientInterface
  83. // @phpstan-ignore-next-line
  84. return $this->getClient()->requestSearchResult($method, $path, self::formatParams($params), $opts);
  85. }
  86. protected function buildPath($basePath, ...$ids)
  87. {
  88. foreach ($ids as $id) {
  89. if (null === $id || '' === \trim($id)) {
  90. $msg = 'The resource ID cannot be null or whitespace.';
  91. throw new \Stripe\Exception\InvalidArgumentException($msg);
  92. }
  93. }
  94. return \sprintf($basePath, ...\array_map('\urlencode', $ids));
  95. }
  96. }