Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parameter serialization logic #404

Merged
merged 2 commits into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion init.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
require(dirname(__FILE__) . '/lib/Error/OAuth/UnsupportedResponseType.php');

// API operations
echo "bouh";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover from manual debugging 😨

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Totally missed that apparently, haha.

require(dirname(__FILE__) . '/lib/ApiOperations/All.php');
require(dirname(__FILE__) . '/lib/ApiOperations/Create.php');
require(dirname(__FILE__) . '/lib/ApiOperations/Delete.php');
require(dirname(__FILE__) . '/lib/ApiOperations/NestedResource.php');
require(dirname(__FILE__) . '/lib/ApiOperations/Request.php');
require(dirname(__FILE__) . '/lib/ApiOperations/Retrieve.php');
require(dirname(__FILE__) . '/lib/ApiOperations/Update.php');

Expand Down
4 changes: 4 additions & 0 deletions lib/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class Account extends ApiResource
use ApiOperations\All;
use ApiOperations\Create;
use ApiOperations\Delete;
use ApiOperations\NestedResource;
use ApiOperations\Retrieve {
retrieve as protected _retrieve;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resources that have their own custom implementation of API methods but need to call the standard implementation (usually because they have some additional parameter checking) can use this syntax to "inherit" the trait methods under a different name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's pretty cool. Thanks for the ntoe!

}
use ApiOperations\Update;

public static function getSavedNestedResources()
Expand Down
16 changes: 14 additions & 2 deletions lib/ApiOperations/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Trait for listable resources. Adds a `all()` static method to the class.
*
* This trait should only be applied to classes that derive from ApiResource.
* This trait should only be applied to classes that derive from StripeObject.
*/
trait All
{
Expand All @@ -17,6 +17,18 @@ trait All
*/
public static function all($params = null, $opts = null)
{
return self::_all($params, $opts);
self::_validateParams($params);
$url = static::classUrl();

list($response, $opts) = static::_staticRequest('get', $url, $params, $opts);
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
if (!is_a($obj, 'Stripe\\Collection')) {
$class = get_class($obj);
$message = "Expected type \"Stripe\\Collection\", got \"$class\" instead";
throw new Error\Api($message);
}
$obj->setLastResponse($response);
$obj->setRequestParams($params);
return $obj;
}
}
10 changes: 8 additions & 2 deletions lib/ApiOperations/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Trait for creatable resources. Adds a `create()` static method to the class.
*
* This trait should only be applied to classes that derive from ApiResource.
* This trait should only be applied to classes that derive from StripeObject.
*/
trait Create
{
Expand All @@ -17,6 +17,12 @@ trait Create
*/
public static function create($params = null, $options = null)
{
return self::_create($params, $options);
self::_validateParams($params);
$url = static::classUrl();

list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
$obj->setLastResponse($response);
return $obj;
}
}
9 changes: 7 additions & 2 deletions lib/ApiOperations/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Trait for deletable resources. Adds a `delete()` method to the class.
*
* This trait should only be applied to classes that derive from ApiResource.
* This trait should only be applied to classes that derive from StripeObject.
*/
trait Delete
{
Expand All @@ -17,6 +17,11 @@ trait Delete
*/
public function delete($params = null, $opts = null)
{
return $this->_delete($params, $opts);
self::_validateParams($params);

$url = $this->instanceUrl();
list($response, $opts) = $this->_request('delete', $url, $params, $opts);
$this->refreshFrom($response, $opts);
return $this;
}
}
115 changes: 115 additions & 0 deletions lib/ApiOperations/NestedResource.php
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);
}
}
64 changes: 64 additions & 0 deletions lib/ApiOperations/Request.php
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];
}
}
9 changes: 6 additions & 3 deletions lib/ApiOperations/Retrieve.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Trait for retrievable resources. Adds a `retrieve()` static method to the
* class.
*
* This trait should only be applied to classes that derive from ApiResource.
* This trait should only be applied to classes that derive from StripeObject.
*/
trait Retrieve
{
Expand All @@ -15,10 +15,13 @@ trait Retrieve
* or an options array containing an `id` key.
* @param array|string|null $opts
*
* @return ApiResource
* @return Stripe\StripeObject
*/
public static function retrieve($id, $opts = null)
{
return self::_retrieve($id, $opts);
$opts = \Stripe\Util\RequestOptions::parse($opts);
$instance = new static($id, $opts);
$instance->refresh();
return $instance;
}
}
26 changes: 19 additions & 7 deletions lib/ApiOperations/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,41 @@
* Trait for updatable resources. Adds an `update()` static method and a
* `save()` method to the class.
*
* This trait should only be applied to classes that derive from ApiResource.
* This trait should only be applied to classes that derive from StripeObject.
*/
trait Update
{
/**
* @param string $id The ID of the resource to update.
* @param array|null $params
* @param array|string|null $options
* @param array|string|null $opts
*
* @return ApiResource The updated resource.
*/
public static function update($id, $params = null, $options = null)
public static function update($id, $params = null, $opts = null)
{
return self::_update($id, $params, $options);
self::_validateParams($params);
$url = static::resourceUrl($id);

list($response, $opts) = static::_staticRequest('post', $url, $params, $opts);
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
$obj->setLastResponse($response);
return $obj;
}

/**
* @param array|string|null $options
* @param array|string|null $opts
*
* @return ApiResource The saved resource.
*/
public function save($options = null)
public function save($opts = null)
{
return $this->_save($options);
$params = $this->serializeParameters();
if (count($params) > 0) {
$url = $this->instanceUrl();
list($response, $opts) = $this->_request('post', $url, $params, $opts);
$this->refreshFrom($response, $opts);
}
return $this;
}
}
Loading