Skip to content

Commit

Permalink
Add static update method (stripe#266)
Browse files Browse the repository at this point in the history
* Add a static `update` method
* Add a test for the update method
* Rename resource URL function and add docs
* Add update method to all necessary resources
  • Loading branch information
kyleconroy authored Jun 27, 2016
1 parent ad16ae7 commit 51d4ece
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 4 deletions.
12 changes: 12 additions & 0 deletions lib/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ public static function create($params = null, $opts = null)
return self::_create($params, $opts);
}

/**
* @param string $id The ID of the account to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Account The updated account.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|string|null $opts
*
Expand Down
32 changes: 29 additions & 3 deletions lib/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ public static function classUrl()
}

/**
* @return string The full API URL for this API resource.
* @return string The instance endpoint URL for the given class.
*/
public function instanceUrl()
public static function resourceUrl($id)
{
$id = $this['id'];
if ($id === null) {
$class = get_called_class();
$message = "Could not determine which URL to request: "
Expand All @@ -81,6 +80,14 @@ public function instanceUrl()
return "$base/$extn";
}

/**
* @return string The full API URL for this API resource.
*/
public function instanceUrl()
{
return static::resourceUrl($this['id']);
}

private static function _validateParams($params = null)
{
if ($params && !is_array($params)) {
Expand Down Expand Up @@ -151,6 +158,25 @@ protected static function _create($params = null, $options = null)
return $obj;
}

/**
* @param string $id The ID of the API resource to update.
* @param array|null $params
* @param array|string|null $opts
*
* @return ApiResource the updated API resource
*/
protected static function _update($id, $params = null, $options = null)
{
self::_validateParams($params);
$base = static::baseUrl();
$url = static::resourceUrl($id);

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

protected function _save($options = null)
{
$params = $this->serializeParameters();
Expand Down
12 changes: 12 additions & 0 deletions lib/ApplicationFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public static function retrieve($id, $opts = null)
return self::_retrieve($id, $opts);
}

/**
* @param string $id The ID of the application fee to update.
* @param array|null $params
* @param array|string|null $options
*
* @return ApplicationFee The updated application fee.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|null $params
* @param array|string|null $opts
Expand Down
12 changes: 12 additions & 0 deletions lib/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public static function create($params = null, $options = null)
return self::_create($params, $options);
}

/**
* @param string $id The ID of the charge to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Charge The updated charge.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|string|null $options
*
Expand Down
12 changes: 12 additions & 0 deletions lib/Coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public static function create($params = null, $opts = null)
return self::_create($params, $opts);
}

/**
* @param string $id The ID of the coupon to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Coupon The updated coupon.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|null $params
* @param array|string|null $opts
Expand Down
12 changes: 12 additions & 0 deletions lib/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public static function create($params = null, $opts = null)
return self::_create($params, $opts);
}

/**
* @param string $id The ID of the customer to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Customer The updated customer.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|string|null $opts
*
Expand Down
12 changes: 12 additions & 0 deletions lib/Dispute.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public static function all($params = null, $options = null)
return self::_all($params, $options);
}

/**
* @param string $id The ID of the dispute to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Dispute The updated dispute.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|string|null $options
*
Expand Down
12 changes: 12 additions & 0 deletions lib/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public static function all($params = null, $opts = null)
return self::_all($params, $opts);
}

/**
* @param string $id The ID of the invoice to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Invoice The updated invoice.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|null $params
* @param array|string|null $opts
Expand Down
12 changes: 12 additions & 0 deletions lib/InvoiceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public static function create($params = null, $opts = null)
return self::_create($params, $opts);
}

/**
* @param string $id The ID of the invoice item to update.
* @param array|null $params
* @param array|string|null $options
*
* @return InvoiceItem The updated invoice item.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|string|null $opts
*
Expand Down
12 changes: 12 additions & 0 deletions lib/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public static function create($params = null, $opts = null)
return self::_create($params, $opts);
}

/**
* @param string $id The ID of the order to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Order The updated order.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|string|null $opts
*
Expand Down
12 changes: 12 additions & 0 deletions lib/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public static function create($params = null, $opts = null)
return self::_create($params, $opts);
}

/**
* @param string $id The ID of the plan to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Plan The updated plan.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|null $params
* @param array|string|null $opts
Expand Down
12 changes: 12 additions & 0 deletions lib/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public static function create($params = null, $opts = null)
return self::_create($params, $opts);
}

/**
* @param string $id The ID of the product to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Product The updated product.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|string|null $opts
*
Expand Down
12 changes: 12 additions & 0 deletions lib/Recipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public static function create($params = null, $opts = null)
return self::_create($params, $opts);
}

/**
* @param string $id The ID of the recipient to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Recipient The updated recipient.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|string|null $opts
*
Expand Down
12 changes: 12 additions & 0 deletions lib/Refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ public static function retrieve($id, $options = null)
return self::_retrieve($id, $options);
}

/**
* @param string $id The ID of the refund to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Refund The updated refund.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|null $params
* @param array|string|null $options
Expand Down
12 changes: 12 additions & 0 deletions lib/SKU.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public static function create($params = null, $opts = null)
return self::_create($params, $opts);
}

/**
* @param string $id The ID of the SKU to update.
* @param array|null $params
* @param array|string|null $options
*
* @return SKU The updated SKU.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|string|null $opts
*
Expand Down
12 changes: 12 additions & 0 deletions lib/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public static function create($params = null, $opts = null)
return self::_create($params, $opts);
}

/**
* @param string $id The ID of the subscription to retrieve.
* @param array|null $params
* @param array|string|null $options
*
* @return Subscription The updated subscription.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|null $params
*
Expand Down
12 changes: 12 additions & 0 deletions lib/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public static function create($params = null, $opts = null)
return self::_create($params, $opts);
}

/**
* @param string $id The ID of the transfer to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Transfer The updated transfer.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @return TransferReversal The created transfer reversal.
*/
Expand Down
25 changes: 24 additions & 1 deletion tests/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testReject()
$this->assertSame($rejected->id, $account->id);
}

public function testUpdateLegalEntity()
public function testSaveLegalEntity()
{
$response = $this->managedAccountResponse('acct_ABC');
$this->mockRequest('POST', '/v1/accounts', array('managed' => 'true'), $response);
Expand All @@ -143,6 +143,29 @@ public function testUpdateLegalEntity()
$this->assertSame('Bob', $account->legal_entity->first_name);
}

public function testUpdateLegalEntity()
{
$response = $this->managedAccountResponse('acct_ABC');
$this->mockRequest('POST', '/v1/accounts', array('managed' => 'true'), $response);

$response['legal_entity']['first_name'] = 'Bob';
$this->mockRequest(
'POST',
'/v1/accounts/acct_ABC',
array('legal_entity' => array('first_name' => 'Bob')),
$response
);

$account = Account::create(array('managed' => true));
$account = Account::update($account['id'], array(
'legal_entity' => array(
'first_name' => 'Bob'
)
));

$this->assertSame('Bob', $account->legal_entity->first_name);
}

public function testCreateAdditionalOwners()
{
$request = array(
Expand Down
Loading

0 comments on commit 51d4ece

Please sign in to comment.