Skip to content

Commit

Permalink
Adds a getType method on Resource
Browse files Browse the repository at this point in the history
Resolves #210
Resolves #245

This adds a method `getType` on every resource object which returns the
xml attribute `type`. This can currently be found on Adjustments,
Transactions, and BillingInfos.
  • Loading branch information
bhelx committed Jan 19, 2017
1 parent 87f2056 commit 85a89c0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions Tests/Recurly/Adjustment_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function testGetAdjustment() {
$this->assertInstanceOf('Recurly_Stub', $adjustment->account);
$this->assertInstanceOf('Recurly_Stub', $adjustment->invoice);
$this->assertInstanceOf('Recurly_Stub', $adjustment->subscription);
$this->assertEquals('charge', $adjustment->getType());
$this->assertEquals('abcdef1234567890', $adjustment->uuid);
$this->assertEquals('invoiced', $adjustment->state);
$this->assertEquals('$12 Annual Subscription', $adjustment->description);
Expand Down
1 change: 1 addition & 0 deletions Tests/Recurly/Billing_Info_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testGetBillingInfo() {
$this->assertEquals($billing_info->year, 2015);
$this->assertEquals($billing_info->month, 1);
$this->assertEquals($billing_info->getHref(), 'https://api.recurly.com/v2/accounts/abcdef1234567890/billing_info');
$this->assertEquals($billing_info->getType(), 'credit_card');
}

public function testGetPayPalBillingInfo() {
Expand Down
1 change: 1 addition & 0 deletions Tests/Recurly/Transaction_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function testGetTransaction() {
$this->assertInstanceOf('Recurly_FraudInfo', $transaction->fraud);
$this->assertEquals($transaction->fraud->score, 99);
$this->assertEquals($transaction->fraud->decision, 'DECLINE');
$this->assertEquals($transaction->getType(), 'credit_card');
}

public function testCreateTransactionFailed() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/fixtures/billing_info/show-200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<billing_info href="https://api.recurly.com/v2/accounts/abcdef1234567890/billing_info">
<billing_info href="https://api.recurly.com/v2/accounts/abcdef1234567890/billing_info" type="credit_card">
<account href="https://api.recurly.com/v2/accounts/abcdef1234567890"/>
<first_name>Larry</first_name>
<last_name>David</last_name>
Expand Down
13 changes: 13 additions & 0 deletions lib/recurly/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
abstract class Recurly_Base
{
protected $_href;
protected $_type;
protected $_client;
protected $_links;

Expand Down Expand Up @@ -149,6 +150,11 @@ public function setHref($href) {
$this->_href = $href;
}

/** Refers to the `type` root xml attribute **/
public function getType() {
return $this->_type;
}

private function addLink($name, $href, $method){
$this->_links[$name] = new Recurly_Link($name, $href, $method);
}
Expand Down Expand Up @@ -299,6 +305,7 @@ protected static function __parseXmlToObject($node, &$object)
}

$new_obj = Recurly_Resource::__createNodeObject($node);

if (!is_null($new_obj)) {
if (is_object($new_obj) || is_array($new_obj)) {
Recurly_Resource::__parseXmlToObject($node->firstChild, $new_obj);
Expand Down Expand Up @@ -403,6 +410,12 @@ private static function __createNodeObject($node)
} else
$new_obj = new $node_class();

// It may have a type attribute we wish to capture
$typeAttribute = $node->getAttribute('type');
if (!empty($typeAttribute)) {
$new_obj->_type = $typeAttribute;
}

$href = $node->getAttribute('href');
if (!empty($href)) {
$new_obj->setHref($href);
Expand Down

0 comments on commit 85a89c0

Please sign in to comment.