-
Notifications
You must be signed in to change notification settings - Fork 852
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #404 from stripe/ob-serialize-params-2
Fix parameter serialization logic
- Loading branch information
Showing
42 changed files
with
1,247 additions
and
501 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
namespace Stripe\ApiOperations; | ||
|
||
/** | ||
* Trait for resources that have nested resources. | ||
* | ||
* This trait should only be applied to classes that derive from StripeObject. | ||
*/ | ||
trait NestedResource | ||
{ | ||
/** | ||
* @param string $method | ||
* @param string $url | ||
* @param array|null $params | ||
* @param array|string|null $options | ||
* | ||
* @return Stripe\StripeObject | ||
*/ | ||
protected static function _nestedResourceOperation($method, $url, $params = null, $options = null) | ||
{ | ||
self::_validateParams($params); | ||
|
||
list($response, $opts) = static::_staticRequest($method, $url, $params, $options); | ||
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts); | ||
$obj->setLastResponse($response); | ||
return $obj; | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param string $nestedPath | ||
* @param string|null $nestedId | ||
* | ||
* @return string | ||
*/ | ||
protected static function _nestedResourceUrl($id, $nestedPath, $nestedId = null) | ||
{ | ||
$url = static::resourceUrl($id) . $nestedPath; | ||
if ($nestedId !== null) { | ||
$url .= "/$nestedId"; | ||
} | ||
return $url; | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param string $nestedPath | ||
* @param array|null $params | ||
* @param array|string|null $options | ||
* | ||
* @return Stripe\StripeObject | ||
*/ | ||
protected static function _createNestedResource($id, $nestedPath, $params = null, $options = null) | ||
{ | ||
$url = static::_nestedResourceUrl($id, $nestedPath); | ||
return self::_nestedResourceOperation('post', $url, $params, $options); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param string $nestedPath | ||
* @param array|null $params | ||
* @param array|string|null $options | ||
* | ||
* @return Stripe\StripeObject | ||
*/ | ||
protected static function _retrieveNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null) | ||
{ | ||
$url = static::_nestedResourceUrl($id, $nestedPath, $nestedId); | ||
return self::_nestedResourceOperation('get', $url, $params, $options); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param string $nestedPath | ||
* @param array|null $params | ||
* @param array|string|null $options | ||
* | ||
* @return Stripe\StripeObject | ||
*/ | ||
protected static function _updateNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null) | ||
{ | ||
$url = static::_nestedResourceUrl($id, $nestedPath, $nestedId); | ||
return self::_nestedResourceOperation('post', $url, $params, $options); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param string $nestedPath | ||
* @param array|null $params | ||
* @param array|string|null $options | ||
* | ||
* @return Stripe\StripeObject | ||
*/ | ||
protected static function _deleteNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null) | ||
{ | ||
$url = static::_nestedResourceUrl($id, $nestedPath, $nestedId); | ||
return self::_nestedResourceOperation('delete', $url, $params, $options); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param string $nestedPath | ||
* @param array|null $params | ||
* @param array|string|null $options | ||
* | ||
* @return Stripe\StripeObject | ||
*/ | ||
protected static function _allNestedResources($id, $nestedPath, $params = null, $options = null) | ||
{ | ||
$url = static::_nestedResourceUrl($id, $nestedPath); | ||
return self::_nestedResourceOperation('get', $url, $params, $options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Stripe\ApiOperations; | ||
|
||
/** | ||
* Trait for resources that need to make API requests. | ||
* | ||
* This trait should only be applied to classes that derive from StripeObject. | ||
*/ | ||
trait Request | ||
{ | ||
/** | ||
* @param array|null|mixed $params The list of parameters to validate | ||
* | ||
* @throws Stripe\Error\Api if $params exists and is not an array | ||
*/ | ||
protected static function _validateParams($params = null) | ||
{ | ||
if ($params && !is_array($params)) { | ||
$message = "You must pass an array as the first argument to Stripe API " | ||
. "method calls. (HINT: an example call to create a charge " | ||
. "would be: \"Stripe\\Charge::create(['amount' => 100, " | ||
. "'currency' => 'usd', 'source' => 'tok_1234'])\")"; | ||
throw new Error\Api($message); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $method HTTP method ('get', 'post', etc.) | ||
* @param string $url URL for the request | ||
* @param array $params list of parameters for the request | ||
* @param array|string|null $options | ||
* | ||
* @return array tuple containing (the JSON response, $options) | ||
*/ | ||
protected function _request($method, $url, $params = [], $options = null) | ||
{ | ||
$opts = $this->_opts->merge($options); | ||
list($resp, $options) = static::_staticRequest($method, $url, $params, $opts); | ||
$this->setLastResponse($resp); | ||
return [$resp->json, $options]; | ||
} | ||
|
||
/** | ||
* @param string $method HTTP method ('get', 'post', etc.) | ||
* @param string $url URL for the request | ||
* @param array $params list of parameters for the request | ||
* @param array|string|null $options | ||
* | ||
* @return array tuple containing (the JSON response, $options) | ||
*/ | ||
protected static function _staticRequest($method, $url, $params, $options) | ||
{ | ||
$opts = \Stripe\Util\RequestOptions::parse($options); | ||
$requestor = new \Stripe\ApiRequestor($opts->apiKey, static::baseUrl()); | ||
list($response, $opts->apiKey) = $requestor->request($method, $url, $params, $opts->headers); | ||
foreach ($opts->headers as $k => $v) { | ||
if (!array_key_exists($k, self::$HEADERS_TO_PERSIST)) { | ||
unset($opts->headers[$k]); | ||
} | ||
} | ||
return [$response, $opts]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.