Skip to content

Commit

Permalink
Merge pull request #411 from stripe/ob-use-traits
Browse files Browse the repository at this point in the history
Use traits for API operations methods
  • Loading branch information
ob-stripe committed Jan 4, 2018
2 parents 7353f3c + 2a5d478 commit 9f10791
Show file tree
Hide file tree
Showing 37 changed files with 253 additions and 1,375 deletions.
7 changes: 7 additions & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
require(dirname(__FILE__) . '/lib/Error/OAuth/UnsupportedGrantType.php');
require(dirname(__FILE__) . '/lib/Error/OAuth/UnsupportedResponseType.php');

// API operations
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/Retrieve.php');
require(dirname(__FILE__) . '/lib/ApiOperations/Update.php');

// Plumbing
require(dirname(__FILE__) . '/lib/ApiResponse.php');
require(dirname(__FILE__) . '/lib/StripeObject.php');
Expand Down
60 changes: 5 additions & 55 deletions lib/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
*/
class Account extends ApiResource
{
use ApiOperations\All;
use ApiOperations\Create;
use ApiOperations\Delete;
use ApiOperations\Update;

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

Expand Down Expand Up @@ -65,50 +70,6 @@ public static function retrieve($id = null, $opts = null)
return self::_retrieve($id, $opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return Account
*/
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
*
* @return Account
*/
public function save($opts = null)
{
return $this->_save($opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return Account The deleted account.
*/
public function delete($params = null, $opts = null)
{
return $this->_delete($params, $opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
Expand All @@ -123,17 +84,6 @@ public function reject($params = null, $opts = null)
return $this;
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return Collection of Accounts
*/
public static function all($params = null, $opts = null)
{
return self::_all($params, $opts);
}

/**
* @param array|null $clientId
* @param array|string|null $opts
Expand Down
22 changes: 22 additions & 0 deletions lib/ApiOperations/All.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Stripe\ApiOperations;

/**
* Trait for listable resources. Adds a `all()` static method to the class.
*
* This trait should only be applied to classes that derive from ApiResource.
*/
trait All
{
/**
* @param array|null $params
* @param array|string|null $opts
*
* @return Collection of ApiResources
*/
public static function all($params = null, $opts = null)
{
return self::_all($params, $opts);
}
}
22 changes: 22 additions & 0 deletions lib/ApiOperations/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Stripe\ApiOperations;

/**
* Trait for creatable resources. Adds a `create()` static method to the class.
*
* This trait should only be applied to classes that derive from ApiResource.
*/
trait Create
{
/**
* @param array|null $params
* @param array|string|null $options
*
* @return ApiResource The created resource.
*/
public static function create($params = null, $options = null)
{
return self::_create($params, $options);
}
}
22 changes: 22 additions & 0 deletions lib/ApiOperations/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Stripe\ApiOperations;

/**
* Trait for deletable resources. Adds a `delete()` method to the class.
*
* This trait should only be applied to classes that derive from ApiResource.
*/
trait Delete
{
/**
* @param array|null $params
* @param array|string|null $opts
*
* @return ApiResource The deleted resource.
*/
public function delete($params = null, $opts = null)
{
return $this->_delete($params, $opts);
}
}
24 changes: 24 additions & 0 deletions lib/ApiOperations/Retrieve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Stripe\ApiOperations;

/**
* Trait for retrievable resources. Adds a `retrieve()` static method to the
* class.
*
* This trait should only be applied to classes that derive from ApiResource.
*/
trait Retrieve
{
/**
* @param array|string $id The ID of the API resource to retrieve,
* or an options array containing an `id` key.
* @param array|string|null $opts
*
* @return ApiResource
*/
public static function retrieve($id, $opts = null)
{
return self::_retrieve($id, $opts);
}
}
34 changes: 34 additions & 0 deletions lib/ApiOperations/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Stripe\ApiOperations;

/**
* 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.
*/
trait Update
{
/**
* @param string $id The ID of the resource to update.
* @param array|null $params
* @param array|string|null $options
*
* @return ApiResource The updated resource.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|string|null $options
*
* @return ApiResource The saved resource.
*/
public function save($options = null)
{
return $this->_save($options);
}
}
51 changes: 5 additions & 46 deletions lib/ApplePayDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
*/
class ApplePayDomain extends ApiResource
{

use ApiOperations\All;
use ApiOperations\Create;
use ApiOperations\Delete;
use ApiOperations\Retrieve;

/**
* @return string The class URL for this resource. It needs to be special
* cased because it doesn't fit into the standard resource pattern.
Expand All @@ -18,49 +22,4 @@ public static function classUrl()
{
return '/v1/apple_pay/domains';
}

/**
* @param array|string $id The ID of the domain to retrieve, or an options
* array containing an `id` key.
* @param array|string|null $opts
*
* @return ApplePayDomain
*/
public static function retrieve($id, $opts = null)
{
return self::_retrieve($id, $opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return ApplePayDomain The created domain.
*/
public static function create($params = null, $opts = null)
{
return self::_create($params, $opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return ApplePayDomain The deleted domain.
*/
public function delete($params = null, $opts = null)
{
return $this->_delete($params, $opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return Collection of ApplePayDomains
*/
public static function all($params = null, $opts = null)
{
return self::_all($params, $opts);
}
}
26 changes: 3 additions & 23 deletions lib/ApplicationFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
class ApplicationFee extends ApiResource
{
use ApiOperations\All;
use ApiOperations\Retrieve;

const PATH_REFUNDS = '/refunds';

/**
Expand All @@ -22,29 +25,6 @@ public static function className()
return 'application_fee';
}

/**
* @param array|string $id The ID of the application fee to retrieve, or an
* options array containing an `id` key.
* @param array|string|null $opts
*
* @return ApplicationFee
*/
public static function retrieve($id, $opts = null)
{
return self::_retrieve($id, $opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return Collection of ApplicationFees
*/
public static function all($params = null, $opts = null)
{
return self::_all($params, $opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
Expand Down
26 changes: 3 additions & 23 deletions lib/BalanceTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/
class BalanceTransaction extends ApiResource
{
use ApiOperations\All;
use ApiOperations\Retrieve;

/**
* @return string The class URL for this resource. It needs to be special
* cased because it doesn't fit into the standard resource pattern.
Expand All @@ -32,27 +35,4 @@ public static function classUrl()
{
return "/v1/balance/history";
}

/**
* @param array|string $id The ID of the balance transaction to retrieve,
* or an options array containing an `id` key.
* @param array|string|null $opts
*
* @return BalanceTransaction
*/
public static function retrieve($id, $opts = null)
{
return self::_retrieve($id, $opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return Collection of BalanceTransactions
*/
public static function all($params = null, $opts = null)
{
return self::_all($params, $opts);
}
}
Loading

0 comments on commit 9f10791

Please sign in to comment.