omise/omise-php

OmiseCardList::Count() works incorrect

Closed this issue · 7 comments

1dnmr commented

Hello,

count from OmiseObject work incorrect.

Please add to OmiseCardList.php this function:
/**
*
* @return integer
*/
public function count()
{
if (array_key_exists('data', $this->_values) === true) {
return count($this->_values['data']);
} else {
return 0;
}
}

Also please add following function:
/**
* @param integer $index
*
* @return OmiseCard
*/
public function getCardByIndex($index)
{
if (array_key_exists('data', $this->_values) === true && $index >= 0 && $index < $this->count()) {
return $this->_values['data'][$index];
} else {
return null;
}
}

Hi @1dnmr

Thanks for report and submit your code here.
I see for your point on count() method. By the way, to retrieve a 'data total', I suggest you to implement with the following approach instead.

$charges = OmiseCharge::retrieve();

echo $charges['total'];

It's because count($this->_values['data']) will just only give you a total of an array that you specify in limit parameter (default: 20) but not exactly total items that you have in your account.

For example , if you have 150 charge transactions.

$charges = OmiseCharge::retrieve();

echo $charges->count(); // would return just 20, not 150.

Anyway, I'll consider more on above use case and interface. Thanks again :)

1dnmr commented

@guzzilar But this is not about Charge, as I mention its about OmiseCardList object. This object does not have 'total'.

@1dnmr Sorry I may not fully understand your issue. Can you explain me more on your use case or any example code that you've implemented with and found the issue?

If you want to get a total of cards that belong to a customer, you can go with

$customers = OmiseCustomer::retrieve('cust_test_xxx');
$cards     = $customers->cards();

echo $cards['total'];
1dnmr commented

Yes, it works, sorry.
I tried before use this code. And count() not work as expected.

$customers = OmiseCustomer::retrieve('cust_test_xxx');
$cards     = $customers->cards();

echo $cards->count();

Also how to retrieve card by Index with existing code?
I added new method getCardByIndex to do this.

@1dnmr Haha no worry.

For retrieve card by index, looking at your code I think this is what you are looking for?

$customers = OmiseCustomer::retrieve('cust_test_53q3u1qxziva8mhw9ld');
$cards     = $customers->cards();

$card_at_index_2 = $cards['data'][2];

// Or 

echo $cards['data'][2]['id']; // card_test_xxx
1dnmr commented

@guzzilar Right, thank you! :)

Welcome :D 👍