Skip to content

Commit

Permalink
Fix parameter serialization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Jan 6, 2018
1 parent f1d8593 commit 42f1b44
Show file tree
Hide file tree
Showing 29 changed files with 1,073 additions and 269 deletions.
1 change: 0 additions & 1 deletion init.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
require(dirname(__FILE__) . '/lib/ApiRequestor.php');
require(dirname(__FILE__) . '/lib/ApiResource.php');
require(dirname(__FILE__) . '/lib/SingletonApiResource.php');
require(dirname(__FILE__) . '/lib/AttachedObject.php');
require(dirname(__FILE__) . '/lib/ExternalAccount.php');

// Stripe API Resources
Expand Down
53 changes: 53 additions & 0 deletions lib/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ class Account extends ApiResource
use ApiOperations\Delete;
use ApiOperations\Update;

public static function getSavedNestedResources()
{
static $savedNestedResources = null;
if ($savedNestedResources === null) {
$savedNestedResources = new Util\Set([
'external_account',
'bank_account',
]);
}
return $savedNestedResources;
}

const PATH_EXTERNAL_ACCOUNTS = '/external_accounts';
const PATH_LOGIN_LINKS = '/login_links';

Expand Down Expand Up @@ -173,4 +185,45 @@ public static function createLoginLink($id, $params = null, $opts = null)
{
return self::_createNestedResource($id, static::PATH_LOGIN_LINKS, $params, $opts);
}

public function serializeParameters($force = false)
{
$update = parent::serializeParameters($force);
if (isset($this->_values['legal_entity'])) {
$entity = $this['legal_entity'];
if (isset($entity->_values['additional_owners'])) {
$owners = $entity['additional_owners'];
$entityUpdate = isset($update['legal_entity']) ? $update['legal_entity'] : [];
$entityUpdate['additional_owners'] = $this->serializeAdditionalOwners($entity, $owners);
$update['legal_entity'] = $entityUpdate;
}
}
return $update;
}

private function serializeAdditionalOwners($legalEntity, $additionalOwners)
{
if (isset($legalEntity->_originalValues['additional_owners'])) {
$originalValue = $legalEntity->_originalValues['additional_owners'];
} else {
$originalValue = [];
}
if (($originalValue) && (count($originalValue) > count($additionalOwners))) {
throw new \InvalidArgumentException(
"You cannot delete an item from an array, you must instead set a new array"
);
}

$updateArr = [];
foreach ($additionalOwners as $i => $v) {
$update = ($v instanceof StripeObject) ? $v->serializeParameters() : $v;

if ($update !== []) {
if (!$originalValue || ($update != $legalEntity->serializeParamsValue($originalValue[$i], null, false, true))) {
$updateArr[$i] = $update;
}
}
}
return $updateArr;
}
}
114 changes: 105 additions & 9 deletions lib/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,48 @@
*/
abstract class ApiResource extends StripeObject
{
private static $HEADERS_TO_PERSIST = ['Stripe-Account' => true, 'Stripe-Version' => true];
/**
* @return Stripe\Util\Set A list of fields that can be their own type of
* API resource (say a nested card under an account for example), and if
* that resource is set, it should be transmitted to the API on a create or
* update. Doing so is not the default behavior because API resources
* should normally be persisted on their own RESTful endpoints.
*/
public static function getSavedNestedResources()
{
static $savedNestedResources = null;
if ($savedNestedResources === null) {
$savedNestedResources = new Util\Set();
}
return $savedNestedResources;
}

public static function baseUrl()
/**
* @var array A list of headers that should be persisted across requests.
*/
private static $HEADERS_TO_PERSIST = [
'Stripe-Account' => true,
'Stripe-Version' => true
];

/**
* @var boolean A flag that can be set a behavior that will cause this
* resource to be encoded and sent up along with an update of its parent
* resource. This is usually not desirable because resources are updated
* individually on their own endpoints, but there are certain cases,
* replacing a customer's source for example, where this is allowed.
*/
public $saveWithParent = false;

public function __set($k, $v)
{
return Stripe::$apiBase;
parent::__set($k, $v);
$v = $this->$k;
if ((static::getSavedNestedResources()->includes($k)) &&
($v instanceof ApiResource)) {
$v->saveWithParent = true;
}
return $v;
}

/**
Expand Down Expand Up @@ -59,6 +96,14 @@ public static function className()
return $name;
}

/**
* @return string The base URL for the given class.
*/
public static function baseUrl()
{
return Stripe::$apiBase;
}

/**
* @return string The endpoint URL for the given class.
*/
Expand Down Expand Up @@ -93,6 +138,11 @@ public function instanceUrl()
return static::resourceUrl($this['id']);
}

