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