/**
* @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)) {
Expand All @@ -104,6 +154,14 @@ protected static function _validateParams($params = null)
}
}

/**
* @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);
Expand All @@ -112,6 +170,14 @@ protected function _request($method, $url, $params = [], $options = null)
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 = Util\RequestOptions::parse($options);
Expand All @@ -125,6 +191,13 @@ protected static function _staticRequest($method, $url, $params, $options)
return [$response, $opts];
}

/**
* @param string|array $id The ID of the API resource to retrieve, or the
* list of parameters for the request.
* @param array|string|null $options
*
* @return Stripe\ApiResource the retrieved API resource
*/
protected static function _retrieve($id, $options = null)
{
$opts = Util\RequestOptions::parse($options);
Expand All @@ -133,6 +206,12 @@ protected static function _retrieve($id, $options = null)
return $instance;
}

/**
* @param array $params The list of parameters for the request.
* @param array|string|null $options
*
* @return Stripe\Collection the retrieved list of API resources
*/
protected static function _all($params = null, $options = null)
{
self::_validateParams($params);
Expand All @@ -150,6 +229,12 @@ protected static function _all($params = null, $options = null)
return $obj;
}

/**
* @param array $params The list of parameters for the request.
* @param array|string|null $options
*
* @return Stripe\ApiResource the created API resource
*/
protected static function _create($params = null, $options = null)
{
self::_validateParams($params);
Expand Down Expand Up @@ -179,6 +264,11 @@ protected static function _update($id, $params = null, $options = null)
return $obj;
}

/**
* @param array|string|null $options
*
* @return Stripe\ApiResource the updated API resource
*/
protected function _save($options = null)
{
$params = $this->serializeParameters();
Expand All @@ -190,6 +280,12 @@ protected function _save($options = null)
return $this;
}

/**
* @param array $params The list of parameters for the request.
* @param array|string|null $options
*
* @return Stripe\ApiResource the deleted API resource
*/
protected function _delete($params = null, $options = null)
{
self::_validateParams($params);
Expand All @@ -206,7 +302,7 @@ protected function _delete($params = null, $options = null)
* @param array|null $params
* @param array|string|null $options
*
* @return StripeObject
* @return Stripe\StripeObject
*/
protected static function _nestedResourceOperation($method, $url, $params = null, $options = null)
{
Expand Down Expand Up @@ -240,7 +336,7 @@ protected static function _nestedResourceUrl($id, $nestedPath, $nestedId = null)
* @param array|null $params
* @param array|string|null $options
*
* @return StripeObject
* @return Stripe\StripeObject
*/
protected static function _createNestedResource($id, $nestedPath, $params = null, $options = null)
{
Expand All @@ -254,7 +350,7 @@ protected static function _createNestedResource($id, $nestedPath, $params = null
* @param array|null $params
* @param array|string|null $options
*
* @return StripeObject
* @return Stripe\StripeObject
*/
protected static function _retrieveNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null)
{
Expand All @@ -268,7 +364,7 @@ protected static function _retrieveNestedResource($id, $nestedPath, $nestedId, $
* @param array|null $params
* @param array|string|null $options
*
* @return StripeObject
* @return Stripe\StripeObject
*/
protected static function _updateNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null)
{
Expand All @@ -282,7 +378,7 @@ protected static function _updateNestedResource($id, $nestedPath, $nestedId, $pa
* @param array|null $params
* @param array|string|null $options
*
* @return StripeObject
* @return Stripe\StripeObject
*/
protected static function _deleteNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null)
{
Expand All @@ -296,7 +392,7 @@ protected static function _deleteNestedResource($id, $nestedPath, $nestedId, $pa
* @param array|null $params
* @param array|string|null $options
*
* @return StripeObject
* @return Stripe\StripeObject
*/
protected static function _allNestedResources($id, $nestedPath, $params = null, $options = null)
{
Expand Down
43 changes: 0 additions & 43 deletions lib/AttachedObject.php

This file was deleted.

39 changes: 38 additions & 1 deletion lib/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,47 @@
*
* @package Stripe
*/
class Collection extends ApiResource
class Collection extends StripeObject
{
protected $_requestParams = [];

/**
* @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 = Util\RequestOptions::parse($options);
$requestor = new ApiRequestor($opts->apiKey);
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];
}

public function setRequestParams($params)
{
$this->_requestParams = $params;
Expand Down
Loading

0 comments on commit 42f1b44

Please sign in to comment.